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

Doc: @Autowired properties not reliably set in FactoryBean before getObject [SPR-8704] #13346

Closed
spring-issuemaster opened this issue Sep 16, 2011 · 5 comments

Comments

@spring-issuemaster
Copy link
Collaborator

commented Sep 16, 2011

John Kew opened SPR-8704 and commented

If I have a factoryBean with an @Autowired field this field may be null before getObject is called.

I wasn't able to find a similar issue, bu #12108 may be related. It's possible I don't understand the guarantees provided by a FactoryBean. See the test case attached to this issue, and I'll see about adding it to the github test cases.

If two bean factories are declared with:

<bean id="bigFancyBean" class="com.various.BigFancyBeanFactory"/>
<bean id="dependentBean" class="com.various.DependentBeanFactory"/>

And the DependentBeanFactory has:

public class DependentBeanFactory implements FactoryBean<DependentBean> {
    @Autowired
    BigFancyBean myFancyBean;
...

dependentBean.getObject() can be called before myFancyBean is set, often leading to a NullPointerException or other issue.


Affects: 3.0.5

Attachments:

Issue Links:

  • #13893 Intermittent BeanCurrentlyInCreationException when using @Autowired on a FactoryBean or anywhere in its dependency graph ("is duplicated by")
  • #14983 Circular dependency problem ("is duplicated by")

Referenced from: commits 28e114b, fd41f63

2 votes, 8 watchers

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Dec 5, 2013

Alexei Shpikat commented

Also see duplicates in #13893 (provides failing test case for this issue) and #14983

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Dec 6, 2013

Alexei Shpikat commented

Also probably connected to #10716

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Oct 6, 2016

Spencer Gibb commented

Bitten by this in Spring Cloud spring-cloud/spring-cloud-netflix#1366

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Oct 7, 2016

Juergen Hoeller commented

Spencer Gibb, we tend to generally recommend against using annotation-driven injection in FactoryBean implementations. Since such classes implement an interface-based contract to begin with, I would rather design them to use programmatic BeanFactory lookups for the target beans needed.

That said, we can certainly try to provide stronger guarantees here... maybe 5.0 only though, depending on the effort and lifecycle impact.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Oct 7, 2016

Spencer Gibb commented

Understood and changes made. I guess I learned that the hard way :P.

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