使用spring覆盖属性文件 [英] Overriding property file using spring

查看:114
本文介绍了使用spring覆盖属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following property file defined in one of my Spring (3.1) XMLs:

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

我希望能够定义第二个可选属性文件,该文件将覆盖"MyConfigFile.properties"文件,并将代替该文件加载.

I want to be able to define a second optional property file which will override the "MyConfigFile.properties" file and will get loaded instead of it.

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

In Other words I want my application to load the "MyConfigFile.properties" file, but if a "StrogerConfigFile.properties" will be available at the classpath- it will get loaded instead.

任何人都知道如何使用Spring XML做到这一点吗?

Anyone knows how it can be done using the Spring XML?

推荐答案

<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中具有基本的默认值,在属性文件中具有默认值,而在另一个属性文件中具有替代值.

This is a setup I use that is pretty flexible. Allows you to have basic default values directly in the xml, defaults in a properties file and overrides in another properties file.

这篇关于使用spring覆盖属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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