Search code examples
springapache-commonsapache-commons-config

Good example of Spring Configuration using java.util.prefs or Commons Configuration


One application I'm working on has several URLs and other information that is instance specific. The first pass uses a typical Spring PropertyPlaceholderConfigurer with a properties file:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:application.properties"/>
</bean>

The main issue with this is of course the property file is an artifact that must be checked in, and for starting a new instance would require updating that artifact. For a streamline deployment, I would like to have the ApplicationContext bootstrap itself based on database table(s). I have seen solutions like this forum post, does anyone here know of better tools or is this defacto approach to this problem? I would also like to be able to update/reload the settings at runtime using JMX or other facilities, but having to restart the app after changes to the database would still be a better solution to the current one.


Solution

  • The way we did it was to put some configuration information in the environment and then pull the relevant info from there.

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="searchSystemEnvironment" value="true" />
    </bean>
    

    If configuration changes then the app will need to be restarted. Can also put all the different configurations into the environment and nest the variables like the following:

    <bean id="db" class="org.DataSource"
            p:databaseServer="${${MODE}_DBSERVER}"
            p:databaseName="${${MODE}_DBNAME}" />
    

    where $MODE = dev, qa, etc.