hoodwink.d enhanced
 

juretta.com

Using Ant-style properties in Spring Bean configuration files | February 10, 2007-->

February 10, 2007

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 class MyBean {
	private String configurationPath;

	public String getConfigurationPath() {
		return configurationPath;
	}

	public void setConfigurationPath(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 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" Object bean = context.getBean("myBean"); out.println(((MyBean)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 
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
@15:30 | Comments: 3 | Tags: Java (13), Spring (1)

Diggman

1
Alef Arendsen says:
Avatar Sun Feb 11 12:03:42 +0100 2007 | #

Good article. Note that Spring also provides a PropertyOverrideConfigurer, allowing you to override a certain bean property. This works nicely with defaults (for development mode) that can be overriden in production mode for example.
regards,
Alef Arendsen

2
Peter Fork says:
Avatar Mon Apr 09 13:42:53 +0200 2007 | #

Interesting post! Just what i was looking for!

3
João Simas says:
Avatar Thu Jun 28 06:25:02 +0200 2007 | #

Exactly what i want. Thanks!

About

juretta.com is the personal workspace of Stefan Saasen. You can send him an email or read more about this site in the „About“ section.

« Previous entry

Mac OS X command line goodies
posted about 1 year ago

» Next entry

List image dimensions
posted about 1 year ago

Recent comment

On: “Attachr.com: OpenID support added

You need to kill this spam stuff!

posted about 1 year ago by entropie

Look!

Latest links  RSS  

More...