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

LazyInitTargetSourceCreator does not work in Java-based configuration [SPR-10508] #15140

Closed
spring-issuemaster opened this issue May 1, 2013 · 8 comments
Assignees
Milestone

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented May 1, 2013

Tobias Flohre opened SPR-10508 and commented

LazyInitTargetSourceCreator does not work in Java based configuration, as you can see in the example code in this forum thread: http://forum.springsource.org/showthread.php?125489-Configuration-BeanNameAutoProxyCreator-and-LazyInitTargetSourceCreator.

Interesting: a workaround is making the @Bean method for the lazy service object static.


Affects: 3.2.2

Attachments:

Issue Links:

  • #16754 InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation not called for bean declared on JavaConfig ("depends on")
  • #16756 InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation called with wrong beanClass for bean declared on JavaConfig ("depends on")
  • #12735 javaconf + @Autowired + @PostConstruct + deep context import bug

Referenced from: commits 8c9274e

2 votes, 5 watchers

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Sep 11, 2013

Antal Kiss commented

Please try to solve this in the next version.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Sep 1, 2014

Arrault Fabien commented

This Issue is still present in Spring 4.0.6.

My tests shows that the problem is linked to the fact the lazy beans are created with JavaConfig, not to the fact that the LazyInitTargetSourceCreator or BeanNameAutoProxyCreator are created with JavaConfig.

I have created two issues for the root causes (in my opinion) of this problem : #16754 and #16756

The workaround works :
When the @Lazy @Bean method is static AND the @Configuration class does not implement any interface (because in this case the proxyTargetClass is forced to true on the ProxyFactory used), the proxy is created
If the @Configuration class do implement an interface, you must force the proxyTargetClass to true yourself on the BeanNameAutoProxyCreator to make it works
In both cases : the final proxyTargetClass setting if true .... while it may not be necessary

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Sep 1, 2014

Arrault Fabien commented

TestCase "AutoProxyLazyInitTest" added to reproduce different behaviours described in my comment.

In particular making the @Configuration implements ApplicationListener make ClassCastException when the proxyid bean is used (when the @Bean as the static modifier)

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Sep 2, 2014

Juergen Hoeller commented

It turns out that after fixing #16754, another issue gets in the way: The fix for #12735 prevents proper delegation to a target @Bean method for which a proxy has been registered already. This will be resolved along with this issue.

Juergen

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Sep 2, 2014

Juergen Hoeller commented

The #12735 case is now handled in ConstructorResolver itself: If the requested singleton magically appears after resolving its target factory bean, we throw an IllegalStateException which in turn gets caught by DefaultSingletonBeanRegistry and silently turned into the pre-existing singleton instance. This avoids special treatment in BeanMethodInterceptor and allows LazyInitTargetSource to work with @Bean methods as well.

Juergen

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Sep 3, 2014

Juergen Hoeller commented

This is available in the latest 4.1 snapshot now. If you have the chance, please give it a try today; the 4.1 GA release is scheduled for tomorrow European morning!

Juergen

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Sep 3, 2014

Arrault Fabien commented

I made a few tests and as far as I am concerned it works.

Fabien

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Sep 3, 2014

Juergen Hoeller commented

Great to hear - thanks for the quick turnaround!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.