春季:获取FactoryBean对象而不是FactoryBean.getObject() [英] Spring: Getting FactoryBean object instead of FactoryBean.getObject()

查看:109
本文介绍了春季:获取FactoryBean对象而不是FactoryBean.getObject()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短的问题::如果我有可以实现FactoryBean接口的类,那么如何从FactoryBean对象本身而不是FactoryBean.getObject()中获得数据呢?

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

长问题:我必须使用基于第三方Spring的库,该库几乎不使用FactoryBean接口.现在,我总是必须配置2个bean:

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>

实际上不会更改名称为"XYZ"(与"XYZ2"比较)的bean的定义,但是由于工厂的性质,我必须为每个配置复制代码. 名称为"aName"的bean的定义始终是新的(即,每个配置都有自己的objectContext值).

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).

我想简化具有单个工厂bean的配置(删除"XYZ2"并删除指向"aName"的链接):

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>

不幸的是,这并不像我期望的那么简单.我想在运行时将工厂(即示例中的XYZ bean)与必要的对象(即"aName","aName2")粘合在一起. 这种方法行不通,因为当我向Spring请求FactoryBean对象时,它会返回给我FactoryBean.getObject(),由于缺少itemReader值,当时无法实例化.

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.

我希望SpringSource预见到我的情况,我可以调用homeBean挂钩" FactoryBean.getObject()来在运行时提供所有必要的属性.

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

另一种让我感到不安的复杂性是工厂链(Factory1从Factory2获取一个对象,我必须在运行时对其进行挂钩").

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.

推荐答案

它是&(&"号),而不是At符号,请参见Spring Framework文档:

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

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

这篇关于春季:获取FactoryBean对象而不是FactoryBean.getObject()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆