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

null FactoryBean results in BeanCreationException [SPR-16250] #20797

Closed
spring-issuemaster opened this issue Nov 30, 2017 · 0 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Nov 30, 2017

Francisco Lozano opened SPR-16250 and commented

When a FactoryBean declaration returns null:

if (something != null) {
     MethodInvokingFactoryBean methodInvokingFactoryBean = new MethodInvokingFactoryBean();
     methodInvokingFactoryBean.setTargetClass(MyClass.class);
     methodInvokingFactoryBean.setTargetMethod("init");
     methodInvokingFactoryBean.setArguments(new Object[] { something });
     return methodInvokingFactoryBean;
} else {
     return null;
}

this happens:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'methodInvokingFactoryBean': Bean instance of type [class org.springframework.beans.factory.support.NullBean] is not a FactoryBean
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getFactoryBean(FactoryBeanRegistrySupport.java:208)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:955)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:833)
	at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:557)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:428)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:399)
	at org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(BeanFactoryUtils.java:222)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1273)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1098)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:583)
	... 65 more

I guess it's the same as #20582, as the NullBean is being exposed.


Affects: 5.0.2

Reference URL: #20582

Issue Links:

  • #20582 CastClass exception when wiring Map of beans (NullBean instead of 'null' in the map)
  • #20384 Autowire contract is not honored in cases where FactoryBean or @Bean return null

Referenced from: commits 18d90ec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.