Is it possible to have multiple PropertyPlaceHolderConfigurer in my applicationContext?

前端 未结 8 1235
臣服心动
臣服心动 2020-12-01 01:16

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

相关标签:
8条回答
  • 2020-12-01 02:04

    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;
    
    0 讨论(0)
  • 2020-12-01 02:05

    Another solution is to use placeholderPrefix property of PropertyPlaceholderConfigurer. You specify it for the second (third, fourth...) configurer, and then prefix all your corresponding placeholders, thus there will be no conflict.

    <bean id="mySecondConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
            p:location="classpath:/myprops.properties" 
            p:placeholderPrefix="myprefix-"/>
    
    <bean class="com.mycompany.MyClass" p:myprop="${myprefix-value.from.myprops}"/>
    
    0 讨论(0)
提交回复
热议问题