NoSuchBeanDefinitionFailureAnalyzer Class — spring-boot Architecture
Architecture documentation for the NoSuchBeanDefinitionFailureAnalyzer class in NoSuchBeanDefinitionFailureAnalyzer.java from the spring-boot codebase.
Entity Profile
Relationship Graph
Source Code
core/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/diagnostics/analyzer/NoSuchBeanDefinitionFailureAnalyzer.java lines 61–341
class NoSuchBeanDefinitionFailureAnalyzer extends AbstractInjectionFailureAnalyzer<NoSuchBeanDefinitionException> {
private final ConfigurableListableBeanFactory beanFactory;
private final MetadataReaderFactory metadataReaderFactory;
private final ConditionEvaluationReport report;
NoSuchBeanDefinitionFailureAnalyzer(BeanFactory beanFactory) {
Assert.isTrue(beanFactory instanceof ConfigurableListableBeanFactory,
"'beanFactory' must be a ConfigurableListableBeanFactory");
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
this.metadataReaderFactory = new CachingMetadataReaderFactory(this.beanFactory.getBeanClassLoader());
// Get early as won't be accessible once context has failed to start
this.report = ConditionEvaluationReport.get(this.beanFactory);
}
@Override
protected @Nullable FailureAnalysis analyze(Throwable rootFailure, NoSuchBeanDefinitionException cause,
@Nullable String description) {
if (cause.getNumberOfBeansFound() != 0) {
return null;
}
List<AutoConfigurationResult> autoConfigurationResults = getAutoConfigurationResults(cause);
List<UserConfigurationResult> userConfigurationResults = getUserConfigurationResults(cause);
StringBuilder message = new StringBuilder();
message.append(String.format("%s required %s that could not be found.%n",
(description != null) ? description : "A component", getBeanDescription(cause)));
InjectionPoint injectionPoint = findInjectionPoint(rootFailure);
if (injectionPoint != null) {
Annotation[] injectionAnnotations = injectionPoint.getAnnotations();
if (injectionAnnotations.length > 0) {
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()) {
message.append(String.format("%nThe following candidates were found but could not be injected:%n"));
for (AutoConfigurationResult result : autoConfigurationResults) {
message.append(String.format("\t- %s%n", result));
}
for (UserConfigurationResult result : userConfigurationResults) {
message.append(String.format("\t- %s%n", result));
}
}
String action = String.format("Consider %s %s in your configuration.",
(!autoConfigurationResults.isEmpty() || !userConfigurationResults.isEmpty())
? "revisiting the entries above or defining" : "defining",
getBeanDescription(cause));
return new FailureAnalysis(message.toString(), action, cause);
}
private String getBeanDescription(NoSuchBeanDefinitionException cause) {
if (cause.getResolvableType() != null) {
Class<?> type = extractBeanType(cause.getResolvableType());
return "a bean of type '" + type.getName() + "'";
}
return "a bean named '" + cause.getBeanName() + "'";
}
private Class<?> extractBeanType(ResolvableType resolvableType) {
Class<?> rawClass = resolvableType.getRawClass();
Assert.state(rawClass != null, "'rawClass' must not be null");
return rawClass;
}
private List<AutoConfigurationResult> getAutoConfigurationResults(NoSuchBeanDefinitionException cause) {
List<AutoConfigurationResult> results = new ArrayList<>();
collectReportedConditionOutcomes(cause, results);
collectExcludedAutoConfiguration(cause, results);
return results;
}
private List<UserConfigurationResult> getUserConfigurationResults(NoSuchBeanDefinitionException cause) {
ResolvableType type = cause.getResolvableType();
if (type == null) {
return Collections.emptyList();
}
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.beanFactory, type);
return Arrays.stream(beanNames)
.map((beanName) -> new UserConfigurationResult(getFactoryMethodMetadata(beanName),
this.beanFactory.getBean(beanName).equals(null)))
.toList();
}
private @Nullable MethodMetadata getFactoryMethodMetadata(String beanName) {
BeanDefinition beanDefinition = this.beanFactory.getBeanDefinition(beanName);
if (beanDefinition instanceof AnnotatedBeanDefinition annotatedBeanDefinition) {
return annotatedBeanDefinition.getFactoryMethodMetadata();
}
return null;
}
private void collectReportedConditionOutcomes(NoSuchBeanDefinitionException cause,
List<AutoConfigurationResult> results) {
this.report.getConditionAndOutcomesBySource()
.forEach((source, sourceOutcomes) -> collectReportedConditionOutcomes(cause, new Source(source),
sourceOutcomes, results));
}
private void collectReportedConditionOutcomes(NoSuchBeanDefinitionException cause, Source source,
ConditionAndOutcomes sourceOutcomes, List<AutoConfigurationResult> results) {
if (sourceOutcomes.isFullMatch()) {
return;
}
BeanMethods methods = new BeanMethods(source, cause);
for (ConditionAndOutcome conditionAndOutcome : sourceOutcomes) {
if (!conditionAndOutcome.getOutcome().isMatch()) {
for (MethodMetadata method : methods) {
results.add(new AutoConfigurationResult(method, conditionAndOutcome.getOutcome()));
}
}
}
}
private void collectExcludedAutoConfiguration(NoSuchBeanDefinitionException cause,
List<AutoConfigurationResult> results) {
for (String excludedClass : this.report.getExclusions()) {
Source source = new Source(excludedClass);
BeanMethods methods = new BeanMethods(source, cause);
for (MethodMetadata method : methods) {
String message = String.format("auto-configuration '%s' was excluded",
ClassUtils.getShortName(excludedClass));
results.add(new AutoConfigurationResult(method, new ConditionOutcome(false, message)));
}
}
}
private @Nullable InjectionPoint findInjectionPoint(Throwable failure) {
UnsatisfiedDependencyException unsatisfiedDependencyException = findCause(failure,
UnsatisfiedDependencyException.class);
if (unsatisfiedDependencyException == null) {
return null;
}
return unsatisfiedDependencyException.getInjectionPoint();
}
private static class Source {
private final String className;
private final @Nullable String methodName;
Source(String source) {
String[] tokens = source.split("#");
this.className = (tokens.length > 1) ? tokens[0] : source;
this.methodName = (tokens.length != 2) ? null : tokens[1];
}
String getClassName() {
return this.className;
}
@Nullable String getMethodName() {
return this.methodName;
}
}
private class BeanMethods implements Iterable<MethodMetadata> {
private final List<MethodMetadata> methods;
BeanMethods(Source source, NoSuchBeanDefinitionException cause) {
this.methods = findBeanMethods(source, cause);
}
private List<MethodMetadata> findBeanMethods(Source source, NoSuchBeanDefinitionException cause) {
try {
MetadataReader classMetadata = NoSuchBeanDefinitionFailureAnalyzer.this.metadataReaderFactory
.getMetadataReader(source.getClassName());
Set<MethodMetadata> candidates = classMetadata.getAnnotationMetadata()
.getAnnotatedMethods(Bean.class.getName());
List<MethodMetadata> result = new ArrayList<>();
for (MethodMetadata candidate : candidates) {
if (isMatch(candidate, source, cause)) {
result.add(candidate);
}
}
return Collections.unmodifiableList(result);
}
catch (Exception ex) {
return Collections.emptyList();
}
}
private boolean isMatch(MethodMetadata candidate, Source source, NoSuchBeanDefinitionException cause) {
if (source.getMethodName() != null && !source.getMethodName().equals(candidate.getMethodName())) {
return false;
}
String name = cause.getBeanName();
ResolvableType resolvableType = cause.getResolvableType();
return ((name != null && hasName(candidate, name))
|| (resolvableType != null && hasType(candidate, extractBeanType(resolvableType))));
}
private boolean hasName(MethodMetadata methodMetadata, String name) {
Map<String, @Nullable Object> attributes = methodMetadata.getAnnotationAttributes(Bean.class.getName());
String[] candidates = (attributes != null) ? (String[]) attributes.get("name") : null;
if (candidates != null) {
for (String candidate : candidates) {
if (candidate.equals(name)) {
return true;
}
}
return false;
}
return methodMetadata.getMethodName().equals(name);
}
private boolean hasType(MethodMetadata candidate, Class<?> type) {
String returnTypeName = candidate.getReturnTypeName();
if (type.getName().equals(returnTypeName)) {
return true;
}
try {
Class<?> returnType = ClassUtils.forName(returnTypeName,
NoSuchBeanDefinitionFailureAnalyzer.this.beanFactory.getBeanClassLoader());
return type.isAssignableFrom(returnType);
}
catch (Throwable ex) {
return false;
}
}
@Override
public Iterator<MethodMetadata> iterator() {
return this.methods.iterator();
}
}
private static class AutoConfigurationResult {
private final MethodMetadata methodMetadata;
private final ConditionOutcome conditionOutcome;
AutoConfigurationResult(MethodMetadata methodMetadata, ConditionOutcome conditionOutcome) {
this.methodMetadata = methodMetadata;
this.conditionOutcome = conditionOutcome;
}
@Override
public String toString() {
return String.format("Bean method '%s' in '%s' not loaded because %s", this.methodMetadata.getMethodName(),
ClassUtils.getShortName(this.methodMetadata.getDeclaringClassName()),
this.conditionOutcome.getMessage());
}
}
private static class UserConfigurationResult {
private final @Nullable MethodMetadata methodMetadata;
private final boolean nullBean;
UserConfigurationResult(@Nullable MethodMetadata methodMetadata, boolean nullBean) {
this.methodMetadata = methodMetadata;
this.nullBean = nullBean;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("User-defined bean");
if (this.methodMetadata != null) {
sb.append(String.format(" method '%s' in '%s'", this.methodMetadata.getMethodName(),
ClassUtils.getShortName(this.methodMetadata.getDeclaringClassName())));
}
if (this.nullBean) {
sb.append(" ignored as the bean value is null");
}
return sb.toString();
}
}
}
Domain
Source
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free