diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc index d4994b4e83..7e38a692af 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc @@ -48,6 +48,9 @@ The following example registers `ProjectConstraintViolationFailureAnalyzer`: com.example.ProjectConstraintViolationFailureAnalyzer ---- +NOTE: If you need access to the `BeanFactory` or the `Environment`, your `FailureAnalyzer` +can simply implement `BeanFactoryAware` or `EnvironmentAware` respectively. + [[howto-troubleshoot-auto-configuration]] diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java index f558fc2288..14343532f4 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -27,6 +27,7 @@ import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.boot.SpringBootExceptionReporter; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.EnvironmentAware; import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.util.Assert; @@ -96,6 +97,9 @@ final class FailureAnalyzers implements SpringBootExceptionReporter { if (analyzer instanceof BeanFactoryAware) { ((BeanFactoryAware) analyzer).setBeanFactory(context.getBeanFactory()); } + if (analyzer instanceof EnvironmentAware) { + ((EnvironmentAware) analyzer).setEnvironment(context.getEnvironment()); + } } @Override diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/FailureAnalyzersTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/FailureAnalyzersTests.java index 0aebdeed17..5cb72cb449 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/FailureAnalyzersTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/FailureAnalyzersTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -26,7 +26,9 @@ import org.junit.Test; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; +import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.core.env.Environment; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; @@ -37,14 +39,15 @@ import static org.mockito.Mockito.verify; * Tests for {@link FailureAnalyzers}. * * @author Andy Wilkinson + * @author Stephane Nicoll */ public class FailureAnalyzersTests { - private static BeanFactoryAwareFailureAnalyzer failureAnalyzer; + private static AwareFailureAnalyzer failureAnalyzer; @Before public void configureMock() { - failureAnalyzer = mock(BeanFactoryAwareFailureAnalyzer.class); + failureAnalyzer = mock(AwareFailureAnalyzer.class); } @Test @@ -61,6 +64,13 @@ public class FailureAnalyzersTests { verify(failureAnalyzer).setBeanFactory(any(BeanFactory.class)); } + @Test + public void environmentIsInjectedIntEnvironmentAwareFailureAnalyzers() { + RuntimeException failure = new RuntimeException(); + analyzeAndReport("basic.factories", failure); + verify(failureAnalyzer).setEnvironment(any(Environment.class)); + } + @Test public void analyzerThatFailsDuringInitializationDoesNotPreventOtherAnalyzersFromBeingCalled() { RuntimeException failure = new RuntimeException(); @@ -113,12 +123,17 @@ public class FailureAnalyzersTests { } - interface BeanFactoryAwareFailureAnalyzer extends BeanFactoryAware, FailureAnalyzer { + interface AwareFailureAnalyzer extends BeanFactoryAware, EnvironmentAware, FailureAnalyzer { } - static class StandardBeanFactoryAwareFailureAnalyzer extends BasicFailureAnalyzer - implements BeanFactoryAwareFailureAnalyzer { + static class StandardAwareFailureAnalyzer extends BasicFailureAnalyzer + implements AwareFailureAnalyzer { + + @Override + public void setEnvironment(Environment environment) { + failureAnalyzer.setEnvironment(environment); + } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { diff --git a/spring-boot-project/spring-boot/src/test/resources/failure-analyzers-tests/basic.factories b/spring-boot-project/spring-boot/src/test/resources/failure-analyzers-tests/basic.factories index d67b9cb731..30ca0a3a54 100644 --- a/spring-boot-project/spring-boot/src/test/resources/failure-analyzers-tests/basic.factories +++ b/spring-boot-project/spring-boot/src/test/resources/failure-analyzers-tests/basic.factories @@ -1,4 +1,4 @@ # Failure Analyzers org.springframework.boot.diagnostics.FailureAnalyzer=\ org.springframework.boot.diagnostics.FailureAnalyzersTests$BasicFailureAnalyzer,\ -org.springframework.boot.diagnostics.FailureAnalyzersTests$StandardBeanFactoryAwareFailureAnalyzer +org.springframework.boot.diagnostics.FailureAnalyzersTests$StandardAwareFailureAnalyzer