jeremy303
jeremy303

Reputation: 9241

Default and user-supplied property files w/ Spring

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.

Upvotes: 0

Views: 1574

Answers (1)

Vitaly
Vitaly

Reputation: 2790

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.

Upvotes: 3

Related Questions