Skip to content

Commit ef59326

Browse files
committed
Merge pull request #38322 from Wzy19930507
* gh-38322: Polish "Remove deprecated support for FailureAnalyzer setter injection" Remove deprecated support for FailureAnalyzer setter injection Closes gh-38322
2 parents 5675d79 + 9b8c45c commit ef59326

File tree

2 files changed

+9
-78
lines changed

2 files changed

+9
-78
lines changed

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

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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}
@@ -66,34 +63,8 @@ public FailureAnalyzers(ConfigurableApplicationContext context) {
6663

6764
private static List<FailureAnalyzer> loadFailureAnalyzers(ConfigurableApplicationContext context,
6865
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;
66+
return springFactoriesLoader.load(FailureAnalyzer.class, getArgumentResolver(context),
67+
FailureHandler.logging(logger));
9768
}
9869

9970
private static ArgumentResolver getArgumentResolver(ConfigurableApplicationContext context) {

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

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,16 +21,12 @@
2121
import org.junit.jupiter.api.extension.ExtendWith;
2222

2323
import org.springframework.beans.factory.BeanFactory;
24-
import org.springframework.beans.factory.BeanFactoryAware;
25-
import org.springframework.boot.testsupport.system.CapturedOutput;
2624
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
27-
import org.springframework.context.EnvironmentAware;
2825
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2926
import org.springframework.core.env.Environment;
3027
import org.springframework.core.test.io.support.MockSpringFactoriesLoader;
3128

3229
import static org.assertj.core.api.Assertions.assertThat;
33-
import static org.mockito.ArgumentMatchers.same;
3430
import static org.mockito.BDDMockito.then;
3531
import static org.mockito.Mockito.mock;
3632
import static org.mockito.Mockito.times;
@@ -45,52 +41,34 @@
4541
@ExtendWith(OutputCaptureExtension.class)
4642
class FailureAnalyzersTests {
4743

48-
private static AwareFailureAnalyzer failureAnalyzer;
44+
private static FailureAnalyzer failureAnalyzer;
4945

5046
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
5147

5248
@BeforeEach
5349
void configureMock() {
54-
failureAnalyzer = mock(AwareFailureAnalyzer.class);
50+
failureAnalyzer = mock(FailureAnalyzer.class);
5551
}
5652

5753
@Test
5854
void analyzersAreLoadedAndCalled() {
5955
RuntimeException failure = new RuntimeException();
60-
analyzeAndReport(failure, BasicFailureAnalyzer.class, StandardAwareFailureAnalyzer.class);
56+
analyzeAndReport(failure, BasicFailureAnalyzer.class, BasicFailureAnalyzer.class);
6157
then(failureAnalyzer).should(times(2)).analyze(failure);
6258
}
6359

6460
@Test
65-
void analyzerIsConstructedWithBeanFactory(CapturedOutput output) {
61+
void analyzerIsConstructedWithBeanFactory() {
6662
RuntimeException failure = new RuntimeException();
6763
analyzeAndReport(failure, BasicFailureAnalyzer.class, BeanFactoryConstructorFailureAnalyzer.class);
6864
then(failureAnalyzer).should(times(2)).analyze(failure);
69-
assertThat(output).doesNotContain("implement BeanFactoryAware or EnvironmentAware");
7065
}
7166

7267
@Test
73-
void analyzerIsConstructedWithEnvironment(CapturedOutput output) {
68+
void analyzerIsConstructedWithEnvironment() {
7469
RuntimeException failure = new RuntimeException();
7570
analyzeAndReport(failure, BasicFailureAnalyzer.class, EnvironmentConstructorFailureAnalyzer.class);
7671
then(failureAnalyzer).should(times(2)).analyze(failure);
77-
assertThat(output).doesNotContain("implement BeanFactoryAware or EnvironmentAware");
78-
}
79-
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()));
9472
}
9573

9674
@Test
@@ -170,22 +148,4 @@ static class EnvironmentConstructorFailureAnalyzer extends BasicFailureAnalyzer
170148

171149
}
172150

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-
191151
}

0 commit comments

Comments
 (0)