Skip to content

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

@spring-projects-issues

Description

@spring-projects-issues

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

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions