[SPR-12915] ConfigurationClassEnhancer.enhanceFactoryBean is not transparent for method calls other than getObject() Created: 15/Apr/15 Updated: 15/Jan/19 Resolved: 04/Jun/15
|Fix Version/s:||4.2 RC1|
|Reporter:||Adrian Moos||Assignee:||Juergen Hoeller|
|Remaining Estimate:||Not Specified|
|Time Spent:||Not Specified|
|Original Estimate:||Not Specified|
|Days since last comment:||1 week, 2 days ago|
|Last commented by a User:||true|
When a @Bean method returns an instance of a FactoryBean, Spring proxies the factory bean, redirecting calls to getObject() to applicationContext.getBean().
The relevant code reads:
In the marked line, obj refers to the proxy object.
Therefore, calls to methods other than getObject() are forwarded to the super implementation on the proxy object, which has a different state than the FactoryBean it proxies.
This breaks the following usecase:
where DBTool has a method:
which now throws
because the proxy object is a factory that has not been configured.
|Comment by Adrian Moos [ 15/Apr/15 ]|
As a workaround, the @Bean method can put a reference to the FactoryBean it has configured into a field:
|Comment by Juergen Hoeller [ 15/Apr/15 ]|
Good catch! Fixed for 4.2 and 4.1.7 now.
|Comment by Juergen Hoeller [ 04/Jun/15 ]|
Due to the implications reported in
Modifying the CGLIB proxy to delegate all calls is surprisingly non-trivial, in particular for cross-method calls within the same instance. We might even have to create such FactoryBean proxy instances via Objenesis, just like we do for AOP proxies. For that reason, this definitely has to go through another RC and is therefore better suited for the 4.2 line.
|Comment by Spring Issuemaster [ 14/Jan/19 ]|