Announcement Announcement Module
Collapse
No announcement yet.
Overriding PropertyPlaceHolderConfigurer locations from system env Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Overriding PropertyPlaceHolderConfigurer locations from system env

    Hi,

    I would like to keep default properties file in my app jar (under src/main/resources) and be able to override this location, if the JVM is being ran with a specific var such as -Dapp.conf.dir=//opt/some/dir.

    I've managed to get this behavior by ordering the locations array. it seems that the latter location will override the previous. But since this is a dangerous un-documented behavior I would prefer something more "safe".
    Before I extend PropertyPlaceHolderConfigurer to process a "classpath:/" resource only if there's no "file:/" resource, is there anything I'm missing in spring which gives this behavior out of the box?

    so the required behavior is:
    - verify if there's a ${app.conf.dir} set as system property
    - if there is, load ${app.conf.dir}/*.properties
    -- else load classpath:/*.properties
    here's an excerpt from my context.xml:

    Code:
    <bean id="propertiesConfigurer"
    		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="ignoreUnresolvablePlaceholders" value="true" />
    		<property name="searchSystemEnvironment" value="true" />
    		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    		<property name="locations">
    			<list>
    				<value>classpath:/META-INF/*.properties</value>
    				<!-- this happens to override previous properties due to the implementation... -->
    				<value>file:/${app.config.dir}/*.properties</value>
    			</list>
    		</property>
    	</bean>

  • #2
    Impala has an implementation of this called SystemPropertyBasedPlaceholderConfigurer:

    http://impala.googlecode.com/svn/tru...onfigurer.java

    Something to note with this implementation is that it will only use the locations specified in the file system folder for overrides. In other words, you only need to add into the file in the file system properties folder the values that you want to apply differently from the values sitting in the file on the classpath.

    Phil Zoio
    Impala - simple dynamic modules for Spring
    http://www.impalaframework.org/
    http://impalablog.blogspot.com/

    Comment


    • #3
      Originally posted by yarinb View Post
      dangerous un-documented behavior I would prefer something more "safe".
      As far as I know it has always been the case and always will be, that locations listed later ovveride those listed earlier: all Spring is doing is loading properties and doing a putAll() with them.

      Before I extend PropertyPlaceHolderConfigurer to process a "classpath:/" resource only if there's no "file:/" resource, is there anything I'm missing in spring which gives this behavior out of the box?
      Personally I'd be quite happy with what you have. You could tidy it up by using the new features in the context: namespace in Spring 3.0, but it's basically sound.

      Or if you are nervous about the ordering, you could use 2 instances of PropertyPlaceHolderConfigurer, one that loads the files from location specified a system property and one with the hard coded path, and add order properties to each one.

      Comment

      Working...
      X