Dark theme | Light theme

February 17, 2009

Use Spring Configurator to support different dev, test and production environments with Spring configuration files (Part 2)

In the previous part we learned about the Spring Configurator. In this part we learn how to create properties for different environments (or running modes) and how to use them.

First we add a simple String property with the name text to our Sample class:

package com.mrhaki.spring.mavenapp;

import org.apache.cocoon.configuration.Settings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

public class Sample {

    private Settings settings;

    private String text;

    public void setSettings(Settings settings) {
        this.settings = settings;

    public void setText(String name) {
        this.text = name;

    public String toString() {
        return "Running in [" + settings.getRunningMode() + "] mode. "
                + "Text = [" + text + "].";

Notice how we changed the toString method so we can see the value for the text property. Next we add the bean definition for the Sample class in our applicationContext.xml. Therefore we add the following lines after <configurator:settings/>:

<bean name="sample" class="com.mrhaki.spring.mavenapp.Sample">
 <property name="text" value="${sample.text}"/>

At line 2 we assign the text property a variable value, which is not set yet. The value will be set if we run the application and is read from a property file. We can now use the Spring Configurator to define a different value for sample.text for every running mode we want to support. For this example we assume a dev and test running mode. If we read the Spring Configuration manual we notice we can place the property files at many locations. This is what makes the Spring Configurator so flexible and useful.

First we create in the src/main/resources directory the following directories: META-INF/cocoon/properties/dev and META-INF/cocoon/properties/test. We create a file in the src/main/resources/META-INF/cocoon/properties/dev directory. In the file we define:

sample.text=We are developing!

We save the file and if we run mvn test -Dorg.apache.cocoon.mode=dev we get the following output:

Running in [dev] mode. Text = [We are developing!].

The Spring Configurator has replaced our variable sample.text with the value from the property file. We also create a file in the src/main/resources/META-INF/cocoon/properties/test directory and give a different value for the property:

sample.text=Ready for testing!

We run the test again, but now use the test running mode:mvn test -Dorg.apache.cocoon.mode=test. And we get this output:

Running in [test] mode. Text = [Ready for testing!]

Finally we create a in the src/main/resources/META-INF/cocoon/properties directory with a default value, which is used by the default running mode (prod):

sample.text=We are alive!

We run mvn test and we see in the output:

Running in [prod] mode. Text = [We are alive!].

So in this part we have learned how to use property files for different running modes to replace variables in the Spring configuration files. In the next part we learn how override properties of beans defined in Spring configuration files.