Trevor
Trevor

Reputation: 13457

How to implement environment-specific init parameters in my tomcat application

I'd like to be able to implement a configuration-less deployment for my java application (tomcat7, spring-mvc). For example, right now we are considering creating one context.xml file for each environment (prod, stage, dev) we deploy to:

context_prod.xml 
context_stage.xml 
context_dev.xml
context.xml (for localhost)

On each of our servers we would have a symlink context.xml which would point to the appropriate context file (e.g. context_prod.xml). So when we deploy, we don't have to worry about changing database references, keys, etc.

I feel like there's probably a better way to do this; perhaps one that is built into spring?

Upvotes: 2

Views: 748

Answers (2)

Shamim Ahmmed
Shamim Ahmmed

Reputation: 8383

You could use Spring @Profile introduced in Spring 3.1. In your case you could use profiles like dev, stage, prod etc.

This profile value could be initialized run time. So when your application started, Spring could fetch appropriate profile based on configuration.

You could setup profile from environment variable, via deployment descriptor (web.xml) etc. This Spring source tutorial could be interesting for you.

I personally using Maven build to replace the profile value during build time in the web.xml. In the build time I passed profile value as build argument.

Upvotes: 1

bh5k
bh5k

Reputation: 880

Spring has recently added the functionality to handle environment configuration:

http://blog.springsource.com/2011/02/11/spring-framework-3-1-m1-released/

This still seems a little bit complicated for me and I have done exactly what you are asking in my own Spring MVC applications for our logging. In my DispatcherServlet configuation I have this line:

<context:property-placeholder location="classpath*:/system.properties"/>

<util:properties id="mySystemProperties" location="classpath:/logging/log4j-${system.runMode}.properties" />

system.runMode is just an env variable that we created and is set in CATALINA.SH at startup like this: Setting environment variable TESSDATA_PREFIX in Tomcat

I then use Spring EL to reference any values I want and it works per environment. As you can see I did this for our logging and have a different logging configuration file per environment.

Upvotes: 1

Related Questions