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
This commit is contained in:
commit
ef59326b44
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2024 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.
|
||||
|
@ -22,16 +22,13 @@ import org.apache.commons.logging.Log;
|
|||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
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.env.Environment;
|
||||
import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||
import org.springframework.core.io.support.SpringFactoriesLoader.ArgumentResolver;
|
||||
import org.springframework.core.io.support.SpringFactoriesLoader.FailureHandler;
|
||||
import org.springframework.core.log.LogMessage;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Utility to trigger {@link FailureAnalyzer} and {@link FailureAnalysisReporter}
|
||||
|
@ -66,34 +63,8 @@ final class FailureAnalyzers implements SpringBootExceptionReporter {
|
|||
|
||||
private static List<FailureAnalyzer> loadFailureAnalyzers(ConfigurableApplicationContext context,
|
||||
SpringFactoriesLoader springFactoriesLoader) {
|
||||
List<FailureAnalyzer> analyzers = springFactoriesLoader.load(FailureAnalyzer.class,
|
||||
getArgumentResolver(context), FailureHandler.logging(logger));
|
||||
List<FailureAnalyzer> awareAnalyzers = analyzers.stream()
|
||||
.filter((analyzer) -> analyzer instanceof BeanFactoryAware || analyzer instanceof EnvironmentAware)
|
||||
.toList();
|
||||
if (!awareAnalyzers.isEmpty()) {
|
||||
String awareAnalyzerNames = StringUtils.collectionToCommaDelimitedString(
|
||||
awareAnalyzers.stream().map((analyzer) -> analyzer.getClass().getName()).toList());
|
||||
logger.warn(LogMessage.format(
|
||||
"FailureAnalyzers [%s] implement BeanFactoryAware or EnvironmentAware. "
|
||||
+ "Support for these interfaces on FailureAnalyzers is deprecated, "
|
||||
+ "and will be removed in a future release. "
|
||||
+ "Instead provide a constructor that accepts BeanFactory or Environment parameters.",
|
||||
awareAnalyzerNames));
|
||||
if (context == null) {
|
||||
logger.trace(LogMessage.format("Skipping [%s] due to missing context", awareAnalyzerNames));
|
||||
return analyzers.stream().filter((analyzer) -> !awareAnalyzers.contains(analyzer)).toList();
|
||||
}
|
||||
awareAnalyzers.forEach((analyzer) -> {
|
||||
if (analyzer instanceof BeanFactoryAware beanFactoryAware) {
|
||||
beanFactoryAware.setBeanFactory(context.getBeanFactory());
|
||||
}
|
||||
if (analyzer instanceof EnvironmentAware environmentAware) {
|
||||
environmentAware.setEnvironment(context.getEnvironment());
|
||||
}
|
||||
});
|
||||
}
|
||||
return analyzers;
|
||||
return springFactoriesLoader.load(FailureAnalyzer.class, getArgumentResolver(context),
|
||||
FailureHandler.logging(logger));
|
||||
}
|
||||
|
||||
private static ArgumentResolver getArgumentResolver(ConfigurableApplicationContext context) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2022 the original author or authors.
|
||||
* Copyright 2012-2024 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.
|
||||
|
@ -21,16 +21,12 @@ import org.junit.jupiter.api.Test;
|
|||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.boot.testsupport.system.CapturedOutput;
|
||||
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.test.io.support.MockSpringFactoriesLoader;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.same;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
|
@ -45,52 +41,34 @@ import static org.mockito.Mockito.times;
|
|||
@ExtendWith(OutputCaptureExtension.class)
|
||||
class FailureAnalyzersTests {
|
||||
|
||||
private static AwareFailureAnalyzer failureAnalyzer;
|
||||
private static FailureAnalyzer failureAnalyzer;
|
||||
|
||||
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
|
||||
@BeforeEach
|
||||
void configureMock() {
|
||||
failureAnalyzer = mock(AwareFailureAnalyzer.class);
|
||||
failureAnalyzer = mock(FailureAnalyzer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void analyzersAreLoadedAndCalled() {
|
||||
RuntimeException failure = new RuntimeException();
|
||||
analyzeAndReport(failure, BasicFailureAnalyzer.class, StandardAwareFailureAnalyzer.class);
|
||||
analyzeAndReport(failure, BasicFailureAnalyzer.class, BasicFailureAnalyzer.class);
|
||||
then(failureAnalyzer).should(times(2)).analyze(failure);
|
||||
}
|
||||
|
||||
@Test
|
||||
void analyzerIsConstructedWithBeanFactory(CapturedOutput output) {
|
||||
void analyzerIsConstructedWithBeanFactory() {
|
||||
RuntimeException failure = new RuntimeException();
|
||||
analyzeAndReport(failure, BasicFailureAnalyzer.class, BeanFactoryConstructorFailureAnalyzer.class);
|
||||
then(failureAnalyzer).should(times(2)).analyze(failure);
|
||||
assertThat(output).doesNotContain("implement BeanFactoryAware or EnvironmentAware");
|
||||
}
|
||||
|
||||
@Test
|
||||
void analyzerIsConstructedWithEnvironment(CapturedOutput output) {
|
||||
void analyzerIsConstructedWithEnvironment() {
|
||||
RuntimeException failure = new RuntimeException();
|
||||
analyzeAndReport(failure, BasicFailureAnalyzer.class, EnvironmentConstructorFailureAnalyzer.class);
|
||||
then(failureAnalyzer).should(times(2)).analyze(failure);
|
||||
assertThat(output).doesNotContain("implement BeanFactoryAware or EnvironmentAware");
|
||||
}
|
||||
|
||||
@Test
|
||||
void beanFactoryIsInjectedIntoBeanFactoryAwareFailureAnalyzers(CapturedOutput output) {
|
||||
RuntimeException failure = new RuntimeException();
|
||||
analyzeAndReport(failure, BasicFailureAnalyzer.class, StandardAwareFailureAnalyzer.class);
|
||||
then(failureAnalyzer).should().setBeanFactory(same(this.context.getBeanFactory()));
|
||||
assertThat(output).contains("FailureAnalyzers [" + StandardAwareFailureAnalyzer.class.getName()
|
||||
+ "] implement BeanFactoryAware or EnvironmentAware.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void environmentIsInjectedIntoEnvironmentAwareFailureAnalyzers() {
|
||||
RuntimeException failure = new RuntimeException();
|
||||
analyzeAndReport(failure, BasicFailureAnalyzer.class, StandardAwareFailureAnalyzer.class);
|
||||
then(failureAnalyzer).should().setEnvironment(same(this.context.getEnvironment()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -170,22 +148,4 @@ class FailureAnalyzersTests {
|
|||
|
||||
}
|
||||
|
||||
interface AwareFailureAnalyzer extends BeanFactoryAware, EnvironmentAware, FailureAnalyzer {
|
||||
|
||||
}
|
||||
|
||||
static class StandardAwareFailureAnalyzer extends BasicFailureAnalyzer implements AwareFailureAnalyzer {
|
||||
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
failureAnalyzer.setEnvironment(environment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
failureAnalyzer.setBeanFactory(beanFactory);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue