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

Registering null singleton results in BNFE on context refresh [SPR-7523] #12181

Closed
spring-projects-issues opened this issue Sep 3, 2010 · 3 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Chris Beams opened SPR-7523 and commented

To reproduce:

@Test
public void nullBeanRegistration() {
		DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
		bf.registerSingleton("nullBean", null);
		new GenericApplicationContext(bf).refresh();
}

Notice the BeanNotFoundException upon refresh().

This is happening because DefaultSingletonBeanRegistry.getSingleton() returns literal null, and AbstractBeanFactory.isFactoryBean() interprets null as meaning "No singleton instance found".

Resolution:

In isFactoryBean(), if the call to getSingleton() returns null, need need to double check whether the bean name is listed in getSingletonNames(). Just evaluating whether it's null is not enough information upon which to make the subsequent decisions.

Those 'subsequent decisions' include assuming that the bean name in question ('nullBean') must be associated with a BeanDefinition, and thus we get the BNFE mentioned above.


Referenced from: commits 05a3f3a

@spring-projects-issues
Copy link
Collaborator Author

Chris Beams commented

Juergen - after attempting to implement the fix, I need to discuss this with you. Ping me when free, thanks.

@spring-projects-issues
Copy link
Collaborator Author

Chris Beams commented

On further consideration, the best approach is probably to assert not null as a guard within registerSingleton(). This was after all just a mistake on my part that led to recognizing the bug in the first place. If the method failed in such a fashion, I would have realized my error quickly.

It's unlikely that there is a legitimate use case for registerSingleton("beanName", null) in the first place, so preventing it shouldn't cause much pain for anyone.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

I eventually fixed this through containsSingleton checks in four AbstractBeanFactory sections. AbstractBeanFactory does contain some explicit support for null instances (primarily for the purposes of a factory method returning null), so it is arguably more consistent if we do support this for manually registered singletons as well.

Juergen

@spring-projects-issues spring-projects-issues added type: bug A general bug in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.0.5 milestone Jan 11, 2019
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) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants