property-placeholder location from another property

后端 未结 2 1307
灰色年华
灰色年华 2020-12-01 06:10

I need to load some properties into a Spring context from a location that I don\'t know until the program runs.

So I thought that if I had a PropertyPlaceholderConf

相关标签:
2条回答
  • 2020-12-01 06:35

    The problem here is that you're trying to configure a property place holder using property placeholder syntax :) It's a bit of a chicken-and-egg situation - spring can't resolve your ${my.location} placeholder until it's configured the property-placeholder.

    This isn't satisfactory, but you could bodge it by using more explicit syntax:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceHolderConfigurer">
       <property name="location">
          <bean class="java.lang.System" factory-method="getenv">
             <constructor-arg value="my.location"/>
          </bean>
       </property>
    </bean>
    
    0 讨论(0)
  • 2020-12-01 06:43

    You can do this with a slightly different approach. Here is how we configure it. I load default properties and then overrided them with properties from a configurable location. This works very well for me.

    <bean id="propertyPlaceholderConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
            <property name="locations">
                <list>
                    <value>classpath:site/properties/default/placeholder.properties
                    </value>
                    <value>classpath:site/properties/${env.name}/placeholder.properties
                    </value>
                </list>
            </property>
        </bean>
    
    0 讨论(0)
提交回复
热议问题