Using multiple property files (via PropertyPlaceholderConfigurer) in multiple projects/modules

前端 未结 5 975
情书的邮戳
情书的邮戳 2020-12-07 08:41

We are currently writing an application which is split into multiple projects/modules. For example, let\'s take the following modules:

  • myApp-DAO
  • myApp
相关标签:
5条回答
  • 2020-12-07 08:42

    I tried the solution below, it works on my machine.

    <context:property-placeholder location="classpath*:connection.properties" ignore-unresolvable="true" order="1" />
    
    <context:property-placeholder location="classpath*:general.properties" order="2"/>
    

    In case multiple elements are present in the Spring context, there are a few best practices that should be followed:

    the order attribute needs to be specified to fix the order in which these are processed by Spring all property placeholders minus the last one (highest order) should have ignore-unresolvable=”true” to allow the resolution mechanism to pass to others in the context without throwing an exception

    source: http://www.baeldung.com/2012/02/06/properties-with-spring/

    0 讨论(0)
  • 2020-12-07 08:50

    The PropertiesPlaceholderConfigurer bean has an alternative property called "propertiesArray". Use this instead of the "properties" property, and configure it with an <array> of property references.

    0 讨论(0)
  • 2020-12-07 09:04

    You can have multiple <context:property-placeholder /> elements instead of explicitly declaring multiple PropertiesPlaceholderConfigurer beans.

    0 讨论(0)
  • 2020-12-07 09:06

    If you ensure that every place holder, in each of the contexts involved, is ignoring unresolvable keys then both of these approaches work. For example:

    <context:property-placeholder
    location="classpath:dao.properties,
              classpath:services.properties,
              classpath:user.properties"
    ignore-unresolvable="true"/>
    

    or

        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:dao.properties</value>
                    <value>classpath:services.properties</value>
                    <value>classpath:user.properties</value>
                </list>
            </property> 
            <property name="ignoreUnresolvablePlaceholders" value="true"/>
        </bean>
    
    0 讨论(0)
  • 2020-12-07 09:07

    I know that this is an old question, but the ignore-unresolvable property was not working for me and I didn't know why.

    The problem was that I needed an external resource (something like location="file:${CATALINA_HOME}/conf/db-override.properties") and the ignore-unresolvable="true" does not do the job in this case.

    What one needs to do for ignoring a missing external resource is:

    ignore-resource-not-found="true"
    

    Just in case anyone else bumps into this.

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