Commit 6daad1f5 authored by Stephane Nicoll's avatar Stephane Nicoll

Allow to inject the Environment in FailureAnalyzer

Closes gh-11569
parent 6a0dbc5c
...@@ -48,6 +48,9 @@ The following example registers `ProjectConstraintViolationFailureAnalyzer`: ...@@ -48,6 +48,9 @@ The following example registers `ProjectConstraintViolationFailureAnalyzer`:
com.example.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]] [[howto-troubleshoot-auto-configuration]]
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -27,6 +27,7 @@ import org.springframework.beans.factory.BeanFactory; ...@@ -27,6 +27,7 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.boot.SpringBootExceptionReporter; import org.springframework.boot.SpringBootExceptionReporter;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.util.Assert; import org.springframework.util.Assert;
...@@ -96,6 +97,9 @@ final class FailureAnalyzers implements SpringBootExceptionReporter { ...@@ -96,6 +97,9 @@ final class FailureAnalyzers implements SpringBootExceptionReporter {
if (analyzer instanceof BeanFactoryAware) { if (analyzer instanceof BeanFactoryAware) {
((BeanFactoryAware) analyzer).setBeanFactory(context.getBeanFactory()); ((BeanFactoryAware) analyzer).setBeanFactory(context.getBeanFactory());
} }
if (analyzer instanceof EnvironmentAware) {
((EnvironmentAware) analyzer).setEnvironment(context.getEnvironment());
}
} }
@Override @Override
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -26,7 +26,9 @@ import org.junit.Test; ...@@ -26,7 +26,9 @@ import org.junit.Test;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.Environment;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
...@@ -37,14 +39,15 @@ import static org.mockito.Mockito.verify; ...@@ -37,14 +39,15 @@ import static org.mockito.Mockito.verify;
* Tests for {@link FailureAnalyzers}. * Tests for {@link FailureAnalyzers}.
* *
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Stephane Nicoll
*/ */
public class FailureAnalyzersTests { public class FailureAnalyzersTests {
private static BeanFactoryAwareFailureAnalyzer failureAnalyzer; private static AwareFailureAnalyzer failureAnalyzer;
@Before @Before
public void configureMock() { public void configureMock() {
failureAnalyzer = mock(BeanFactoryAwareFailureAnalyzer.class); failureAnalyzer = mock(AwareFailureAnalyzer.class);
} }
@Test @Test
...@@ -61,6 +64,13 @@ public class FailureAnalyzersTests { ...@@ -61,6 +64,13 @@ public class FailureAnalyzersTests {
verify(failureAnalyzer).setBeanFactory(any(BeanFactory.class)); 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 @Test
public void analyzerThatFailsDuringInitializationDoesNotPreventOtherAnalyzersFromBeingCalled() { public void analyzerThatFailsDuringInitializationDoesNotPreventOtherAnalyzersFromBeingCalled() {
RuntimeException failure = new RuntimeException(); RuntimeException failure = new RuntimeException();
...@@ -113,12 +123,17 @@ public class FailureAnalyzersTests { ...@@ -113,12 +123,17 @@ public class FailureAnalyzersTests {
} }
interface BeanFactoryAwareFailureAnalyzer extends BeanFactoryAware, FailureAnalyzer { interface AwareFailureAnalyzer extends BeanFactoryAware, EnvironmentAware, FailureAnalyzer {
} }
static class StandardBeanFactoryAwareFailureAnalyzer extends BasicFailureAnalyzer static class StandardAwareFailureAnalyzer extends BasicFailureAnalyzer
implements BeanFactoryAwareFailureAnalyzer { implements AwareFailureAnalyzer {
@Override
public void setEnvironment(Environment environment) {
failureAnalyzer.setEnvironment(environment);
}
@Override @Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
......
# Failure Analyzers # Failure Analyzers
org.springframework.boot.diagnostics.FailureAnalyzer=\ org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.diagnostics.FailureAnalyzersTests$BasicFailureAnalyzer,\ org.springframework.boot.diagnostics.FailureAnalyzersTests$BasicFailureAnalyzer,\
org.springframework.boot.diagnostics.FailureAnalyzersTests$StandardBeanFactoryAwareFailureAnalyzer org.springframework.boot.diagnostics.FailureAnalyzersTests$StandardAwareFailureAnalyzer
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment