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 e5365fc06b9..aa978602306 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 @@ -19,10 +19,14 @@ package org.springframework.boot.diagnostics.analyzer; import java.util.ArrayList; import java.util.List; +import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCurrentlyInCreationException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.InjectionPoint; import org.springframework.beans.factory.UnsatisfiedDependencyException; +import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory; import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; import org.springframework.boot.diagnostics.FailureAnalysis; import org.springframework.util.StringUtils; @@ -33,7 +37,17 @@ import org.springframework.util.StringUtils; * * @author Andy Wilkinson */ -class BeanCurrentlyInCreationFailureAnalyzer extends AbstractFailureAnalyzer { +class BeanCurrentlyInCreationFailureAnalyzer extends AbstractFailureAnalyzer + implements BeanFactoryAware { + + private AbstractAutowireCapableBeanFactory beanFactory; + + @Override + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + if (beanFactory instanceof AbstractAutowireCapableBeanFactory) { + this.beanFactory = (AbstractAutowireCapableBeanFactory) beanFactory; + } + } @Override protected FailureAnalysis analyze(Throwable rootFailure, BeanCurrentlyInCreationException cause) { @@ -41,12 +55,18 @@ class BeanCurrentlyInCreationFailureAnalyzer extends AbstractFailureAnalyzer readDescriptionLines(FailureAnalysis analysis) throws IOException { try (BufferedReader reader = new BufferedReader(new StringReader(analysis.getDescription()))) { return reader.lines().collect(Collectors.toList()); @@ -138,13 +149,23 @@ class BeanCurrentlyInCreationFailureAnalyzerTests { } private FailureAnalysis performAnalysis(Class configuration) { - FailureAnalysis analysis = this.analyzer.analyze(createFailure(configuration)); + return performAnalysis(configuration, true); + } + + private FailureAnalysis performAnalysis(Class configuration, boolean allowCircularReferences) { + FailureAnalysis analysis = this.analyzer.analyze(createFailure(configuration, allowCircularReferences)); assertThat(analysis).isNotNull(); return analysis; } - private Exception createFailure(Class configuration) { - try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(configuration)) { + private Exception createFailure(Class configuration, boolean allowCircularReferences) { + try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { + context.register(configuration); + AbstractAutowireCapableBeanFactory beanFactory = (AbstractAutowireCapableBeanFactory) context + .getBeanFactory(); + this.analyzer.setBeanFactory(beanFactory); + beanFactory.setAllowCircularReferences(allowCircularReferences); + context.refresh(); fail("Expected failure did not occur"); return null; }