Thursday, June 18, 2015

How to set development/test/production environment in spring project (XML)

In real life development, a project always need different sets of  configurations for development, system test and production environments. Most common variables in these different environments are database location or jms location.

If the project uses spring and will not run in any container, the spring profiles can be used to make your life easier when switching environments. This article demonstrates the best practice to do so in my experiences to achieve these goals:

  1. Finally there will be only one packaged jar for all different environment
  2. The same jar can run in development environment by giving option  "" to JVM
  3. The same jar can run in test environment by giving option  "" to JVM
  4. The same jar can run in production environment without options to JVM
  5. The JUnit test cases can easily switch between different environments

If your project will run within web containers, should use JNDI to define the resources and inject resources into Java code.

The basic idea is put property variables in 3 different property files, namely, then use the proper file for different environment ( spring active profile).

This tutorial uses spring XML config. For tutorial using spring Java config, see here.

In this demo, there are 2 beans, a data source which connecting to different DB, and a hello world print out different string in different environment.

0. What you need

  • JDK 1.7+
  • Maven 3.2.1
  • Spring 4.1.0.RELEASE

1. Maven pom.xml





<!-- Spring -->

<!-- Database -->


<!-- Test Artifacts -->


<!-- Use Java 1.7 -->

This dependencies in this demo can be divided into 3 groups, Spring, Database connection and Unit test.

2. Define Java class

There are 2 classes. The first one is a simple hello world bean, which use a property value to say hello to.

package com.shengwang.demo;

import org.springframework.stereotype.Component;

public class HelloWorldBean {
String name;

public HelloWorldBean(String name) { = name;

public String sayHello() {
return "Hello "+name;

The second class is the main class.

package com.shengwang.demo;

import org.springframework.core.env.ConfigurableEnvironment;

public class DemoMain {

public static void main(String[] args) {

// Initial spring context with profiles
GenericXmlApplicationContext ctx= new GenericXmlApplicationContext();
ConfigurableEnvironment env = (ConfigurableEnvironment) ctx.getEnvironment();

HelloWorldBean helloWorld = ctx.getBean(HelloWorldBean.class);

In the main, spring context is created from xml configuration file. Notice that the default active profile sets to "production", so if there is no option to set active profile when running, the program  run in production environment.

3. Spring configuration

Spring configuration file spriing-context.xml, is the key of this article.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<!-- ======================================= -->
<!-- use varibles in bean definition -->
<!-- ======================================= -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />

<bean class="com.shengwang.demo.HelloWorldBean">
<constructor-arg value="${}" />

<!-- ====================================================== -->
<!-- import different variables according to active profile -->
<!-- ====================================================== -->
<beans profile="development">
location="classpath*:/META-INF/" />

<beans profile="test">
location="classpath*:/META-INF/" />

<beans profile="production">
location="classpath*:/META-INF/" />


Beside the xml header, the spring xml configuration mainly has 2 parts. The first part define 2 beans, a dataSource and a helloworldBean. The point need to mention is the placeholders, looks like ${…}, used as values in bean definitions. Where are the placeholders defined? In the property files that are imported in the second part of the configuration.  There are 3 property files represents  development/test/production environments respectively.

4. Property file

Property files are plain text files. Line starts with '#' is comment. There are 3 property files:,,  Three files have same properties but with different values.  Only 2 are listed here to save space.



Now the whole project directory hierarchy looks like.


5. Specify profile during development

As beginning mentions, in development, set JVM options in your IDE or command line.  For example in Eclipse, this can be done using menu Run->Run Configuration. This JVM option only need to be specified for run the project. There is no need to specify this JVM option for JUNIT test.


Run the project and we can see it's using the development properties.


6. Specify profile for Junit

Add @ActiveProfiles in the junit test to specify the profile. There is NO need to specify the "" JVM option for JUNIT test.

package com.shengwang.demo;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@ContextConfiguration(locations = "classpath:spring-context.xml")
public class HelloWorldBeanTest {

@Autowired HelloWorldBean helloWorld;

@Test public void testSayHello() {
assertEquals("Hello development env",helloWorld.sayHello());

Hope this article can make your life easier!


  1. can we have 2 or more property place holders in context.xml?


Powered by Blogger.

About The Author

My Photo

Has been a senior software developer, project manager for 10+ years. Dedicate himself to Alcatel-Lucent and China Telecom for delivering software solutions.


Unordered List