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

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-29 03:39:09

问题


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.


回答1:


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

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



回答2:


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" />


来源:https://stackoverflow.com/questions/1655140/spring-getting-factorybean-object-instead-of-factorybean-getobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!