Finding dependencies with JarJar Sun getting in the way again? FreeBSD just wants a JVM :)
Jan 03

More DRY with Spring ServletContextPropertyPlaceholderConfigurer

Tech Add comments

I always hate it when I see a project with confi settings in env variables, properties files, and the myriad of XML deployment descriptors.

Spring has helped out a lot with its PropertyPlaceholderConfigurer that allows you to put everything in one place (like a properties file or three) and suck them out into ${variables}.

How about getting values into something like a web.xml?

One generic solution is to use your build system do the work for you.

With ant, you can use filters and when you copy over certain files, ant can do the s/SOME_VALUE/the real value/g.

E.g.

Turn on filtering when you copy

<copy todir=”${build.web.dir}/WEB-INF/classes” filtering=”true”>
<fileset dir=”${resources.dir}”/>
</copy>

Setup tokens to filter on

<filter token=”smtpHost” value=”${smtpHost}”/>

Juergen just added a new class to what will be Spring 1.1.4: ServletContextPropertyPlaceholderConfigurer.

This class looks into the web.xml for <context-param>’s if the ${value} isn’t found. This isn’t the same as the other solution, as the core value is now in the web.xml, rather than having everything in ONE place, and pushing that to the other config files.

2 Responses to “More DRY with Spring ServletContextPropertyPlaceholderConfigurer”

  1. Jason Boutwell Says:

    Ant has a built-in property filter that works MUCH better than manually filtering tokens.

    <property file=”spring.properties”/>
    <copy todir=”${test.build.dir}” overwrite=”true”>
    <fileset dir=”${conf.dir}/spring/test” includes=”*Context.xml”/>
    <filterchain>
    <expandproperties/>
    </filterchain>
    </copy>

  2. doom3 Says:

    useful information)))

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: Type in the word 'ajax'