声明具有相同属性的bean

时间:2014-11-13 20:25:09

标签: java spring spring-bean

可以在Spring XML上下文定义几个具有相同属性集的bean吗?

实施例

<bean id="bean1" class="com.my.company.model.MyProcedureBean">
    <property name="name" value="val1"/>
    <property name="pre">
        <list>
            <ref bean="Y00"/>
            <ref bean="YNT"/>
            <ref bean="YAB"/>
        </list>
    </property>
    <property name="post">
        <list>
                <ref bean="YIO"/>
                <ref bean="YC1"/>
        </list>
    </property>
    <property name="plain">
        <list>
            <ref bean="YA3"/>
            <ref bean="YP4"/>
            <ref bean="YA5"/>
        </list>
    </property>
</bean>

<bean id="bean2" class="com.my.company.model.MyProcedureBean">
  // DO NOT WANT TO DUPLICATE ALL PROPERTIES HERE AS AT **bean1**
</bean>

1 个答案:

答案 0 :(得分:3)

将bean的Abstract属性设置为true,然后使用父属性等于抽象bean来定义其他bean! 像这样

<bean id="parentBean" class="xxx" abstract="true">
    <property name="..." value="..." />
    <property name="..." value="..." />
    <property name="..." value="..." />
</bean>

<bean id="bean1" parent="parentBean">

</bean>
<bean id="bean2" parent="parentBean">

</bean>