Issue Details (XML | Word | Printable)

Key: SPR-1876
Type: New Feature New Feature
Status: Open Open
Priority: Minor Minor
Assignee: Juergen Hoeller
Reporter: Chris Lee
Votes: 16
Watchers: 16
Operations

If you were logged in you would be able to see more operations.
Spring Framework

Extend PropertyPlaceholderConfigurer to choose property file based on runtime environment

Created: 10/Apr/06 03:27 PM   Updated: 26/Aug/08 08:40 PM
Component/s: SpringCORE
Affects Version/s: 2.1 M4
Fix Version/s: 3.0 M2

Time Tracking:
Not Specified

File Attachments: 1. Java Source File RuntimeEnvironmentPropertiesConfigurer.java (6 kB)
2. Zip Archive RuntimeEnvironmentPropertiesConfigurer.zip (5 kB)
3. Zip Archive RuntimeEnvironmentPropertiesConfigurer.zip (3 kB)



 Description  « Hide

PropertyPlaceholderConfigurer can be readily extended to choose the property file to read at runtime, based on the current runtime environment. This simplifies moving the same deployment package (EAR/WAR) between environments w/o having to rebuild (saving time, reducing risk).

See the attached file for a reference implementation (feel free to adjust as necessary).

 All   Comments   Work Log   Change History   FishEye   Related Builds      Sort Order: Ascending order - Click to sort in descending order
Chris Lee added a comment - 10/Apr/06 03:28 PM
Reference implements - RuntimeEnvironmentPropertiesConfigurer

Fabio Grassi added a comment - 11/Apr/06 01:22 AM
Chris has a good point here. I resolved the very same use case he describes having MethodInvokingFactoryBean call System.getProperty and using the returned value to set the location property of PropertyPlaceholderConfigurer.

His solution is obviously much cleaner and at the same time easy to implement and doesn't add too much complexity.

I hope for the adoption of his proposal.

Ciao, Fabio.

David Medinets added a comment - 12/Apr/06 08:37 PM
I suggest extending the concept even farther:

 - read the configuration name (ie, dev, staging, production) from a text file.
 - read the configuration value from a URL.
 - read the configuration value from a database (ie, provide a datasource and SQL statement)

Chris Lee added a comment - 12/Apr/06 08:45 PM
I'll be posting a v2 with a pluggable RuntimeEnvironmentKeyResolver; the default implementation will be equivalent to what is there now. This will allow users to provide their own implementation(s), resolving the current environment in an appropriate manner.

Chris.

Chris Lee added a comment - 12/Apr/06 08:58 PM
Version 2 is attached; this adds an (optional) RuntimeEnvironmentKeyResolver interface, allowing for custom implementations that determine the runtime environment in an appropriate manner. The default implementation still uses a system property.

Peter Monks added a comment - 13/Apr/06 12:38 PM
The following thread goes into a bit more detail about why this change is important: http://forum.springframework.org/showthread.php?t=24066

Peter Monks added a comment - 13/Apr/06 05:38 PM
Added an environment aware version of ClassPathXmlApplicationContext and another RuntimeEnvironmentKeyResolver implementation.

Sivan Mozes added a comment - 20/Jul/06 06:21 PM
RuntimeEnvironmentPropertiesConfigurator line 117, empty while loop due to semi-colon

Sivan Mozes added a comment - 20/Jul/06 06:26 PM
The above is the 1.4 backport only. Please disregard.

Alex Wei added a comment - 31/Jul/06 09:02 PM
Another approach is to use an EagerPropertyPlaceholderConfigurer to load the properties file specified by a system property.

Please see the link of a related issue:
[link]http://opensource.atlassian.com/projects/spring/browse/SPR-1076[/link]

Chris Lee added a comment - 01/Aug/06 10:51 AM
EagerPropertyPlaceholderConfigurer is a orthogonal approach to resolving properties; would be nice to merge the two concepts, so that we have the ability to eagerly load properties and specify (with validation) which environment to load properties for.

Kambiz Darabi added a comment - 15/Sep/06 12:19 AM

Peter Monks added a comment - 15/Sep/06 04:36 PM
Keep in mind that the problem stated here (and the proposed solution) covers more than just property differences between environments - specifically it also covers differences in bean wirings between environments (something that property placeholders do not support by themselves).

This is a reasonably common use case; eg.
* developer sandbox environments have mock versions of DAOs / web service clients / what-have-you configured but other environments are configured with the real thing
* integration test environment has a series of "validation shims" (akin to JDK 1.4+ asserts, only more sophisticated) configured but other environments don't
* performance test environment has a series of "timing shims" configured but other environments don't
etc. etc.


Alin Dreghiciu added a comment - 19/Sep/06 05:12 AM

Clint Shank added a comment - 27/Jan/08 11:01 AM
As an alternative, what about specifying all the properties in a single file. I like this from a maintainer's perspective. All properties are in one file. You could have default properties and override them by environment, only if necessary. The properties file would look like:

# this is the default value for all environments
my.prop=defaultValue
# but in the production environment, it is different
[production].my.prop=productionValue


# here's another property with no default
[development].my.prop2=someDevelopmentValue
[test].my.prop2=someTestValue
[production].my.prop2=someProductionValue


The beandef would specify only the property name minus the environment and the Configurator would resolve appropriately:

"${my.prop}"

Peter Monks added a comment - 03/Mar/08 04:30 PM
Makes perfect sense in the case where the set of properties isn't too large, although you can still get the same defaulting behaviour using Chris' original implementation (although using multiple files, rather than a single file as you suggest).

If you implement this, would you mind using the same environment key resolver infrastructure? That just makes it easier for users of this functionality to mix and match the various approaches without having to change the way their environments are identified.

Bob Tiernay added a comment - 26/Aug/08 08:02 PM
I think this should be implemented in the core. Juergen, where you at on this one buddy :)

I have found at least 12-15 articles about this very issue, and Spring needs an official implementation to address it IMO. This is a great start guys!

Anyways, support for multiple properties files with a configurable convention would be nice too. This way you could have something like:

jdbc.development.properties
jdbc.staging.properties
jdbc.production.properties

Sometimes this is needed due to framework constraints while trying to satisfy the DRY principle.


Antony Stubbs added a comment - 26/Aug/08 08:39 PM
This is definitely required.

Here's my take on the subject from yonks ago:
http://forum.springframework.org/showthread.php?t=46504