【问题标题】:Can a single property be used in multiple Bean IDs?可以在多个 Bean ID 中使用单个属性吗?
【发布时间】:2018-01-25 21:06:41
【问题描述】:

我有一些完全相同的 SQL 查询,但用于我的 bean XML 中的不同 bean ids/classes。您可以创建一个不绑定到特定 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>

【问题讨论】:

    标签: spring


    【解决方案1】:

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

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

    现在在你的 bean 定义中

    <bean id="regionalPage" class="com.test.RegionalPageDao">
        <property name="jdbcTemplate" ref="jdbcTemplate" />
        <property name="activeList">
            <value>${activeList}</value>
        </property> 
    </bean>
    

    等等。 请看看这个stackoverflow question

    【讨论】:

    • 谢谢曼尼什。正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多