I am working on a product suite which has 4 products. Right now, all of the configuration data is either in the XML or properties files.This approach is not maintainable as
For all of our environments, configuration data lives on the target machines in the form of properties files. We use PropertyPlaceholderconfigurer from SpringFramework to bind these properties to our apps to keep things portable accross environments.
For example, as long as I know that /etc/myapp/database.properties will be present on whatever machine my app will be running on, then in my spring configuration, I just need something like so:
<bean id="myPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/etc/myapp/database.properties</value>
</list>
</property>
</bean>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://${db.host}:3306/${db.name}" />
<property name="username" value="${db.user}" />
<property name="password" value="${db.pass}" />
</bean>
There are a bunch of options for that Spring class about where properties files can live. You can even make them substitutions and pass them in as environment variables:
<bean id="myPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="searchSystemEnvironment" value="true" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="locations">
<list>
<value>${database.configuration.file.url}</value>
</list>
</property>
</bean>
And in bash_profile (or whatever): export JAVA_OPTS="-Ddatabase.configuration.file.url=file:///etc/myapp/database.properties"
Or just the same -D option passed in when you call "java" depending on what you are doing.
FWIW, we maintain our properties files separately as RPMs.
Here is an keep simple stupid solution.
The code snippet, which try to use the properties file in the current directory first. If failed, use the properties file in resource directory (or in the jar file) instead.
Properties configFile = new Properties();
try {
configFile.load(new FileInputStream("production.properties"));
} catch (Exception e) {
System.err.println("Can not read properties, try to use default properties file.");
configFile.load(this.getClass().getClassLoader().
getResourceAsStream("development.properties"));
}