Spring: Getting FactoryBean object instead of FactoryBean.getObject()

FoxyBOA picture FoxyBOA · Oct 31, 2009 · Viewed 9.7k times · Source

Short question: If I have class that impelemnts FactoryBean interface, how can I get from FactoryBean object itself instead of FactoryBean.getObject()?

Long question: I have to use 3-rd party Spring based library which is hardly use FactoryBean interface. Right now I always must configure 2 beans:

<!-- Case 1-->
<bean id="XYZ" class="FactoryBean1" scope="prototype">
    <property name="steps">
        <bean class="FactoryBean2">
            <property name="itemReader" ref="aName"/>
        </bean>
    </property>
</bean>

<bean id="aName" class="com.package.ClassName1" scope="prototype">
    <property name="objectContext">
        <bean class="com.package.ABC"/>
    </property>
</bean>

<!-- Case 2-->
<bean id="XYZ2" class="FactoryBean1" scope="prototype">
    <property name="steps">
        <bean class="FactoryBean2">
            <property name="itemReader" ref="aName2"/>
        </bean>
    </property>
</bean>

<bean id="aName2" class="com.package.ClassName1" scope="prototype">
    <property name="objectContext">
        <bean class="com.package.QWE"/>
    </property>
</bean>

Actyually defintion of a bean with name "XYZ" (compare with "XYZ2") never will be changed, but because of factory nature I must copy the code for each configuration. Definition of a bean with name "aName" always will be new (i.e. each configuration will have own objectContext value).

I would like to simplify the configuration have a single factory bean (remove "XYZ2" and rid of link to "aName"):

<bean id="XYZ" class="FactoryBean1" scope="prototype">
    <property name="steps">
        <bean class="FactoryBean2"/>
    </property>
</bean>

<bean id="aName" class="com.package.ClassName1" scope="prototype">
    <property name="objectContext">
        <bean class="com.package.ABC"/>
    </property>
</bean>


<bean id="aName2" class="com.package.ClassName1" scope="prototype">
    <property name="objectContext">
        <bean class="com.package.QWE"/>
    </property>
</bean>

Unfortunately, it's not as simple as I expect. I suppose to glue factory (i.e. XYZ bean from the example) with necessary objects (i.e. "aName", "aName2") at runtime. The approach doesn't work because when I ask Spring for FactoryBean object it returns to me FactoryBean.getObject() which impossible to instanciate at that time because of missing itemReader value.

I hope that SpringSource foresee my case I can somehome "hook" FactoryBean.getObject() call to provide all necessary properties at runtime.

Another complexity that disturb me a bit it's chains of Factories (Factory1 get an object from Factory2 that I have to "hook" at runtime).

Any ideas will be appreciated.

Answer

mhaller picture mhaller · Nov 1, 2009

It's the & (ampersand), not the At-symbol, see Spring Framework documentation: Customizing instantiation logic using FactoryBeans

<property name="factoryBean" ref="&amp;theFactoryBean" />