I have no special talent. I'm only passionately curious - Albert Einstein
Managing Application Configurations Comment on Managing Application Configurations 0

Most applications require configurations specific to the environment they are being deployed to. Java applications are meant to be written once, deployed anywhere, so developers need a mechanism that allows them to deploy a single WAR or EAR file to any environment, instead of packaging a deployment for each specific environment. Using Spring, a simple naming convention for properties files, and a single environment variable (either on the server or from the command line) we can achieve this feat.

The directory Structure

In this example, we create a directory structure under the src directory like this:

+-src
|
+-cfg
|
+-dev
|
+-model
|
+-prod

Under each environment specific directory, we place a file called application.properties. Each application.properties file contains similiar properties, all specific to the environment you would like to target on deployment.

 

Leverage the Spring Framework

Next, we create a Spring application context configuration, with a PropertyPlaceholderConfigurer as one of the first beans:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:cfg/${environment}/application.properties</value>
</property>
</bean>

You'll notice that the PropertyPlaceholderConfigurer contains an Ant-like property placeholder reference, for a variable called 'environment'. The environment variable will be passed in as a System property, either from the application server or the command line, and will be set to either dev, model, or prod. This will allow the Spring application context to pick up the correct properties file based on the System property set.

Once the properties file is selected, the PropertyPlaceholderConfigurer will allow developers to configure Spring beans using any property from the property file as the key. Again, an Ant-like property placeholder syntax is used:

	<bean id="sampleService" class="avidmalone.sandbox.service.TestService">
<constructor-arg ref="${server.url}" />
<constructor-arg ref="${auth.username}" />
<constructor-arg ref="${auth.password}" />
</bean>

Setting the System Properties

This section contains instructions that can be used to set the 'environment' System property in different application containers

Configuring Websphere 6.1

  • Login to the WAS Console
  • Navigate to Servers > Application servers > server name (e.g. server1)
  • Under Server Infrastructure section, select Java and Process Management > Process Definition
  • In the process definition screen, under Additional Properties, select Environment Entries
  • Add a new entry with the name of 'environment' (case sensitive), and give the entry the value specific to the targeted environment (dev, model, prod)

0 comments

Add a comment

Please provide your name, email address (won't be published) and a comment

About

David Malone is a Java developer residing in the Twin Cities area.  He has been developing enterprise applications since 2004.  This is his personal blog, as well as his design and development workspace.