Is it possible to have multiple PropertyPlaceHolderConfigurer in my applicationContext?

Viewed 48661

I need to load a specific applicationContext.xml file according to a given system property. This itself loads a file with the actual configuration. Therefore I need two PropertyPlaceHolderConfigurer, one which resolves the system param, and the other one within the actual configuration.

Any ideas how to do this?

8 Answers

Yes you can do more than one. Be sure to set ignoreUnresolvablePlaceholders so that the first will ignore any placeholders that it can't resolve.

<bean id="ppConfig1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="ignoreUnresolvablePlaceholders" value="true"/>
   <property name="locations">
    <list>
             <value>classpath*:/my.properties</value>
    </list>
  </property>
</bean>

<bean id="ppConfig2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="ignoreUnresolvablePlaceholders" value="false"/>
   <property name="locations">
    <list>
             <value>classpath*:/myOther.properties</value>
    </list>
  </property>
</bean>

Depending on your application, you should investigate systemPropertiesMode, it allows you to load properties from a file, but allow the system properties to override values in the property file if set.

Just giving 2 distinct ids worked for me. I am using spring 3.0.4.

Hope that helps.

In case, you need to define two PPC's (like in my situation) and use them independently. By setting property placeholderPrefix, you can retrieve values from desired PPC. This will be handy when both set of PPC's properties has same keys, and if you don't use this the property of ppc2 will override ppc1.

Defining your xml:

<bean name="ppc1"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="properties" ref="ref to your props1" />
        <property name="placeholderPrefix" value="$prefix1-{" />
    </bean>
<bean name="ppc2"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="properties" ref="ref to your props2" />
        <property name="placeholderPrefix" value="$prefix2-{" />
    </bean>

Retrieving during Run time:

@Value(value = "$prefix1-{name}")
private String myPropValue1;

@Value(value = "$prefix2-{name}")
private String myPropValue2;
Related