Friday, April 11, 2008

Properties in Spring: PropertyPlaceholderConfigurer and PropertyOverrideConfigurer

The Spring Framework's PropertyPlaceholderConfigurer and PropertyOverrideConfigurer make using Java .properties files with Spring easy. In this blog entry, I'll use a simple example to demonstrate these two classes that enable configuration of Spring via .properties files.

The first code listing is a simple Java class that will be exposed by Spring and will have its attributes set based on Java properties files.


package dustin;

/**
* Simple Java class that will be exposed as a Spring bean and configured via
* properties files.
*/
public class SpringPropertiesHandlingExample implements SpringPropertiesHandlingIf
{
private String valueFromConfigurer;
private String valueFromOverrider;

public SpringPropertiesHandlingExample()
{
}

public String getValueFromConfigurer()
{
return this.valueFromConfigurer;
}

public void setValueFromConfigurer(final String valueFromConfigurer)
{
this.valueFromConfigurer = valueFromConfigurer;
}

public String getValueFromOverrider()
{
return this.valueFromOverrider;
}

public void setValueFromOverrider(final String valueFromOverrider)
{
this.valueFromOverrider = valueFromOverrider;
}
}


The interface for the above class is simple and shown next.


package dustin;

/**
* Interface for simple Java class configured in Spring via properties files.
*/
public interface SpringPropertiesHandlingIf
{
String getValueFromConfigurer();

String getValueFromOverrider();

void setValueFromConfigurer(final String valueFromConfigurer);

void setValueFromOverrider(final String valueFromOverrider);
}


The next code listing is the executable Java class that bootstraps the Spring container used in this example.


package dustin;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* Main executable starting Spring Container.
*/
public class SpringPropertiesMain
{
/**
* Main executable starting Spring Container.
*
* @param arguments Command-line arguments; none anticipated.
*/
public static void main(final String arguments[])
{
final ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("/spring-properties-example.xml");
final SpringPropertiesHandlingIf propertiesExample =
(SpringPropertiesHandlingExample)
context.getBean("SpringPropertiesHandlingBean");
System.err.println( "Configurer Value: "
+ propertiesExample.getValueFromConfigurer());
System.err.println( "Overrider Value: "
+ propertiesExample.getValueFromOverrider());
}
}


This simple main class instantiates the Spring container. It then displays its attributes as set in the XML configuration file (called spring-properties-example.xml and shown next).


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

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:locations="classpath:/spring-properties-configure.properties" />

<bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer"
p:locations="classpath:/spring-properties-override.properties" />

<bean id="SpringPropertiesHandlingBean"
class="dustin.SpringPropertiesHandlingExample"
p:valueFromConfigurer="${value.configurer}"
p:valueFromOverrider="yadayadayada" />
</beans>


In the Spring XML configuration shown above, the highlighted portion contains use of the PropertyPlaceholderConfigurer and the PropertyOverrideConfigurer.

The Spring XML above points at two properties files. They are simple in this example and are shown next.

spring-properties-configure.properties

value.configurer=Hello, World!
value.overrider=Hello, Spring!


spring-properties-override.properties

SpringPropertiesHandlingBean.valueFromOverrider=Overridden!


The first properties file is used to set the two values of the simple Java class and the second properties files overrides the value of the second of the two values of that Java class.

The output from running this looks like this:


Configurer Value: Hello, World!
Overrider Value: Overridden!


If I comment out the single line in the spring-properties-override.properties file so that it doesn't do any overriding, the output from running this now looks like this:


Configurer Value: Hello, World!
Overrider Value: yadayadayada


These two pieces of output show that the PropertyPlaceholderConfigurer configures the values in the Spring application based on properties files and the PropertyOverrideConfigurer allows these values to be overridden. Of course, you could override hard-coded values in the XML as well, but I thought it was interesting to see both the original values set with PropertyPlaceholderConfigurer and then see them overridden with PropertyOverrideConfigurer.

1 comment:

J. Jahir said...

thank you very much.
I served much your example.