juretta.com

Using Ant-style properties in Spring Bean configuration files

February 10, 2007
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:

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"

src/beans.xml

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

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.


About

This is the defunct blog of Stefan Saasen.

Ads