RSS feed
<< A better way to configure Java Web Applications | Home | Packaging Java apps for OS X with Maven >>

Better webapp config: Spring integration and an example


I promised following up my post about a better way to configure Java web applications with an example. The key point to take home from that post was that:

We should move all configuration out of the [Java] webapp, treat configuration as just another type of data and stick it in a directory that we point our webapp at.

Now for the example: There are two things you need to set up.The first part is to configure your application so that it will be able to find it's data directory. The second part is to configure your application server to point at the correct directory.

Preparing your app

If your application is using Spring all you need to do is to add the following line of XML to your bean context:

<bean class="com.simplericity.datadirlocator.spring.DataDirectoryPropertyReplacer"/>

That's all there is to it. The DataDirectoryPropertyReplacer will  configure a DefaultDataDirectoryLocator and replace any references to ${dataDir} in your Spring context with whatever is returned by the locateDataDirectory() method. Lets say you  have an UploadFileController and it needs to  know where to put  uploaded files.  Then configure your controller like this:

<bean class="com.example.UploadController" id="uploadController">
 <property name="uploadDirectory" value="${dataDir}/uploads"/>
</bean>

The defaults should work fine in most cases, but there are quite a few options if you'd like a more customized soluton. The first thing to do is to configure a custom DefaultDataDirectoryLocator:

<bean class="com.simplericity.datadirlocator.spring.DataDirectoryPropertyReplacer">
<bean id="dataDirLocator" class="com.simplericity.datadirlocator.spring.ServletContextAwareDataDirectoryLocator">
<property name="defaultDir" value="$HOME/.myappdata"/>
</bean>
</bean>

This example configures the fallback data directory to be the .myappdata directory inside the user's home directory. There are other properties you can set to override the various property names the locator will look for. See the source Javadocs
for full details.

Configuring your app server

There seems to be as many ways to do this as there are app servers, so I'll show you how to do it on Jetty and Tomcat and leave the rest as an exercise.

Using a JDNI property on Jetty

Add this to your Jetty configuration:

<New id="dataDir"  class="org.mortbay.jetty.plus.naming.EnvEntry">
    <Arg>dataDirectory</Arg>
    <Arg type="java.lang.String">/var/local/myapp/</Arg
    <Arg type="boolean">true</Arg>
</New>

Using a JNDI propery on Tomcat:

Add this inside your webapp's Context tag:

<Environment name="dataDirectory" value="/var/local/myapp"
      type="java.lang.String" override="true"/>


Using a Context param on Jetty:

Add this to your jetty-web.xml:

<Set name="initParams">
    <New class="java.util.HashMap">
        <Put name="dataDirectory">/var/local/myapp/</Put>
    </New>
</Set>


Setting a system property
This should be easy to do on any app server using the -DdataDirectory=/var/local/myapp command line switch.


Adding property file to the classpath

Add a file called myapp.properties to the WEB-INF/classes directory containing the following property:

dataDir=/var/local/myapp

There is a complete webapp example in the Simplericity Subversion repository.
Tags :



Add a comment Send a TrackBack