Spring FactoryBean and scopes working together

Posted by TTar on Stack Overflow See other posts from Stack Overflow or by TTar
Published on 2010-05-13T21:11:55Z Indexed on 2010/05/13 21:14 UTC
Read the original article Hit count: 188

Filed under:

I'd like to use FactoryBeans and scopes together. Specifically, I'd like the object created and returned by a FactoryBean to be placed into a specified (perhaps custom) scope. The issue is that doing the following:

<bean class="x.y.z.TestFactoryBean" scope="test" />

Results in the FactoryBean itself being scoped, and has somewhat unpredictable behaviour on the object created by the factory. I understand why this is; the factory itself is a first-class spring-managed bean, and has its own lifecycle. However, I can't find a way to specify that the object returned from the factory should itself be scoped.

On the other hand, this does exactly what I want (as long as TestFactoryBean does NOT implement the FactoryBean interface):

<bean class="x.y.z.TestFactoryBean" name="testFactory">
<bean class="x.y.z.TestBean" factory-bean="testFactory" 
      factory-method="getObject" scope="test" />

So the real question is, how can I make Spring behave like it does for the 2nd example above, but using real FactoryBeans?

© Stack Overflow or respective owner

Related posts about spring