Sunday, February 7, 2016

Spring externalize application configuration

To configure your application is different environment using different setup use the following in Spring framework -
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:default.properties</value>
                <value>file:${APPLICATION_CONFIG_HOME}/application.properties</value>
            </list>
        </property>
        <property name="ignoreResourceNotFound" value="true" />
        <property name="searchSystemEnvironment" value="true" />
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    </bean>
By default it will read the default.properties available in your application classpath. Typically it is available at src/main/resources/default.proerties.

Setup environment variable APPLICATION_CONFIG_HOME which points to directory where customer application.properties is available. If the file is available then it will override those properties which you have specified in that file.

For local development user need not required to define this as mostly developers will use the default.properties.

This setup provies an option to override if required in a particular environment e.g. QA, performance, staging, production, etc.

No comments:

Post a Comment