Wednesday, September 15, 2010

Tomcat Context Configuration and JNDI

A coworker discovered a feature of Tomcat and Spring that may make some configuration easier. It turns out that it’s not just the <Resource> tags in the context.xml file that get set in the JNDI context but also <Environment> tags. So we can do something like the following in the context.xml file


Then I add the following reference in my spring xml files where I need it:


Since it’s a string, it can be assigned as a property that expects a string on any bean. This isn’t specifically a Spring solution as much as a it is a JEE solution. The JEE spec says that any items defined in the context should be stored in the JNDI context. Tomcat does this via context.xml files but you could also do it in the web.xml file. By doing it in the context.xml file you can achieve some degree of deployment independence for multiple web apps hosted in one container.

No comments:

Post a Comment