PropertyPlaceholderConfigurer and environment variables in .properties files

后端 未结 3 1385
误落风尘
误落风尘 2020-12-25 15:28

I have a Spring application-context.xml with PropertyPlaceholderConfigurer to get properties\' values from .properties file. Main and test source folders have separate .prop

相关标签:
3条回答
  • 2020-12-25 15:44

    Using:

    <context:property-placeholder location="classpath:env.properties"/>
    

    Change your:

    property.name=${env.SYSTEM_PROPERTY}
    

    To:

    property.name=${SYSTEM_PROPERTY}
    

    I'm using Spring 3.0.4.RELEASE, but I have no idea when this was introduced.

    0 讨论(0)
  • 2020-12-25 15:54

    I'd probably change the solution completely: inject the system property directly, as opposed to injecting the property which refers to a system property

    E.g.

    @Value("#{ systemProperties['JAVA_MY_ENV'] }") 
    private String myVar;
    

    or

    <property name ="myVar" value="#{systemProperties['JAVA_MY_ENV']}"/>
    

    I use a property placeholder configurer like this

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations">
        <list>
            <value>classpath:someprops.properties</value>
        </list>
      </property>
      <property name="ignoreResourceNotFound" value="true" />
      <property name="searchSystemEnvironment" value="true" />
      <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    

    You must also remember to pass the parameter into the program using

     -DJAVA_MY_ENV=xyz
    

    This way when you run the production version you can pass one thing and when you are running tests another.

    Also what I often what I do is something like this:

      <property name="locations">
        <list>
          <value>classpath:someprops.properties</value>
          <value>classpath:someprops-{environment}.properties</value>
        </list>
      </property>
    

    where environment is prod/stage/test/int/ci/local (1 per environment - you may only have 2 or 3 for now). You can pass the environment variable to the program. Any properties which should be the same regardless of if its production/running on your local pc/tests would be in the someprops.properties property file. Any ones specific to the environment/way its being run as will go in the more specific file (you should put it in the someprops.properties file as well as a default unless overridden mechanism)

    E.g. in classpath:someprops.properties

    url=www.mysite.com
    

    in classpath:someprops-local.properties

    url=localhost
    

    By using this basic idea you can separate tests and the program's normal running properties in a clean manner.

    0 讨论(0)
  • 2020-12-25 16:02

    I used benkiefer's approach, but I had to add a listener to web.xml:

    <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    0 讨论(0)
提交回复
热议问题