From 044c902a8cc734e8a77f339575c626f3d56fde3a Mon Sep 17 00:00:00 2001 From: Roman Zabaluev Date: Wed, 28 Apr 2021 23:07:27 +0300 Subject: [PATCH 1/2] Improve failure analysis with a single bean cycle See gh-26292 --- ...eanCurrentlyInCreationFailureAnalyzer.java | 4 +-- ...rrentlyInCreationFailureAnalyzerTests.java | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 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 4e028fbbb01..a67ac2bb2dc 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 @@ class BeanCurrentlyInCreationFailureAnalyzer extends AbstractFailureAnalyzer──┐%n" : "┌─────┐%n")); } else if (i > 0) { String leftSide = (i < cycleStart) ? " " : "↑"; @@ -83,7 +83,7 @@ class BeanCurrentlyInCreationFailureAnalyzer extends AbstractFailureAnalyzer 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 +150,7 @@ class BeanCurrentlyInCreationFailureAnalyzerTests { } @org.springframework.context.annotation.Configuration(proxyBeanMethods = false) + @SuppressWarnings("unused") static class CyclicBeanMethodsConfiguration { @Bean @@ -167,6 +181,7 @@ class BeanCurrentlyInCreationFailureAnalyzerTests { } @Configuration(proxyBeanMethods = false) + @SuppressWarnings("unused") static class CycleReferencedViaOtherBeansConfiguration { @Bean @@ -231,6 +246,7 @@ class BeanCurrentlyInCreationFailureAnalyzerTests { @org.springframework.context.annotation.Configuration(proxyBeanMethods = false) static class BeanThreeConfiguration { + @SuppressWarnings("unused") @Bean BeanThree three(BeanOne one) { return new BeanThree(); @@ -240,6 +256,17 @@ class BeanCurrentlyInCreationFailureAnalyzerTests { } + @Configuration(proxyBeanMethods = false) + static class SelfReferenceBeanConfiguration { + + @SuppressWarnings("unused") + @Bean + SelfReferenceBean bean(SelfReferenceBean bean) { + return new SelfReferenceBean(); + } + + } + static class RefererOne { @Autowired @@ -266,4 +293,11 @@ class BeanCurrentlyInCreationFailureAnalyzerTests { } + static class SelfReferenceBean { + + @Autowired + SelfReferenceBean bean; + + } + } From a6e59b357f51e5b3fbd2dcbffa68808f7dd54266 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 30 Apr 2021 13:29:23 +0200 Subject: [PATCH 2/2] Polish "Improve failure analysis with a single bean cycle" See gh-26292 --- .../analyzer/BeanCurrentlyInCreationFailureAnalyzer.java | 7 ++++--- .../BeanCurrentlyInCreationFailureAnalyzerTests.java | 6 +----- 2 files changed, 5 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 a67ac2bb2dc..a9c58a50397 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -70,11 +70,12 @@ class BeanCurrentlyInCreationFailureAnalyzer extends AbstractFailureAnalyzer beansInCycle = dependencyCycle.getBeansInCycle(); + boolean singleBean = beansInCycle.size() == 1; int cycleStart = dependencyCycle.getCycleStart(); 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(singleBean ? "┌──->──┐%n" : "┌─────┐%n")); } else if (i > 0) { String leftSide = (i < cycleStart) ? " " : "↑"; @@ -83,7 +84,7 @@ class BeanCurrentlyInCreationFailureAnalyzer extends AbstractFailureAnalyzer