Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegant ways to separate configuration from WAR in Tomcat

Tags:

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?

like image 299
Nigel Avatar asked Nov 12 '09 04:11

Nigel


People also ask

Where do I put WAR file in Tomcat?

Perhaps the simplest way to deploy a WAR file to Tomcat is to copy the file to Tomcat's webapps directory. Copy and paste WAR files into Tomcat's webapps directory to deploy them. Tomcat monitors this webapps directory for changes, and if it finds a new file there, it will attempt to deploy it.

How do I run a WAR file locally?

Run the WAR fileOpen up a terminal/command prompt window to the download directory. Run the command java -jar jenkins. war . Browse to http://localhost:8080 and wait until the Unlock Jenkins page appears.

How do I know if war is deployed in Tomcat?

The definitive way to determine if a war has finished deploying and the webapp has started is to look in the catalina. out log. A message will be logged. In operational terms, Tomcat will respond to a request made to that webapp in various ways depending on how far along it is.

How does Tomcat WAR file work?

Java web applications are usually packaged as WAR files for deployment. These files can be created on the command line or with an IDE, like Eclipse. After deploying the WAR file, Tomcat unpacks it and stores all the project files from the webapps directory in a new directory named after the project.


1 Answers

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

like image 88
candiru Avatar answered Sep 30 '22 01:09

candiru