Skip to content

SPR-16628 - Improve NoSuchBeanDefinitionException to include a reference to the dependent bean #1751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class NoSuchBeanDefinitionException extends BeansException {
@Nullable
private ResolvableType resolvableType;

@Nullable
private ResolvableType dependentBeanType;


/**
* Create a new {@code NoSuchBeanDefinitionException}.
Expand All @@ -61,6 +64,12 @@ public NoSuchBeanDefinitionException(String name, String message) {
this.beanName = name;
}

public NoSuchBeanDefinitionException(String name, ResolvableType dependentBeanType) {
super("'" + dependentBeanType + "' depends on a bean named '" + name + "' but it wasn't available");
this.beanName = name;
this.dependentBeanType = dependentBeanType;
}

/**
* Create a new {@code NoSuchBeanDefinitionException}.
* @param type required type of the missing bean
Expand Down Expand Up @@ -99,7 +108,6 @@ public NoSuchBeanDefinitionException(ResolvableType type, String message) {
this.resolvableType = type;
}


/**
* Return the name of the missing bean, if it was a lookup <em>by name</em> that failed.
*/
Expand Down Expand Up @@ -136,4 +144,9 @@ public int getNumberOfBeansFound() {
return 0;
}

@Nullable
public ResolvableType getDependentBeanType() {
return this.dependentBeanType;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,18 @@ else if (args != null) {
"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
}
registerDependentBean(dep, beanName);
getBean(dep);
try {
getBean(dep);
}catch(NoSuchBeanDefinitionException ex) {
Class dependentClass = null;
try {
dependentClass = Class.forName(mbd.getBeanClassName());
}
catch (ClassNotFoundException e) {} //swallow
finally {
throw new NoSuchBeanDefinitionException(dep, ResolvableType.forClass(dependentClass));
}
}
}
}

Expand Down