Multiple properties files in Spring 3.0

前端 未结 3 1266
轮回少年
轮回少年 2020-12-13 20:25

I am working on a project with several separate modules, each with their own application context properties files. I want to be able to load all these properties so that the

相关标签:
3条回答
  • 2020-12-13 20:50

    This is all you need to do:

    <context:property-placeholder location="first.properties" order="0" ignore-unresolvable="true"/>
    <context:property-placeholder location="second.properties" order="0" ignore-unresolvable="true"/>
    <context:property-placeholder location="empty.properties" order="1"/>
    

    Problem is simple: if a property-placeholder has no value for a certain property it will throw an exception, even though other property-placeholder are present.

    The solution uses order to know which is the last property-placeholder and sets ignore-unresolvable="true" on all others, so that every property-placeholder has the chance to provide a value. In multi-module projects the last property-placeholder could be empty or provide failsafe-defaults.

    Note: if you set all property-placeholder to ignore-unresolvable="true" Spring will just pass on what you wrote without throwing an exception. If of course you expect it to be something other than String you will most certainly get an eception like java.lang.NumberFormatException: For input string: "${something}" during format conversion.

    Note: only the first (one with lowest order) property-placeholder with a value for the specific property will be used. If you want to override properties use a greater order range than 0 and 1 or property-override.

    Tested with Spring 3.2.1, but all mentioned properties exist in 3.0. See JavaDoc of PropertyPlaceholderConfigurer

    0 讨论(0)
  • 2020-12-13 20:52

    Following code snippet should get you started

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" id="corePlaceHolder">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
        <property name="searchSystemEnvironment" value="true"/>
        <property name="locations">
            <list>
                <value>classpath*:config/*/config1/*.properties</value>
                <value>classpath*:config/*/config2/*.properties</value>
                <value>classpath*:config/*/config3/*.properties</value>
                <value>classpath*:custom.properties</value>
            </list>
        </property>
    </bean>     
    

    You may store the property files in following hierarchy making sure config is reachable from via classpath

    config
      config1
         a.properties
      config2
         b.properties
      config3
         c.properties
    custom.properties
    
    0 讨论(0)
  • 2020-12-13 21:05

    You can collect multiple property files into one bean quite easily:

    <bean id="allProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
      <property name="singleton" value="true"/>
      <property name="ignoreResourceNotFound" value="true"/>
      <property name="locations">
        <list>
          <value>classpath*:default.properties</value>
          <value>classpath*:overrides.properties</value>
          <value>file:${APP_HOME}/**/*.properties</value>
        </list>
      </property>
    </bean>
    

    This particular example will gather all default.properties, overrides.properties on classpath and property files in your APP_HOME. Now you can refer to this bean from ProperyPlaceholderConfigurer or your custom postprocessor.

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