-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Calls to FactoryBean @Bean methods cause ClassCastException [SPR-6602] #11268
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
Comments
Liu, Yinwei David commented Please see test case:
public static void main(String[] args) { public class C3 { public class MyFactoryBean implements FactoryBean<MyBean>, InitializingBean, SmartLifecycle {
... public class MyBean { |
clay atkins commented I get a class cast exception when the applicationEntityManagerFactoryBean is called in the applicationEntityManagerFactory method:
|
Chris Beams commented Attaching a simplified example that reproduces the issue. Import as a maven project and execute the |
Chris Beams commented Resolved. The ClassCastException was due to how caching of FactoryBean instances works. The subclass proxy implementation of the FactoryBean method was delegating to the container to return any cached instance of the value originally returned from getObject() during container initialization. The interceptor for the proxy now specifically checks to see if a bean is a FactoryBean, and if so returns the actual FactoryBean (via &-dereferencing). It also became clear while resolving this issue that calling a FactoryBean's getObject() method directly from within another For example, consider a Hibernate SessionFactory configured through Spring's LocalSessionFactoryBean:
One would expect that references to 'sessionFactory' will all receive the same (singleton) SessionFactory instance.
the fooDao and barDao beans should have a reference to the same single sessionFactory instance. Now consider the same scenario in a
In the example above, fooDao and barDao will recieve different instances of the SessionFactory object! This is because while the sessionFactory() bean method is guaranteed to return the cached, singleton LocalSessionFactoryBean instance, once that instance is obtained, calling its getObject() method simply executes the creation logic for the LSFB anew. This behavior is out of sync with XML and generally undesirable. For this reason, functionality has been added along with this bugfix to proxy any FactoryBean instances returned directly from |
Chris Beams commented updated issue title for searchability and changed issue type from new feature to bug |
Chris Beams commented As a workaround until 3.0.1 is released, you have several options:
Again, these are all workarounds and non-ideal in different ways. The fix just committed should allow for usage of FactoryBeans in a way that parallels XML closely. |
Liu, Yinwei David opened SPR-6602 and commented
Hi,
It looks like that the javaconfig in Spring 3 does not support configure a FactoryBean in javaconfig, and then easily inject it to another Bean.
What I want in Spring 3 java config is:
class MyFactoryBean implements FactoryBean<B>{}
Can Spring 3 support FactoryBean in its javaconfig?
Affects: 3.0 GA
Attachments:
Issue Links:
Referenced from: commits 4c05eae
1 votes, 1 watchers
The text was updated successfully, but these errors were encountered: