D.C
D.C

Reputation: 425

Multiple property-placeholder with overlapping locations does not work?

I have three properties files:

file1.properties contains:
propA=1

file2.properties contains:
propA=2 propB=2

file3.properties contains:
propA=3 propB=3 propC=3

And two application-context:

applicationContext1.xml contains:
<context:property-placeholder location="classpath:file1.properties,classpath:file2.properties" ignore-resource-not-found="true" ignore-unresolvable="true" system-properties-mode="OVERRIDE"/>

applicationContext2.xml contains:
<context:property-placeholder location="classpath:file2.properties,classpath:file3.properties" ignore-resource-not-found="true" ignore-unresolvable="true" system-properties-mode="OVERRIDE"/>

And a test that loads both contexts and injects all properties. MyTest.java:

@Value("${propA}")
private String propA;

@Value("${propB}")
private String propB;

@Value("${propC}")
private String propC;

And i get the following values:

 propA=2
 propB=2
 propC=3

Why 'propA' and 'propB' wasn't taken from file3.properties?

Upvotes: 1

Views: 657

Answers (2)

gregdim
gregdim

Reputation: 2071

Having multiple property-placeholder-configurers does not work as you assumed. There is no property-override feature. The first one tries and replaces what it can, then the next one takes its chance with what is left and so on. If you want to override properties you might better define a properties bean with multiple sources such as :

<bean name="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
        <property name="locations">
            <list>
                <value>classpath:conf/app-defaults.properties</value>
                <value>file:${CATALINA_BASE}/conf/my-app.properties</value>                 
            </list>
        </property>
        <property name="ignoreResourceNotFound" value="true" />
    </bean>

The above code defines a properties bean with defaults, coming from a classpath file and an optional externalized file that overrides defaults and resides in a tomcat installation. Then you can use a property placeholder like:

<context:property-placeholder properties-ref="appProperties" /> 

Upvotes: 1

Bhagavathy Vinoth
Bhagavathy Vinoth

Reputation: 11

Applicationcontext2 has override applicationcontext1.

To confirm add one new variable in file1.prop that should not available in another two file2 and file3.

Upvotes: 0

Related Questions