Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding property file using spring

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

<context:property-placeholder location="classpath: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.

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.

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

like image 423
Omri Avatar asked Dec 08 '22 21:12

Omri


1 Answers

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

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.

like image 136
pap Avatar answered Jan 12 '23 23:01

pap