Skip to content

Commit 1af7fa2

Browse files
committed
Merge pull request #26292 from Haarolean
* pr/26292: Polish "Improve failure analysis with a single bean cycle" Improve failure analysis with a single bean cycle Closes gh-26292
2 parents 3f528bb + a6e59b3 commit 1af7fa2

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,11 +70,12 @@ private String buildMessage(DependencyCycle dependencyCycle) {
7070
message.append(
7171
String.format("The dependencies of some of the beans in the application context form a cycle:%n%n"));
7272
List<BeanInCycle> beansInCycle = dependencyCycle.getBeansInCycle();
73+
boolean singleBean = beansInCycle.size() == 1;
7374
int cycleStart = dependencyCycle.getCycleStart();
7475
for (int i = 0; i < beansInCycle.size(); i++) {
7576
BeanInCycle beanInCycle = beansInCycle.get(i);
7677
if (i == cycleStart) {
77-
message.append(String.format("┌─────┐%n"));
78+
message.append(String.format(singleBean ? "┌──->──┐%n" : "┌─────┐%n"));
7879
}
7980
else if (i > 0) {
8081
String leftSide = (i < cycleStart) ? " " : "↑";
@@ -83,7 +84,7 @@ else if (i > 0) {
8384
String leftSide = (i < cycleStart) ? " " : "|";
8485
message.append(String.format("%s %s%n", leftSide, beanInCycle));
8586
}
86-
message.append(String.format("└─────┘%n"));
87+
message.append(String.format(singleBean ? "└──<-──┘%n" : "└─────┘%n"));
8788
return message.toString();
8889
}
8990

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzerTests.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -109,6 +109,19 @@ void cycleReferencedViaOtherBeans() throws IOException {
109109
assertThat(lines.get(11)).isEqualTo("└─────┘");
110110
}
111111

112+
@Test
113+
void testSelfReferenceCycle() throws IOException {
114+
FailureAnalysis analysis = performAnalysis(SelfReferenceBeanConfiguration.class);
115+
List<String> lines = readDescriptionLines(analysis);
116+
assertThat(lines).hasSize(5);
117+
assertThat(lines.get(0))
118+
.isEqualTo("The dependencies of some of the beans in the application context form a cycle:");
119+
assertThat(lines.get(1)).isEqualTo("");
120+
assertThat(lines.get(2)).isEqualTo("┌──->──┐");
121+
assertThat(lines.get(3)).startsWith("| bean defined in " + SelfReferenceBeanConfiguration.class.getName());
122+
assertThat(lines.get(4)).isEqualTo("└──<-──┘");
123+
}
124+
112125
@Test
113126
void cycleWithAnUnknownStartIsNotAnalyzed() {
114127
assertThat(this.analyzer.analyze(new BeanCurrentlyInCreationException("test"))).isNull();
@@ -240,6 +253,16 @@ BeanThree three(BeanOne one) {
240253

241254
}
242255

256+
@Configuration(proxyBeanMethods = false)
257+
static class SelfReferenceBeanConfiguration {
258+
259+
@Bean
260+
SelfReferenceBean bean(SelfReferenceBean bean) {
261+
return new SelfReferenceBean();
262+
}
263+
264+
}
265+
243266
static class RefererOne {
244267

245268
@Autowired
@@ -266,4 +289,11 @@ static class BeanThree {
266289

267290
}
268291

292+
static class SelfReferenceBean {
293+
294+
@Autowired
295+
SelfReferenceBean bean;
296+
297+
}
298+
269299
}

0 commit comments

Comments
 (0)