Skip to content
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

Add javadoc notes on potential exception suppression in ListableBeanFactory#getBeansOfType #34629

Closed
bekoenig opened this issue Mar 21, 2025 · 4 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: documentation A documentation task
Milestone

Comments

@bekoenig
Copy link

bekoenig commented Mar 21, 2025

Hi,

DefaultListableBeanFactory#getBeansOfType catches and swallows BeanCreationException. The method documentation and name creates the assumption, that getBeansOfType has the same behiour like

for (String beanName : listableBeanFactory.getBeanNamesForType(type)) {
  Object bean = listableBeanFactory.getBean(beanName);
}

But the result is totally differnt. This behaviour is not well documented and unexpected to the client.

Best greetings,
Ben

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 21, 2025
@jhoeller
Copy link
Contributor

jhoeller commented Mar 21, 2025

getBeansOfType suppresses exceptions for circular reference scenarios where the root cause is a BeanCurrentlyInCreationException, is this what you mean? That should not be common at all but obviously you might run into it. In any case, we can't change that behavior due to backwards compatibility concerns, so we can only really document it.

Note that we generally recommend getBeanNamesForType with explicit bean instance retrieval, also for potentially lazy bean creation purposes. I suppose we should document that recommendation as well.

@jhoeller jhoeller added type: documentation A documentation task in: core Issues in core modules (aop, beans, core, context, expression) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 21, 2025
@jhoeller jhoeller self-assigned this Mar 21, 2025
@jhoeller jhoeller added this to the 6.2.6 milestone Mar 21, 2025
@bekoenig
Copy link
Author

bekoenig commented Mar 21, 2025

Hello @jhoeller,

getBeansOfType also suppresses UnsatisfiedDependencyException. In my scenario, this bean depends on a bean provided by a later processed starter. To indicate an implementation error, an exception should be thrown. However, currently, this bean seems to be incorrectly configured or not found by my starter.

I will change my implementation to getBeanNamesForType and getBean for fail-fast.

Thank you!

@jhoeller
Copy link
Contributor

It only suppresses UnsatisfiedDependencyException if the root cause is a BeanCurrentlyInCreationException, and even then only if the affected target bean is still in creation at the point of evaluating the exception (check the implementation of DefaultListableBeanFactory.getBeansOfType). Otherwise, it rethrows the exception. I can see that this might be unexpected in some scenarios but I'm afraid it's already tightened to only do what's absolutely necessary for handling circular reference scenarios.

I'll reopen this issue for adding some javadoc notes to getBeansOfType, including a recommendation to consider getBeanNamesForType as an alternative.

@jhoeller jhoeller reopened this Mar 21, 2025
@jhoeller jhoeller changed the title DefaultListableBeanFactory#getBeansOfType swallows UnsatisfiedDependencyException Add javadoc notes on potential exception suppression in ListableBeanFactory#getBeansOfType Mar 21, 2025
@jhoeller jhoeller added the for: backport-to-6.1.x Marks an issue as a candidate for backport to 6.1.x label Mar 21, 2025
@github-actions github-actions bot added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-6.1.x Marks an issue as a candidate for backport to 6.1.x labels Mar 21, 2025
@bekoenig
Copy link
Author

Your improvement on javadoc is good and clear enough. I think this note would have prevented my bug.

jhoeller added a commit that referenced this issue Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

3 participants