Is it possible to have multiple PropertyPlaceHolderConfigurer in my applicationContext?

前端 未结 8 1239
臣服心动
臣服心动 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 01:43

    Beware -- there might be a bug related to multiple configurers. See http://jira.spring.io/browse/SPR-5719 for more details.

    I'm unable to get multiple to work locally... but I'm not yet blaming anyone but myself.

    0 讨论(0)
  • 2020-12-01 01:49

    You can't do this directly, and this JIRA issue from Spring explains why (check the comment from Chris Beams for a detailed explanation):

    https://jira.springsource.org/browse/SPR-6428

    However, he does provide a workaround using Spring 3.1 or later, which is to use the PropertySourcesPlaceholderConfigurer class instead of PropertyPlaceholderConfigurer class.

    You can download a Maven-based project that demonstrates the problem and the solution from the Spring framework issues github:

    https://github.com/SpringSource/spring-framework-issues

    Look for the issue number, SPR-6428, in the downloaded projects.

    0 讨论(0)
  • 2020-12-01 01:51

    We have the following approach working:

    <util:properties id="defaultProperties">
        <prop key="stand.name">DEV</prop>
        <prop key="host">localhost</prop>
    </util:properties>
    <context:property-placeholder 
        location="file:${app.properties.path:app.properties}" 
        properties-ref="defaultProperties"/>
    

    System property app.properties.path can be used to override path to config file.

    And application bundles some default values for placeholders that cannot be defined with defaults in common modules.

    0 讨论(0)
  • 2020-12-01 01:52

    On my own side, playing with PropertyPlaceholderConfigurer both properties :

    • order (should be lower for first accessed/parsed PPC)
    • ignoreUnresolvablePlaceholders ("false" for first accessed/parsed PPC, "true" for next one)

    • and also give 2 distinct id(s) to both PPC (to avoid one to be overwritten by the other)

    works perfectly

    Hope it helps

    0 讨论(0)
  • 2020-12-01 02:00

    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.

    0 讨论(0)
  • 2020-12-01 02:04

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

    Hope that helps.

    0 讨论(0)
提交回复
热议问题