Allow to inject the Environment in FailureAnalyzer
Closes gh-11569
This commit is contained in:
@@ -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]]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user