Nov 13

Quick Tip: Spring 2.5 makes use of PropertyPlaceholderConfigurer simpler

Tag: java,springpmularien @ 8:09 pm

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!

Similar Posts:

6 Responses to “Quick Tip: Spring 2.5 makes use of PropertyPlaceholderConfigurer simpler”

  1. Jason says:

    you seriously still use the … syntax still? try

  2. Jason says:

    oh and fix your blog to quote > and < rather than removing them

  3. pmularien says:

    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?

  4. pmularien says:

    Seems to be fixed. Please make sure to wrap your code with <code> tags.

  5. Jason says:

    use <property name="..." value="..."/> instead of <property name="..."><value>...</value></property>

  6. pmularien says:

    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).

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">