diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index 46b5ed88e3ef..3accbafa2265 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -25,7 +25,6 @@ org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition org.springframework.boot.diagnostics.FailureAnalyzer=\ org.springframework.boot.autoconfigure.data.redis.RedisUrlSyntaxFailureAnalyzer,\ org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzer,\ -org.springframework.boot.autoconfigure.flyway.FlywayMigrationScriptMissingFailureAnalyzer,\ org.springframework.boot.autoconfigure.jdbc.DataSourceBeanCreationFailureAnalyzer,\ org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer,\ org.springframework.boot.autoconfigure.jooq.NoDslContextBeanFailureAnalyzer,\ diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto/application.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto/application.adoc index 26b1da2c2e23..a45d342484da 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto/application.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto/application.adoc @@ -23,7 +23,7 @@ The following example registers `ProjectConstraintViolationFailureAnalyzer`: com.example.ProjectConstraintViolationFailureAnalyzer ---- -NOTE: If you need access to the `BeanFactory` or the `Environment`, your `FailureAnalyzer` can implement `BeanFactoryAware` or `EnvironmentAware` respectively. +NOTE: If you need access to the `BeanFactory` or the `Environment`, your `FailureAnalyzer` can accept a `BeanFactory` and/or `Environment` as constructor parameters. diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java index 7a905a8fcbe8..cf9bb3f199c6 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java @@ -22,16 +22,13 @@ 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} @@ -61,39 +58,8 @@ public FailureAnalyzers(ConfigurableApplicationContext context) { FailureAnalyzers(ConfigurableApplicationContext context, SpringFactoriesLoader springFactoriesLoader) { this.springFactoriesLoader = springFactoriesLoader; - this.analyzers = loadFailureAnalyzers(context, this.springFactoriesLoader); - } - - private static List loadFailureAnalyzers(ConfigurableApplicationContext context, - SpringFactoriesLoader springFactoriesLoader) { - List analyzers = springFactoriesLoader.load(FailureAnalyzer.class, - getArgumentResolver(context), FailureHandler.logging(logger)); - List 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; + this.analyzers = springFactoriesLoader.load(FailureAnalyzer.class, getArgumentResolver(context), + FailureHandler.logging(logger)); } private static ArgumentResolver getArgumentResolver(ConfigurableApplicationContext context) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/FailureAnalyzersTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/FailureAnalyzersTests.java index d1fdcd7747ed..42637ed5974b 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/FailureAnalyzersTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/FailureAnalyzersTests.java @@ -21,16 +21,13 @@ 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,20 +42,13 @@ @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); - } - - @Test - void analyzersAreLoadedAndCalled() { - RuntimeException failure = new RuntimeException(); - analyzeAndReport(failure, BasicFailureAnalyzer.class, StandardAwareFailureAnalyzer.class); - then(failureAnalyzer).should(times(2)).analyze(failure); + failureAnalyzer = mock(FailureAnalyzer.class); } @Test @@ -77,22 +67,6 @@ void analyzerIsConstructedWithEnvironment(CapturedOutput output) { 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 void analyzerThatFailsDuringInitializationDoesNotPreventOtherAnalyzersFromBeingCalled() { RuntimeException failure = new RuntimeException(); @@ -170,22 +144,4 @@ static class EnvironmentConstructorFailureAnalyzer extends BasicFailureAnalyzer } - 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); - } - - } - }