Hai All,
finally a new technical item. It is a short item, but I have been struggling to get this to work. Therefore I want to share this so other can use it to configure there properties in a flexible way.

Properties like database url’s, userid’s and pasword’s are different in your test environment and your production environment. There are other items you might want to configure.

You can make complicated deployment scripts that get the right property files for the right environment. But you need to create different packages for different environments. I think a more elegant solution is to use the JNDI parameters to load the specific environmental values. If there are a lot a good way might be to store them in a database and only configure the database access parameters using JNDI.

The following solution is based on two posts in different forums:
http://forum.java.sun.com/thread.jspa?threadID=647327&messageID=3810991 – gives details about the configuration of tomcat.
http://forum.springframework.org/showthread.php?t=30991 – gives details about the spring configuration

Now the code, we start with the tomcat configuration. The server.xml file contains all the actual values connected to the exposed JNDI parameter names.


  <!-- Global JNDI resources -->
    <!-- Test entry for demonstration purposes -->
    <Environment name="jdbc/username" value="myUser" type="java.lang.String"/>
    <Environment name="jdbc/password" value="myPwd" type="java.lang.String"/>
    <Environment name="jdbc/url" value="jdbc:mysql://servername/schema" type="java.lang.String"/>
    <Environment name="jdbc/drivername" value="com.mysql.jdbc.Driver" type="java.lang.String"/>

We need to tell the application the names of the parameters that are available. We must add the following items to the web configuration file.



Of course you need to do this for all four parameters. Then the tricky part. This is the one I missed while getting this to work. You need to add a tomcat specific deployment configuration with the following lines


<Context path="/webapp">
    <ResourceLink name="jdbc/username" global="jdbc/username" type="java.lang.String"/>
    <ResourceLink name="jdbc/password" global="jdbc/password" type="java.lang.String"/>
    <ResourceLink name="jdbc/url" global="jdbc/url" type="java.lang.String"/>
    <ResourceLink name="jdbc/drivername" global="jdbc/drivername" type="java.lang.String"/>

Thats is all we need to do to make these values available to the spring beans. So the last part is the spring configuration. We are going to create a PropertyPlaceHolderConfigurer. That way you can access the properties just like properties from a file.


    <bean id="databaseprops" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="properties">
            <bean class="java.util.Properties">
                        <entry key="database.username">
                            <jee:jndi-lookup jndi-name="java:comp/env/jdbc/username"/>
                        <entry key="database.password">
                            <jee:jndi-lookup jndi-name="java:comp/env/jdbc/password"/>
                        <entry key="database.url">
                            <jee:jndi-lookup jndi-name="java:comp/env/jdbc/url"/>
                        <entry key="database.drivername">
                            <jee:jndi-lookup jndi-name="java:comp/env/jdbc/drivername"/>
    <bean id="sampleConfigBean" class="nl.gridshore.samples.config.SampleConfiguredBean">
        <property name="username" value="${database.username}"/>
        <property name="password" value="${database.password}"/>
        <property name="url" value="${database.url}"/>
        <property name="drivername" value="${database.drivername}"/>

This made my deployment life a lot easier, hope you will find some good use of it. Thanks again to contributers on the forum for making the different parts clear to me.

Till next time, which is going to be a piece about JPA.

greetz Jettro Coenradie

Loading properties using JNDI within the springframework deployed on tomcat