You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@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.
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.
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.
Chris Beams opened SPR-7523 and commented
To reproduce:
Notice the
BeanNotFoundException
uponrefresh()
.This is happening because
DefaultSingletonBeanRegistry.getSingleton()
returns literalnull
, andAbstractBeanFactory.isFactoryBean()
interpretsnull
as meaning "No singleton instance found".Resolution:
In
isFactoryBean()
, if the call togetSingleton()
returns null, need need to double check whether the bean name is listed ingetSingletonNames()
. 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
The text was updated successfully, but these errors were encountered: