Consider possibility of missing @ConstructorBinding in failure analysis

Previously, when a NoSuchBeanDefinitionException was being analyzed,
the possibility of a missing @ConstructorBinding annotation on a
@ConfigurationProperties class was not considered.

This commit updates the failure analysis for failed constructor
injection of an instance of a @ConfigurationProperties-annotated
class. When such a failure occurs, adding @ConstructorBinding is
now suggested as an action.

Closes gh-18545
pull/18612/head
Andy Wilkinson 5 years ago
parent 04e035caff
commit 07d0794827

@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.diagnostics.analyzer; package org.springframework.boot.autoconfigure.diagnostics.analyzer;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -30,6 +31,7 @@ import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.InjectionPoint;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.UnsatisfiedDependencyException; import org.springframework.beans.factory.UnsatisfiedDependencyException;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
@ -39,10 +41,14 @@ import org.springframework.boot.autoconfigure.condition.ConditionEvaluationRepor
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcome; import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcomes; import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcomes;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.boot.diagnostics.FailureAnalysis; import org.springframework.boot.diagnostics.FailureAnalysis;
import org.springframework.boot.diagnostics.analyzer.AbstractInjectionFailureAnalyzer; import org.springframework.boot.diagnostics.analyzer.AbstractInjectionFailureAnalyzer;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.type.MethodMetadata; import org.springframework.core.type.MethodMetadata;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory; import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReader;
@ -85,11 +91,14 @@ class NoSuchBeanDefinitionFailureAnalyzer extends AbstractInjectionFailureAnalyz
StringBuilder message = new StringBuilder(); StringBuilder message = new StringBuilder();
message.append(String.format("%s required %s that could not be found.%n", message.append(String.format("%s required %s that could not be found.%n",
(description != null) ? description : "A component", getBeanDescription(cause))); (description != null) ? description : "A component", getBeanDescription(cause)));
List<Annotation> injectionAnnotations = findInjectionAnnotations(rootFailure); InjectionPoint injectionPoint = findInjectionPoint(rootFailure);
if (!injectionAnnotations.isEmpty()) { if (injectionPoint != null) {
message.append(String.format("%nThe injection point has the following annotations:%n")); Annotation[] injectionAnnotations = injectionPoint.getAnnotations();
for (Annotation injectionAnnotation : injectionAnnotations) { if (injectionAnnotations.length > 0) {
message.append(String.format("\t- %s%n", injectionAnnotation)); message.append(String.format("%nThe injection point has the following annotations:%n"));
for (Annotation injectionAnnotation : injectionAnnotations) {
message.append(String.format("\t- %s%n", injectionAnnotation));
}
} }
} }
if (!autoConfigurationResults.isEmpty() || !userConfigurationResults.isEmpty()) { if (!autoConfigurationResults.isEmpty() || !userConfigurationResults.isEmpty()) {
@ -105,6 +114,18 @@ class NoSuchBeanDefinitionFailureAnalyzer extends AbstractInjectionFailureAnalyz
(!autoConfigurationResults.isEmpty() || !userConfigurationResults.isEmpty()) (!autoConfigurationResults.isEmpty() || !userConfigurationResults.isEmpty())
? "revisiting the entries above or defining" : "defining", ? "revisiting the entries above or defining" : "defining",
getBeanDescription(cause)); getBeanDescription(cause));
if (injectionPoint != null && injectionPoint.getMember() instanceof Constructor) {
Constructor<?> constructor = (Constructor<?>) injectionPoint.getMember();
Class<?> declaringClass = constructor.getDeclaringClass();
MergedAnnotation<ConfigurationProperties> configurationProperties = MergedAnnotations.from(declaringClass)
.get(ConfigurationProperties.class);
if (configurationProperties.isPresent()) {
action = String.format(
"%s%nConsider adding @%s to %s if you intended to use constructor-based "
+ "configuration property binding.",
action, ConstructorBinding.class.getSimpleName(), constructor.getName());
}
}
return new FailureAnalysis(message.toString(), action, cause); return new FailureAnalysis(message.toString(), action, cause);
} }
@ -182,13 +203,13 @@ class NoSuchBeanDefinitionFailureAnalyzer extends AbstractInjectionFailureAnalyz
} }
} }
private List<Annotation> findInjectionAnnotations(Throwable failure) { private InjectionPoint findInjectionPoint(Throwable failure) {
UnsatisfiedDependencyException unsatisfiedDependencyException = findCause(failure, UnsatisfiedDependencyException unsatisfiedDependencyException = findCause(failure,
UnsatisfiedDependencyException.class); UnsatisfiedDependencyException.class);
if (unsatisfiedDependencyException == null) { if (unsatisfiedDependencyException == null) {
return Collections.emptyList(); return null;
} }
return Arrays.asList(unsatisfiedDependencyException.getInjectionPoint().getAnnotations()); return unsatisfiedDependencyException.getInjectionPoint();
} }
private class Source { private class Source {

@ -30,6 +30,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionEvaluationRepor
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.diagnostics.FailureAnalysis; import org.springframework.boot.diagnostics.FailureAnalysis;
import org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter; import org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.util.TestPropertyValues;
@ -155,6 +157,16 @@ class NoSuchBeanDefinitionFailureAnalyzerTests {
.containsPattern("@org.springframework.beans.factory.annotation.Qualifier\\(value=\"*alpha\"*\\)"); .containsPattern("@org.springframework.beans.factory.annotation.Qualifier\\(value=\"*alpha\"*\\)");
} }
@Test
void failureAnalysisForConfigurationPropertiesThatMaybeShouldHaveBeenConstructorBound() {
FailureAnalysis analysis = analyzeFailure(
createFailure(ConstructorBoundConfigurationPropertiesConfiguration.class));
assertThat(analysis.getAction()).startsWith(
String.format("Consider defining a bean of type '%s' in your configuration.", String.class.getName()));
assertThat(analysis.getAction()).contains(
"Consider adding @ConstructorBinding to " + NeedsConstructorBindingProperties.class.getName());
}
private void assertDescriptionConstructorMissingType(FailureAnalysis analysis, Class<?> component, int index, private void assertDescriptionConstructorMissingType(FailureAnalysis analysis, Class<?> component, int index,
Class<?> type) { Class<?> type) {
String expected = String.format( String expected = String.format(
@ -167,6 +179,7 @@ class NoSuchBeanDefinitionFailureAnalyzerTests {
assertThat(analysis.getAction()).startsWith(String.format( assertThat(analysis.getAction()).startsWith(String.format(
"Consider revisiting the entries above or defining a bean of type '%s' in your configuration.", "Consider revisiting the entries above or defining a bean of type '%s' in your configuration.",
type.getName())); type.getName()));
assertThat(analysis.getAction()).doesNotContain("@ConstructorBinding");
} }
private void assertActionMissingName(FailureAnalysis analysis, String name) { private void assertActionMissingName(FailureAnalysis analysis, String name) {
@ -359,4 +372,25 @@ class NoSuchBeanDefinitionFailureAnalyzerTests {
} }
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(NeedsConstructorBindingProperties.class)
static class ConstructorBoundConfigurationPropertiesConfiguration {
}
@ConfigurationProperties("test")
static class NeedsConstructorBindingProperties {
private final String name;
NeedsConstructorBindingProperties(String name) {
this.name = name;
}
String getName() {
return this.name;
}
}
} }

Loading…
Cancel
Save