可以在多个Bean ID中使用单个属性吗?

时间:2018-01-25 21:06:42

标签: spring

我有一些SQL查询完全相同但用于我的bean XML中的不同bean ID /类。您是否可以创建单个属性,而不是绑定到特定的bean,并根据需要将其注入bean ID?

以下是两个不同bean中两个确切属性的示例。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd">


<bean id="regionalPage" class="com.test.RegionalPageDao">
    <property name="jdbcTemplate" ref="jdbcTemplate" />
    <property name="activeList">
        <value><![CDATA[select * from regionalTable]]></value>
    </property> 
</bean>
<bean id="regionalBackup" class="com.test.RegionalPageBackupDao">
    <property name="jdbcTemplate" ref="jdbcTemplate" />
    <property name="activeList">
        <value><![CDATA[select * from regionalTable]]></value>
    </property> 
</bean>
</beans>

1 个答案:

答案 0 :(得分:0)

是的,你可以这样做。在PropertyPlaceholderConfigurer中定义所有属性,它们将在您想要使用它们的项目中可用。 做

Get-ChildItem

现在你的bean定义

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="properties">
        <util:properties>
            <prop key="activeList"><![CDATA[select * from regionalTable]]></prop>
            ...
        </util:properties>
    </property>
</bean>

等等。 请查看此stackoverflow question