juretta.com

Using Ant-style properties in Spring Bean configuration files

February 10, 2007
Tags: Java Spring

Being a long time ant user i always wanted to use Ant-style properties in my Spring bean definitions. And no surprise here - this is quite easy to accomplish.

Spring has the PropertyPlaceholderConfigurer class which takes a Java properties file and replaces ${} placeholders in your XML based bean definition files.

A very simple examples shows how to use the property placeholders.

This is the directory layout in my java source folder:

macbook-sts:~ stefan$ dP src 
src
  \-com
    \-juretta
      \-spring
        \-example:
          MyBean.java
src
  \-example:
    Runner.java
.
  beans.xml
  configuration.properties 
lib:
  commons-logging.jar
  spring.jar

You will need the spring.jar and at least commons-logging.jar to successfully compile and run this very simple example. You are advised to download the spring-framework-with-dependencies zip file as it contains every library you might need. You can get the Spring framework here.

The simple example has only one Spring bean. The Spring bean has one String property that will be set through spring dependency injection. For this property a property placeholder will be used.

Files used in this example: a Pojo Bean "com.juretta.spring.example.MyBean", a Runner class (example.Runner), the xml bean definition file (beans.xml) and a simple property file "configuration.properties"

macbook-sts:~ stefan$ cat src/configuration.properties 
# An example path
conf.path=/usr/local/tomcat/conf

src/beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

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

  </bean>

  <bean id="myBean" class="com.juretta.spring.example.MyBean">
    <property name="configurationPath" value="${conf.path}" />

  </bean>
</beans>

The single bean has just a setter and getter for one string property. src/com/juretta/spring/example/MyBean.java

package com.juretta.spring.example;

/**
 * @author Stefan Saasen <s@juretta.com>

 */
public final class MyBean {
  private String configurationPath;

  public String getConfigurationPath() {
    return configurationPath;
  }

  public void setConfigurationPath(final String configurationPath) {
    this.configurationPath = configurationPath;

  }

  @Override
  public String toString() {
    return new StringBuilder("MyBean:\n")

    .append("\t")
    .append("configurationPath=")
    .append(this.configurationPath)

    .append("\n\t")
    .append("super.toString: ")
    .append(super.toString()).toString();
  }
}
package example; 

import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.juretta.spring.example.MyBean; 
import static java.lang.System.out; 

public final class Runner {  
  /**  
  * @param args  
  */  
  public static void main(String[] args) {  
    // create a Spring application context  
    ClassPathXmlApplicationContext context =  
		new ClassPathXmlApplicationContext("beans.xml");  

    // get the Spring Bean with id "myBean" 
    MyBean bean = (MyBean) context.getBean("myBean");  

    out.println(bean.getConfigurationPath());  
    out.println(bean.toString());  
  } // main 
}

To compile and run the program one would usually use ant or the Eclipse IDE. The following commands are compiling and running the simple example project:

Running the simple example shows that the property placeholder ${conf.path} is now the value for conf.path as defined in configuration.properties.

macbook-sts:~ stefan$ cd simple-example
# this direcotry has the following subdirectories: src, bin and lib

macbook-sts:~ stefan$ cp src/beans.xml bin; cp src/configuration.* bin

# compile 
macbook-sts:~ stefan$ javac -cp .:lib/spring.jar:lib/commons-logging.jar \
    -d bin src/example/Runner.java \
    src/com/juretta/spring/example/MyBean.java

# and run
macbook-sts:~ stefan$ java -cp \
.:lib/spring.jar:lib/commons-logging.jar:bin \
example/Runner

10.02.2007 15:16:45 org.springframework.core.CollectionFactory <clinit>
INFO: JDK 1.4+ collections available
10.02.2007 15:16:45 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
[... more log information ...]
MyBean:
        configurationPath=/usr/local/tomcat/conf
        super.toString: com.juretta.spring.example.MyBean@5cc942

About

This is the defunct blog of Stefan Saasen.