From fde49c974a285fc67f5a6abb5576fa5ad97f99a5 Mon Sep 17 00:00:00 2001 From: Roman Zabaluev Date: Wed, 28 Apr 2021 23:07:27 +0300 Subject: [PATCH 1/2] Improve single bean dependency cycle failure analysis message --- ...eanCurrentlyInCreationFailureAnalyzer.java | 4 +- ...rrentlyInCreationFailureAnalyzerTests.java | 47 ++++++++++++++++--- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzer.java index 4e028fbbb017..76c77ba1d5a6 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzer.java @@ -74,7 +74,7 @@ private String buildMessage(DependencyCycle dependencyCycle) { for (int i = 0; i < beansInCycle.size(); i++) { BeanInCycle beanInCycle = beansInCycle.get(i); if (i == cycleStart) { - message.append(String.format("┌─────┐%n")); + message.append(String.format(beansInCycle.size() == 1 ? "┌──->──┐%n" : "┌─────┐%n")); } else if (i > 0) { String leftSide = (i < cycleStart) ? " " : "↑"; @@ -83,7 +83,7 @@ else if (i > 0) { String leftSide = (i < cycleStart) ? " " : "|"; message.append(String.format("%s %s%n", leftSide, beanInCycle)); } - message.append(String.format("└─────┘%n")); + message.append(String.format(beansInCycle.size() == 1 ? "└──<-──┘%n" : "└─────┘%n")); return message.toString(); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzerTests.java index 541b5c9a53ac..e907f2f3b198 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzerTests.java @@ -16,14 +16,7 @@ package org.springframework.boot.diagnostics.analyzer; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.StringReader; -import java.util.List; -import java.util.stream.Collectors; - import org.junit.jupiter.api.Test; - import org.springframework.beans.factory.BeanCurrentlyInCreationException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.diagnostics.FailureAnalysis; @@ -37,6 +30,12 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; +import java.util.List; +import java.util.stream.Collectors; + import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.fail; @@ -109,6 +108,19 @@ void cycleReferencedViaOtherBeans() throws IOException { assertThat(lines.get(11)).isEqualTo("└─────┘"); } + @Test + void testSelfReferenceCycle() throws IOException { + FailureAnalysis analysis = performAnalysis(SelfReferenceBeanConfiguration.class); + List lines = readDescriptionLines(analysis); + assertThat(lines).hasSize(5); + assertThat(lines.get(0)) + .isEqualTo("The dependencies of some of the beans in the application context form a cycle:"); + assertThat(lines.get(1)).isEqualTo(""); + assertThat(lines.get(2)).isEqualTo("┌──->──┐"); + assertThat(lines.get(3)).startsWith("| bean defined in " + SelfReferenceBeanConfiguration.class.getName()); + assertThat(lines.get(4)).isEqualTo("└──<-──┘"); + } + @Test void cycleWithAnUnknownStartIsNotAnalyzed() { assertThat(this.analyzer.analyze(new BeanCurrentlyInCreationException("test"))).isNull(); @@ -137,6 +149,7 @@ private Exception createFailure(Class configuration) { } @org.springframework.context.annotation.Configuration(proxyBeanMethods = false) + @SuppressWarnings("unused") static class CyclicBeanMethodsConfiguration { @Bean @@ -167,6 +180,7 @@ BeanOne one(BeanTwo two) { } @Configuration(proxyBeanMethods = false) + @SuppressWarnings("unused") static class CycleReferencedViaOtherBeansConfiguration { @Bean @@ -231,6 +245,7 @@ BeanTwo two() { @org.springframework.context.annotation.Configuration(proxyBeanMethods = false) static class BeanThreeConfiguration { + @SuppressWarnings("unused") @Bean BeanThree three(BeanOne one) { return new BeanThree(); @@ -240,6 +255,17 @@ BeanThree three(BeanOne one) { } + @Configuration(proxyBeanMethods = false) + static class SelfReferenceBeanConfiguration { + + @SuppressWarnings("unused") + @Bean + SelfReferenceBean bean(SelfReferenceBean bean) { + return new SelfReferenceBean(); + } + + } + static class RefererOne { @Autowired @@ -266,4 +292,11 @@ static class BeanThree { } + static class SelfReferenceBean { + + @Autowired + SelfReferenceBean bean; + + } + } From 8847d04f221593de128ecf9d65d38c39efaa2880 Mon Sep 17 00:00:00 2001 From: Roman Zabaluev Date: Thu, 29 Apr 2021 02:32:27 +0300 Subject: [PATCH 2/2] Fix formatting --- .../BeanCurrentlyInCreationFailureAnalyzer.java | 4 ++-- ...BeanCurrentlyInCreationFailureAnalyzerTests.java | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzer.java index 76c77ba1d5a6..a67ac2bb2dc8 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzer.java @@ -74,7 +74,7 @@ private String buildMessage(DependencyCycle dependencyCycle) { for (int i = 0; i < beansInCycle.size(); i++) { BeanInCycle beanInCycle = beansInCycle.get(i); if (i == cycleStart) { - message.append(String.format(beansInCycle.size() == 1 ? "┌──->──┐%n" : "┌─────┐%n")); + message.append(String.format((beansInCycle.size() == 1) ? "┌──->──┐%n" : "┌─────┐%n")); } else if (i > 0) { String leftSide = (i < cycleStart) ? " " : "↑"; @@ -83,7 +83,7 @@ else if (i > 0) { String leftSide = (i < cycleStart) ? " " : "|"; message.append(String.format("%s %s%n", leftSide, beanInCycle)); } - message.append(String.format(beansInCycle.size() == 1 ? "└──<-──┘%n" : "└─────┘%n")); + message.append(String.format((beansInCycle.size() == 1) ? "└──<-──┘%n" : "└─────┘%n")); return message.toString(); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzerTests.java index e907f2f3b198..4b6636b5d665 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzerTests.java @@ -16,7 +16,14 @@ package org.springframework.boot.diagnostics.analyzer; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; +import java.util.List; +import java.util.stream.Collectors; + import org.junit.jupiter.api.Test; + import org.springframework.beans.factory.BeanCurrentlyInCreationException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.diagnostics.FailureAnalysis; @@ -30,12 +37,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.StringReader; -import java.util.List; -import java.util.stream.Collectors; - import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.fail;