使用spring覆盖属性文件

时间:2012-09-12 06:05:09

标签: xml spring javabeans placeholder

我在我的一个Spring(3.1)XML中定义了以下属性文件:

<context:property-placeholder location="classpath:MyConfigFile.properties"/> 

我希望能够定义第二个可选属性文件,该文件将覆盖“MyConfigFile.properties”文件并将被加载而不是它。

换句话说,我希望我的应用程序加载“MyConfigFile.properties”文件,但是如果类路径中有“StrogerConfigFile.properties”,它将被加载。

任何人都知道如何使用Spring XML完成它?

2 个答案:

答案 0 :(得分:13)

<context:property-placeholder location="file:///[path]/override1.properties, file:///[path]/override2.properties" properties-ref="defaultProps" />


<bean id="defaultProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <array>
            <value>classpath:default1.properties</value>
            <value>classpath:default2.properties</value>
        </array>
    </property>
    <property name="properties">
        <util:properties local-override="true">
            <prop key="some.property">some value</prop>
        </util:properties>
    </property>
</bean>

这是我使用的设置非常灵活。允许您直接在xml中使用基本默认值,在属性文件中使用默认值,并在另一个属性文件中覆盖。

答案 1 :(得分:1)

你试过吗

<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
    <list>
        <value>classpath:default.properties</value>
        <value>classpath:overwrite.properties</value>
    </list>
</property>