views:

347

answers:

2

I am trying to find the best way to pass complex configurations in a Spring webapp running in Tomcat. Currently I use JNDI to pass data sources and strings from the Tomcat context into the webapp, and this works well.

But, lets say I need to select the implementation of a notification service. There is no way that Spring can conditionally select which bean to instantiate (although in the past I have used a JNDI string to import a predefined configuration of beans by setting contextConfigLocation).

I've also seen many webapps which supply a configuration tool which will create a custom WAR file. In my opinion this is bad form, if for no other reason than it prevents the redeployment of WARs from upstream without many checks to ensure all the configuration has been re-applied.

Ideally I would be able to supply a Spring XML file which existed on the filesystem, outside of the webapp. But, the spring import directive does not seem to resolve ${} variables, making it impossible to supply customisations.

Are there any techniques I can employ here to properly separate complex configuration from the webapp?

+4  A: 

If I have a specific set of beans that I'd like to configure, and this configuration must be separated from the WAR file, I usually do the following:

In applicationContext.xml:

<!-- here you have a configurer based on a *.properties file -->
<bean id="configurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="file://${configDir}/configuration.properties"/>
    <property name="ignoreResourceNotFound" value="false" />
    <property name="ignoreUnresolvablePlaceholders" value="false" />
    <property name="searchSystemEnvironment" value="false" />
</bean>

<!-- this is how you can use configuration properties -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="${smtp.host}"/>
</bean>

In configuration.properties:

smtp.host=smtp.your-isp.com

You also need to start Tomcat with -DconfigDir=/path/to/configuration/directory

candiru
Wouldn't it be even "eleganter" to use a `classpath:` URI to the properties file, so you can just drop it somewhere in the classpath and avoid fiddling with the Tomcat startup parameters?
gustafc
Well, is there a way to get something onto the Tomcat classpath without it being in the WAR? I know there is shared/lib or common/lib is there a classes equivalent?
Note to self, in Tomcat 6.0 it is ${catalina.home}/lib.Thanks for the assistance, I think I this will work.
A: 

If you want to be fully portable between web containers you cannot rely on anything outside your WAR-file. In Tomcat the SecurityManager allows you to discover the physical location on disk where your code is deployed, and you can then use that knowledge to navigate the disk to a location where your configuration file is placed.

See e.g. http://stackoverflow.com/questions/1127166/determine-location-of-a-java-class-loaded-by-matlab

Thorbjørn Ravn Andersen