Merge branch '2.7.x'

pull/30627/head
Scott Frederick 3 years ago
commit 0dd9493dc1

@ -26,9 +26,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;
@ -57,18 +55,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());

@ -46,10 +46,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() {
@ -208,11 +212,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) {

@ -90,9 +90,9 @@ final class FailureAnalyzers implements SpringBootExceptionReporter {
String awareAnalyzerNames = StringUtils.collectionToCommaDelimitedString(awareAnalyzers.stream()
.map((analyzer) -> analyzer.getClass().getName()).collect(Collectors.toList()));
logger.warn(LogMessage.format(
"FailureAnalyzers [%s] implement BeanFactoryAware or EnvironmentAware."
"FailureAnalyzers [%s] implement BeanFactoryAware or EnvironmentAware. "
+ "Support for these interfaces on FailureAnalyzers is deprecated, "
+ "and will be removed in a future release."
+ "and will be removed in a future release. "
+ "Instead provide a constructor that accepts BeanFactory or Environment parameters.",
awareAnalyzerNames));
if (context == null) {

@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 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.
@ -19,11 +19,9 @@ package org.springframework.boot.diagnostics.analyzer;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanCurrentlyInCreationException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InjectionPoint;
import org.springframework.beans.factory.UnsatisfiedDependencyException;
import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
@ -36,17 +34,19 @@ import org.springframework.util.StringUtils;
* {@link BeanCurrentlyInCreationException}.
*
* @author Andy Wilkinson
* @author Scott Frederick
*/
class BeanCurrentlyInCreationFailureAnalyzer extends AbstractFailureAnalyzer<BeanCurrentlyInCreationException>
implements BeanFactoryAware {
class BeanCurrentlyInCreationFailureAnalyzer extends AbstractFailureAnalyzer<BeanCurrentlyInCreationException> {
private AbstractAutowireCapableBeanFactory beanFactory;
private final AbstractAutowireCapableBeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
if (beanFactory instanceof AbstractAutowireCapableBeanFactory) {
BeanCurrentlyInCreationFailureAnalyzer(BeanFactory beanFactory) {
if (beanFactory != null && beanFactory instanceof AbstractAutowireCapableBeanFactory) {
this.beanFactory = (AbstractAutowireCapableBeanFactory) beanFactory;
}
else {
this.beanFactory = null;
}
}
@Override

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 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,7 +27,6 @@ import org.springframework.boot.diagnostics.FailureAnalysis;
import org.springframework.boot.diagnostics.FailureAnalyzer;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.origin.OriginLookup;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
@ -38,14 +37,14 @@ import org.springframework.util.StringUtils;
* {@link InvalidConfigurationPropertyValueException}.
*
* @author Stephane Nicoll
* @author Scott Frederick
*/
class InvalidConfigurationPropertyValueFailureAnalyzer
extends AbstractFailureAnalyzer<InvalidConfigurationPropertyValueException> implements EnvironmentAware {
extends AbstractFailureAnalyzer<InvalidConfigurationPropertyValueException> {
private ConfigurableEnvironment environment;
private final ConfigurableEnvironment environment;
@Override
public void setEnvironment(Environment environment) {
InvalidConfigurationPropertyValueFailureAnalyzer(Environment environment) {
this.environment = (ConfigurableEnvironment) environment;
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 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.
@ -32,7 +32,6 @@ import org.springframework.boot.diagnostics.FailureAnalysis;
import org.springframework.boot.diagnostics.FailureAnalyzer;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.origin.OriginLookup;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
@ -42,14 +41,14 @@ import org.springframework.core.env.PropertySource;
* {@link MutuallyExclusiveConfigurationPropertiesException}.
*
* @author Andy Wilkinson
* @author Scott Frederick
*/
class MutuallyExclusiveConfigurationPropertiesFailureAnalyzer
extends AbstractFailureAnalyzer<MutuallyExclusiveConfigurationPropertiesException> implements EnvironmentAware {
extends AbstractFailureAnalyzer<MutuallyExclusiveConfigurationPropertiesException> {
private ConfigurableEnvironment environment;
private final ConfigurableEnvironment environment;
@Override
public void setEnvironment(Environment environment) {
MutuallyExclusiveConfigurationPropertiesFailureAnalyzer(Environment environment) {
this.environment = (ConfigurableEnvironment) environment;
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 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.
@ -16,9 +16,7 @@
package org.springframework.boot.diagnostics.analyzer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
@ -32,14 +30,13 @@ import org.springframework.util.StringUtils;
* by a {@link NoUniqueBeanDefinitionException}.
*
* @author Andy Wilkinson
* @author Scott Frederick
*/
class NoUniqueBeanDefinitionFailureAnalyzer extends AbstractInjectionFailureAnalyzer<NoUniqueBeanDefinitionException>
implements BeanFactoryAware {
class NoUniqueBeanDefinitionFailureAnalyzer extends AbstractInjectionFailureAnalyzer<NoUniqueBeanDefinitionException> {
private ConfigurableBeanFactory beanFactory;
private final ConfigurableBeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
NoUniqueBeanDefinitionFailureAnalyzer(BeanFactory beanFactory) {
Assert.isInstanceOf(ConfigurableBeanFactory.class, beanFactory);
this.beanFactory = (ConfigurableBeanFactory) beanFactory;
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 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.
@ -43,10 +43,14 @@ import static org.junit.jupiter.api.Assertions.fail;
* Tests for {@link BeanCurrentlyInCreationFailureAnalyzer}.
*
* @author Andy Wilkinson
* @author Scott Frederick
*/
class BeanCurrentlyInCreationFailureAnalyzerTests {
private final BeanCurrentlyInCreationFailureAnalyzer analyzer = new BeanCurrentlyInCreationFailureAnalyzer();
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
private final BeanCurrentlyInCreationFailureAnalyzer analyzer = new BeanCurrentlyInCreationFailureAnalyzer(
this.context.getBeanFactory());
@Test
void cyclicBeanMethods() throws IOException {
@ -131,13 +135,13 @@ class BeanCurrentlyInCreationFailureAnalyzerTests {
}
@Test
void cycleWithCircularReferencesAllowed() throws IOException {
void cycleWithCircularReferencesAllowed() {
FailureAnalysis analysis = performAnalysis(CyclicBeanMethodsConfiguration.class, true);
assertThat(analysis.getAction()).contains("Despite circular references being allowed");
}
@Test
void cycleWithCircularReferencesProhibited() throws IOException {
void cycleWithCircularReferencesProhibited() {
FailureAnalysis analysis = performAnalysis(CyclicBeanMethodsConfiguration.class, false);
assertThat(analysis.getAction()).contains("As a last resort");
}
@ -159,13 +163,12 @@ class BeanCurrentlyInCreationFailureAnalyzerTests {
}
private Exception createFailure(Class<?> configuration, boolean allowCircularReferences) {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
context.register(configuration);
AbstractAutowireCapableBeanFactory beanFactory = (AbstractAutowireCapableBeanFactory) context
try {
this.context.register(configuration);
AbstractAutowireCapableBeanFactory beanFactory = (AbstractAutowireCapableBeanFactory) this.context
.getBeanFactory();
this.analyzer.setBeanFactory(beanFactory);
beanFactory.setAllowCircularReferences(allowCircularReferences);
context.refresh();
this.context.refresh();
fail("Expected failure did not occur");
return null;
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 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.
@ -34,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link InvalidConfigurationPropertyValueFailureAnalyzer}.
*
* @author Stephane Nicoll
* @author Scott Frederick
*/
class InvalidConfigurationPropertyValueFailureAnalyzerTests {
@ -43,7 +44,7 @@ class InvalidConfigurationPropertyValueFailureAnalyzerTests {
void analysisWithNullEnvironment() {
InvalidConfigurationPropertyValueException failure = new InvalidConfigurationPropertyValueException(
"test.property", "invalid", "This is not valid.");
FailureAnalysis analysis = new InvalidConfigurationPropertyValueFailureAnalyzer().analyze(failure);
FailureAnalysis analysis = new InvalidConfigurationPropertyValueFailureAnalyzer(null).analyze(failure);
assertThat(analysis).isNull();
}
@ -106,8 +107,8 @@ class InvalidConfigurationPropertyValueFailureAnalyzerTests {
}
private FailureAnalysis performAnalysis(InvalidConfigurationPropertyValueException failure) {
InvalidConfigurationPropertyValueFailureAnalyzer analyzer = new InvalidConfigurationPropertyValueFailureAnalyzer();
analyzer.setEnvironment(this.environment);
InvalidConfigurationPropertyValueFailureAnalyzer analyzer = new InvalidConfigurationPropertyValueFailureAnalyzer(
this.environment);
return analyzer.analyze(failure);
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 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.
@ -39,6 +39,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link MutuallyExclusiveConfigurationPropertiesFailureAnalyzer}.
*
* @author Andy Wilkinson
* @author Scott Frederick
*/
class MutuallyExclusiveConfigurationPropertiesFailureAnalyzerTests {
@ -49,7 +50,7 @@ class MutuallyExclusiveConfigurationPropertiesFailureAnalyzerTests {
MutuallyExclusiveConfigurationPropertiesException failure = new MutuallyExclusiveConfigurationPropertiesException(
new HashSet<>(Arrays.asList("com.example.a", "com.example.b")),
new HashSet<>(Arrays.asList("com.example.a", "com.example.b")));
FailureAnalysis failureAnalysis = new MutuallyExclusiveConfigurationPropertiesFailureAnalyzer()
FailureAnalysis failureAnalysis = new MutuallyExclusiveConfigurationPropertiesFailureAnalyzer(null)
.analyze(failure);
assertThat(failureAnalysis).isNull();
}
@ -112,8 +113,8 @@ class MutuallyExclusiveConfigurationPropertiesFailureAnalyzerTests {
}
private FailureAnalysis performAnalysis(MutuallyExclusiveConfigurationPropertiesException failure) {
MutuallyExclusiveConfigurationPropertiesFailureAnalyzer analyzer = new MutuallyExclusiveConfigurationPropertiesFailureAnalyzer();
analyzer.setEnvironment(this.environment);
MutuallyExclusiveConfigurationPropertiesFailureAnalyzer analyzer = new MutuallyExclusiveConfigurationPropertiesFailureAnalyzer(
this.environment);
return analyzer.analyze(failure);
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 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.
@ -36,10 +36,14 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link NoUniqueBeanDefinitionFailureAnalyzer}.
*
* @author Andy Wilkinson
* @author Scott Frederick
*/
class NoUniqueBeanDefinitionFailureAnalyzerTests {
private final NoUniqueBeanDefinitionFailureAnalyzer analyzer = new NoUniqueBeanDefinitionFailureAnalyzer();
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
private final NoUniqueBeanDefinitionFailureAnalyzer analyzer = new NoUniqueBeanDefinitionFailureAnalyzer(
this.context.getBeanFactory());
@Test
void failureAnalysisForFieldConsumer() {
@ -90,18 +94,15 @@ class NoUniqueBeanDefinitionFailureAnalyzerTests {
}
private BeanCreationException createFailure(Class<?> consumer) {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
context.register(DuplicateBeansProducer.class, consumer);
context.setParent(new AnnotationConfigApplicationContext(ParentProducer.class));
try {
context.refresh();
}
catch (BeanCreationException ex) {
this.analyzer.setBeanFactory(context.getBeanFactory());
return ex;
}
return null;
this.context.register(DuplicateBeansProducer.class, consumer);
this.context.setParent(new AnnotationConfigApplicationContext(ParentProducer.class));
try {
this.context.refresh();
}
catch (BeanCreationException ex) {
return ex;
}
return null;
}
private FailureAnalysis analyzeFailure(BeanCreationException failure) {

Loading…
Cancel
Save