Nov 13
Quick Tip: Spring 2.5 makes use of PropertyPlaceholderConfigurer simpler
While not explicitly highlighted in the Spring 2.5 “What’s New” section, this is a nice little bit to remove a few lines of XML. Spring 2.5 (with the introduction of a new “context:” namespace) takes the common use of a PropertyPlaceholderConfigurer externalizing properties and folds it from the awkward 5-line syntax into a single line.
Before (Spring 2.0 and earlier):
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>file:${user.home}/env.properties</value>
</property>
</bean>
After (Spring 2.5):
<context:property-placeholder location="file:${user.home}/env.properties"/>
The nit-picky will note that you will need to add in the spring-context-2.5.xsd schema reference, however you may be using that already anyway in Spring 2.5 to take advantage of some of the Java-based annotation functionality.
Enjoy!



December 4th, 2007 at 4:03 pm
you seriously still use the … syntax still? try
December 4th, 2007 at 4:04 pm
oh and fix your blog to quote > and < rather than removing them
December 4th, 2007 at 4:12 pm
Thanks, I’ll look into that issue. Unfortunately it seems that the actual purpose of your comment was lost. What was it you were trying to say?
December 4th, 2007 at 4:42 pm
Seems to be fixed. Please make sure to wrap your code with
<code>tags.December 5th, 2007 at 1:15 pm
use <property name="..." value="..."/> instead of <property name="..."><value>...</value></property>December 5th, 2007 at 1:50 pm
Thanks for the follow-up.
Actually, I had pulled this particular example out of the Spring 2.5 documentation (3.7.2.1. Example: the PropertyPlaceholderConfigurer), which uses the legacy
<value>...</value>syntax.I agree that there are more elegant ways to set property values, and in the projects where I’ve used Spring, I’ve used the ‘value=’ syntax, myself (I am not a fan of the p: namespace syntax, FWIW).