0

I'd like to allow my Spring 3.1.2 application to load configuration properties from a default properties file embedded within my jar and additionally allow the user to specify the path to an overriding properties file as a command line parameter.

I understand I can use <context:property-placeholder> for the simple scenario of loading the properties from my class path, but how might I handle the scenario above with properties from potentially two merged property files?

The scenario I'm trying to duplicate is basically that addressed by the CompositeConfiguration of Apache Commons Configuration.

HolySamosa
  • 9,011
  • 14
  • 69
  • 102

1 Answers1

3

You can add properties file name via system properties

Check this

how to read System environment variable in Spring applicationContext

http://www.summa-tech.com/blog/2009/04/20/6-tips-for-managing-property-files-with-spring/

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/expressions.html#expressions-beandef-xml-based

http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.html

UPD

1 . The first way to go is to declare PSPC as

      <context:property-placeholder 
         location="classpath:app.properties, classpath:user.properties"
         ignore-resource-not-found="true" />

Then you include your app.properties into jar.

User includes (or not) a folder containing user.properties into the classpath.

user.properties takes precedence over app.properties.

2 . If you need user to specify the exact file

    <context:property-placeholder
    location="classpath:app.properties, file:${userPropsPath}"
    ignore-resource-not-found="true" />

User adds -DuserPropsPath="<full path here>"


Both cases are working and tested with spring-3.1.1.

Community
  • 1
  • 1
Vitaly
  • 2,760
  • 2
  • 19
  • 26
  • Thanks for the answer, but one important element is missing- how can the two property files (one supplied as an argument to the app, the other with default values in a resource) be merged or treated as a composite? The scenario I'm trying to duplicate is basically that addressed by the `CompositeConfiguration` of Apache Commons Configuration. – HolySamosa Apr 12 '13 at 22:10
  • I wasn't working over the weekend, so this was my first opportunity to review your most excellent answer. Just what I was looking for. Thanks! – HolySamosa Apr 15 '13 at 18:59