Skip to content

Commit 044c902

Browse files
Haaroleansnicoll
authored andcommitted
Improve failure analysis with a single bean cycle
See gh-26292
1 parent 3f528bb commit 044c902

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private String buildMessage(DependencyCycle dependencyCycle) {
7474
for (int i = 0; i < beansInCycle.size(); i++) {
7575
BeanInCycle beanInCycle = beansInCycle.get(i);
7676
if (i == cycleStart) {
77-
message.append(String.format("┌─────┐%n"));
77+
message.append(String.format((beansInCycle.size() == 1) ? "┌──->──┐%n" : "┌─────┐%n"));
7878
}
7979
else if (i > 0) {
8080
String leftSide = (i < cycleStart) ? " " : "↑";
@@ -83,7 +83,7 @@ else if (i > 0) {
8383
String leftSide = (i < cycleStart) ? " " : "|";
8484
message.append(String.format("%s %s%n", leftSide, beanInCycle));
8585
}
86-
message.append(String.format("└─────┘%n"));
86+
message.append(String.format((beansInCycle.size() == 1) ? "└──<-──┘%n" : "└─────┘%n"));
8787
return message.toString();
8888
}
8989

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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();
@@ -137,6 +150,7 @@ private Exception createFailure(Class<?> configuration) {
137150
}
138151

139152
@org.springframework.context.annotation.Configuration(proxyBeanMethods = false)
153+
@SuppressWarnings("unused")
140154
static class CyclicBeanMethodsConfiguration {
141155

142156
@Bean
@@ -167,6 +181,7 @@ BeanOne one(BeanTwo two) {
167181
}
168182

169183
@Configuration(proxyBeanMethods = false)
184+
@SuppressWarnings("unused")
170185
static class CycleReferencedViaOtherBeansConfiguration {
171186

172187
@Bean
@@ -231,6 +246,7 @@ BeanTwo two() {
231246
@org.springframework.context.annotation.Configuration(proxyBeanMethods = false)
232247
static class BeanThreeConfiguration {
233248

249+
@SuppressWarnings("unused")
234250
@Bean
235251
BeanThree three(BeanOne one) {
236252
return new BeanThree();
@@ -240,6 +256,17 @@ BeanThree three(BeanOne one) {
240256

241257
}
242258

259+
@Configuration(proxyBeanMethods = false)
260+
static class SelfReferenceBeanConfiguration {
261+
262+
@SuppressWarnings("unused")
263+
@Bean
264+
SelfReferenceBean bean(SelfReferenceBean bean) {
265+
return new SelfReferenceBean();
266+
}
267+
268+
}
269+
243270
static class RefererOne {
244271

245272
@Autowired
@@ -266,4 +293,11 @@ static class BeanThree {
266293

267294
}
268295

296+
static class SelfReferenceBean {
297+
298+
@Autowired
299+
SelfReferenceBean bean;
300+
301+
}
302+
269303
}

0 commit comments

Comments
 (0)