Using the Spring Framework and Java to Configure Development Evironments

Configuring separate development, QA and production connection configurations for a software project is a task that has many different solutions.  My solution had been to store a Spring configuration file on the application server and load DataSources from it programmatically, using the $Catalina_Home system property to locate the base directory.  This always felt a little hackish to me so, for a new product we’re developing, I set out to find a better way.

I didn’t have to look far.  The article linked below describes an excellent solution that keeps all configuration files inside the project and uses Java Options set on the server to identify the environment.

Enterprise Deployment Environment Configuration with Java Spring.

My solution differs slightly from the article in that I’m using Spring’s property-placeholder to resolve values in my property files.  In my source packages, I keep three files:

  • dev.jdbc.properties
  • qa.jdbc.properties
  • prod.jdbc.properties

These files, as you would expect, store the appropriate jdbc connection parameters for the intended development environment.  Obviously, I could create addional properties files for environment sensitive information such as web service URLs.

I’m using Tomcat on my development computer (just because it’s faster for what I’m doing) and deploying to Glassfish.  I pass the Java Option -Denv=dev (or -Denv=qa, or -Denv=prod) as a start up VM option (set up in intelliJ’s run configuration) but I could just as easily set it up in the Catalina.properties file.  For glassfish, I set the JVM option using the admin tool on port: 4848.  In the JVM settings tab of the Application Server menu, there is gui that makes this step trivial.

I have my DataSource bean configured in my applicationContext-business.xml Spring files (not my WebApplication context file)

<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
<!-- (in this case, JDBC-related settings for the dataSource definition below) -->

<context:property-placeholder location="classpath:${env}.jdbc.properties"/>

<bean id="identDataSource"
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"
     p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
     p:username="${jdbc.username}" p:password="${jdbc.password}"/>

${env} will reflect the value of the Java Option set in the server, which means the connection properties will be loaded from the correct properties file.

Advertisements
This entry was posted in Code and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s