Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 521
FoxyBOA Avatar asked Oct 31 '09 18:10

FoxyBOA


2 Answers

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

<property name="factoryBean" ref="&amp;theFactoryBean" />
like image 115
mhaller Avatar answered Oct 13 '22 17:10

mhaller


You can get the factory bean itself using the & syntax in the spring config:

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

as opposed to:

<property name="createdBean" ref="theFactoryBean" />
like image 32
oxbow_lakes Avatar answered Oct 13 '22 18:10

oxbow_lakes