Migrate FailureAnalyzers to constructor injection
All `FailureAnalyzer` implementations should use constructor injection for `BeanFactory` and `Environment` instead of implementing `BeanFactoryAware` or `EnvironmentAware` interfaces. Fixes gh-30585
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2022 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.
|
||||
@@ -27,9 +27,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.beans.factory.InjectionPoint;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
@@ -63,18 +61,17 @@ import org.springframework.util.ClassUtils;
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Phillip Webb
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
class NoSuchBeanDefinitionFailureAnalyzer extends AbstractInjectionFailureAnalyzer<NoSuchBeanDefinitionException>
|
||||
implements BeanFactoryAware {
|
||||
class NoSuchBeanDefinitionFailureAnalyzer extends AbstractInjectionFailureAnalyzer<NoSuchBeanDefinitionException> {
|
||||
|
||||
private ConfigurableListableBeanFactory beanFactory;
|
||||
private final ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
private MetadataReaderFactory metadataReaderFactory;
|
||||
private final MetadataReaderFactory metadataReaderFactory;
|
||||
|
||||
private ConditionEvaluationReport report;
|
||||
private final ConditionEvaluationReport report;
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
NoSuchBeanDefinitionFailureAnalyzer(BeanFactory beanFactory) {
|
||||
Assert.isInstanceOf(ConfigurableListableBeanFactory.class, beanFactory);
|
||||
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
|
||||
this.metadataReaderFactory = new CachingMetadataReaderFactory(this.beanFactory.getBeanClassLoader());
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2022 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.
|
||||
@@ -49,10 +49,14 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* Tests for {@link NoSuchBeanDefinitionFailureAnalyzer}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
class NoSuchBeanDefinitionFailureAnalyzerTests {
|
||||
|
||||
private final NoSuchBeanDefinitionFailureAnalyzer analyzer = new NoSuchBeanDefinitionFailureAnalyzer();
|
||||
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
|
||||
private final NoSuchBeanDefinitionFailureAnalyzer analyzer = new NoSuchBeanDefinitionFailureAnalyzer(
|
||||
this.context.getBeanFactory());
|
||||
|
||||
@Test
|
||||
void failureAnalysisForMultipleBeans() {
|
||||
@@ -227,11 +231,10 @@ class NoSuchBeanDefinitionFailureAnalyzerTests {
|
||||
}
|
||||
|
||||
private FatalBeanException createFailure(Class<?> config, String... environment) {
|
||||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
|
||||
this.analyzer.setBeanFactory(context.getBeanFactory());
|
||||
TestPropertyValues.of(environment).applyTo(context);
|
||||
context.register(config);
|
||||
context.refresh();
|
||||
try {
|
||||
TestPropertyValues.of(environment).applyTo(this.context);
|
||||
this.context.register(config);
|
||||
this.context.refresh();
|
||||
return null;
|
||||
}
|
||||
catch (FatalBeanException ex) {
|
||||
|
||||
@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Copyright 2012-2022 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
*
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.diagnostics.analyzer
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
@@ -18,11 +34,12 @@ import org.springframework.context.annotation.Configuration
|
||||
* on the classpath.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
@ClassPathExclusions("kotlin-reflect*.jar")
|
||||
class KotlinNoSuchBeanFailureAnalyzerNoKotlinReflectTests {
|
||||
|
||||
private val analyzer = NoSuchBeanDefinitionFailureAnalyzer()
|
||||
private val context = AnnotationConfigApplicationContext()
|
||||
|
||||
@Test
|
||||
fun failureAnalysisForConfigurationPropertiesThatMaybeShouldHaveBeenConstructorBound() {
|
||||
@@ -37,7 +54,6 @@ class KotlinNoSuchBeanFailureAnalyzerNoKotlinReflectTests {
|
||||
private fun createFailure(config: Class<*>, vararg environment: String): FatalBeanException? {
|
||||
try {
|
||||
AnnotationConfigApplicationContext().use { context ->
|
||||
this.analyzer.setBeanFactory(context.beanFactory)
|
||||
TestPropertyValues.of(*environment).applyTo(context)
|
||||
context.register(config)
|
||||
context.refresh()
|
||||
@@ -50,7 +66,8 @@ class KotlinNoSuchBeanFailureAnalyzerNoKotlinReflectTests {
|
||||
}
|
||||
|
||||
private fun analyzeFailure(failure: Exception?): FailureAnalysis? {
|
||||
val analysis = this.analyzer.analyze(failure)
|
||||
val analyzer = NoSuchBeanDefinitionFailureAnalyzer(this.context.beanFactory)
|
||||
val analysis = analyzer.analyze(failure)
|
||||
if (analysis != null) {
|
||||
LoggingFailureAnalysisReporter().report(analysis)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user