Question

Play!Framework provides a very good feature that enable you to configure different set of configurations, hence I could have something like

storage=fs
storage.fs.home.dir=...
storage.fs.home.url=...
...

%at.storage=s3
%at.storage.s3.key=...
%at.storage.s3.secret=...
...

%prod.storage=s3
%prod.storage.s3.key=...
%prod.storage.s3.secret=...
...

By default the application is use fs (filesystem) storage, if application started using --%at (acceptance test) mode, it uses aws s3 as storage implementation, and if application started with --%prod, the production mode, it also use s3 storage but could with different S3 configuration. This feature makes it very easy to version control the application configuration file, you don't need to update configuration file when deploy to a live server or acceptance testing server.

I am curious has anyone implemented certain mechanism for a Spring framework based application.

Was it helpful?

Solution

Try it like this, create different directories in the root of the classpath, containing the configuration of the different environments. Keep the same file names:

.
|____pom.xml
|____src
| |____main
| | |____resources
| | | |____dev 
| | | | |____application.properties
| | | |____test 
| | | | |____application.properties
| | | |____prod 
| | | | |____application.properties

Then at application startup, pass in an environment variable that says which environment to use. For example:

-Denv=prod

configure the loading of a given properties file depending on the environment variable using util:properties like this:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd">

    <util:properties id="application" location="classpath:${env}/application.properties"/>

    ... remaining config here

</beans>

OTHER TIPS

Have a look at Spring's Profiles.

In my applications I usually have a default.properties and something like dev.properties which overrides some values from default.properties. I enable this with ProfileResourceProvider.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top