Skip to content

Commit 697b252

Browse files
Zhiyang.Wang1wilkinsona
Zhiyang.Wang1
authored andcommitted
Remove deprecated support for FailureAnalyzer setter injection
See gh-38322
1 parent 5675d79 commit 697b252

File tree

2 files changed

+4
-82
lines changed

2 files changed

+4
-82
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java

+2-36
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,13 @@
2222
import org.apache.commons.logging.LogFactory;
2323

2424
import org.springframework.beans.factory.BeanFactory;
25-
import org.springframework.beans.factory.BeanFactoryAware;
2625
import org.springframework.boot.SpringBootExceptionReporter;
2726
import org.springframework.context.ConfigurableApplicationContext;
28-
import org.springframework.context.EnvironmentAware;
2927
import org.springframework.core.env.Environment;
3028
import org.springframework.core.io.support.SpringFactoriesLoader;
3129
import org.springframework.core.io.support.SpringFactoriesLoader.ArgumentResolver;
3230
import org.springframework.core.io.support.SpringFactoriesLoader.FailureHandler;
3331
import org.springframework.core.log.LogMessage;
34-
import org.springframework.util.StringUtils;
3532

3633
/**
3734
* Utility to trigger {@link FailureAnalyzer} and {@link FailureAnalysisReporter}
@@ -61,39 +58,8 @@ public FailureAnalyzers(ConfigurableApplicationContext context) {
6158

6259
FailureAnalyzers(ConfigurableApplicationContext context, SpringFactoriesLoader springFactoriesLoader) {
6360
this.springFactoriesLoader = springFactoriesLoader;
64-
this.analyzers = loadFailureAnalyzers(context, this.springFactoriesLoader);
65-
}
66-
67-
private static List<FailureAnalyzer> loadFailureAnalyzers(ConfigurableApplicationContext context,
68-
SpringFactoriesLoader springFactoriesLoader) {
69-
List<FailureAnalyzer> analyzers = springFactoriesLoader.load(FailureAnalyzer.class,
70-
getArgumentResolver(context), FailureHandler.logging(logger));
71-
List<FailureAnalyzer> awareAnalyzers = analyzers.stream()
72-
.filter((analyzer) -> analyzer instanceof BeanFactoryAware || analyzer instanceof EnvironmentAware)
73-
.toList();
74-
if (!awareAnalyzers.isEmpty()) {
75-
String awareAnalyzerNames = StringUtils.collectionToCommaDelimitedString(
76-
awareAnalyzers.stream().map((analyzer) -> analyzer.getClass().getName()).toList());
77-
logger.warn(LogMessage.format(
78-
"FailureAnalyzers [%s] implement BeanFactoryAware or EnvironmentAware. "
79-
+ "Support for these interfaces on FailureAnalyzers is deprecated, "
80-
+ "and will be removed in a future release. "
81-
+ "Instead provide a constructor that accepts BeanFactory or Environment parameters.",
82-
awareAnalyzerNames));
83-
if (context == null) {
84-
logger.trace(LogMessage.format("Skipping [%s] due to missing context", awareAnalyzerNames));
85-
return analyzers.stream().filter((analyzer) -> !awareAnalyzers.contains(analyzer)).toList();
86-
}
87-
awareAnalyzers.forEach((analyzer) -> {
88-
if (analyzer instanceof BeanFactoryAware beanFactoryAware) {
89-
beanFactoryAware.setBeanFactory(context.getBeanFactory());
90-
}
91-
if (analyzer instanceof EnvironmentAware environmentAware) {
92-
environmentAware.setEnvironment(context.getEnvironment());
93-
}
94-
});
95-
}
96-
return analyzers;
61+
this.analyzers = springFactoriesLoader.load(FailureAnalyzer.class, getArgumentResolver(context),
62+
FailureHandler.logging(logger));
9763
}
9864

9965
private static ArgumentResolver getArgumentResolver(ConfigurableApplicationContext context) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/FailureAnalyzersTests.java

+2-46
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,13 @@
2121
import org.junit.jupiter.api.extension.ExtendWith;
2222

2323
import org.springframework.beans.factory.BeanFactory;
24-
import org.springframework.beans.factory.BeanFactoryAware;
2524
import org.springframework.boot.testsupport.system.CapturedOutput;
2625
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
27-
import org.springframework.context.EnvironmentAware;
2826
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2927
import org.springframework.core.env.Environment;
3028
import org.springframework.core.test.io.support.MockSpringFactoriesLoader;
3129

3230
import static org.assertj.core.api.Assertions.assertThat;
33-
import static org.mockito.ArgumentMatchers.same;
3431
import static org.mockito.BDDMockito.then;
3532
import static org.mockito.Mockito.mock;
3633
import static org.mockito.Mockito.times;
@@ -45,20 +42,13 @@
4542
@ExtendWith(OutputCaptureExtension.class)
4643
class FailureAnalyzersTests {
4744

48-
private static AwareFailureAnalyzer failureAnalyzer;
45+
private static FailureAnalyzer failureAnalyzer;
4946

5047
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
5148

5249
@BeforeEach
5350
void configureMock() {
54-
failureAnalyzer = mock(AwareFailureAnalyzer.class);
55-
}
56-
57-
@Test
58-
void analyzersAreLoadedAndCalled() {
59-
RuntimeException failure = new RuntimeException();
60-
analyzeAndReport(failure, BasicFailureAnalyzer.class, StandardAwareFailureAnalyzer.class);
61-
then(failureAnalyzer).should(times(2)).analyze(failure);
51+
failureAnalyzer = mock(FailureAnalyzer.class);
6252
}
6353

6454
@Test
@@ -77,22 +67,6 @@ void analyzerIsConstructedWithEnvironment(CapturedOutput output) {
7767
assertThat(output).doesNotContain("implement BeanFactoryAware or EnvironmentAware");
7868
}
7969

80-
@Test
81-
void beanFactoryIsInjectedIntoBeanFactoryAwareFailureAnalyzers(CapturedOutput output) {
82-
RuntimeException failure = new RuntimeException();
83-
analyzeAndReport(failure, BasicFailureAnalyzer.class, StandardAwareFailureAnalyzer.class);
84-
then(failureAnalyzer).should().setBeanFactory(same(this.context.getBeanFactory()));
85-
assertThat(output).contains("FailureAnalyzers [" + StandardAwareFailureAnalyzer.class.getName()
86-
+ "] implement BeanFactoryAware or EnvironmentAware.");
87-
}
88-
89-
@Test
90-
void environmentIsInjectedIntoEnvironmentAwareFailureAnalyzers() {
91-
RuntimeException failure = new RuntimeException();
92-
analyzeAndReport(failure, BasicFailureAnalyzer.class, StandardAwareFailureAnalyzer.class);
93-
then(failureAnalyzer).should().setEnvironment(same(this.context.getEnvironment()));
94-
}
95-
9670
@Test
9771
void analyzerThatFailsDuringInitializationDoesNotPreventOtherAnalyzersFromBeingCalled() {
9872
RuntimeException failure = new RuntimeException();
@@ -170,22 +144,4 @@ static class EnvironmentConstructorFailureAnalyzer extends BasicFailureAnalyzer
170144

171145
}
172146

173-
interface AwareFailureAnalyzer extends BeanFactoryAware, EnvironmentAware, FailureAnalyzer {
174-
175-
}
176-
177-
static class StandardAwareFailureAnalyzer extends BasicFailureAnalyzer implements AwareFailureAnalyzer {
178-
179-
@Override
180-
public void setEnvironment(Environment environment) {
181-
failureAnalyzer.setEnvironment(environment);
182-
}
183-
184-
@Override
185-
public void setBeanFactory(BeanFactory beanFactory) {
186-
failureAnalyzer.setBeanFactory(beanFactory);
187-
}
188-
189-
}
190-
191147
}

0 commit comments

Comments
 (0)