views:

450

answers:

3

How do I move my spring xml configuration outside of my java web application?

I'd like to store my spring.xml outside of my web application so I don't have to create a new build of my application to change the configuration.

What is the best way to do this?

+2  A: 

You can move it to some folder (outside of webapp structure) and explicitly specify context location to point to context in that folder in your web.xml:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>file:/full/path/to/context.xml</param-value>
</context-param>

That said, the best way to do this may be to not do it at all :-) and instead reconsider how you deploy your application (e.g. you can create a "patch" or "upgrade" deployment unit that contains changes rather then full blown WAR). Specifying absolute paths tends to be more hassle than it's worth.

ChssPly76
A: 

Why complicate you build for this, when it's a deployment problem. Most containers deploy WARs in an "exploded" form, which means that somewhere on you file system is the spring.xml file.

If you'd like to update that, you can simply locate the actual location and then copy your new spring.xml over the old one. Yet, at the same time, your WAR remains the de riguer "source of truth".

WARs tend to be very simple to use and deploy, so there's a benefit to bundling your configuration as best you can in the WAR.

So, you can update the spring.xml by going behind the back of the container, and editing it (or copying over) it directly.

Finally, having the spring.xml outside of your WAR means that's it's available to ALL of your WARs, and if you decide later to add another WAR to your system, you will likely have difficulty segregating the two files as they are no long anchored to a specific WAR.

Will Hartung
Agreed on this being a deployment issue. You're wrong about context being available to all WARs, though - why would it be?
ChssPly76
I think he means that two copies of the same WAR would refer to the same file on the filesystem... doesn't seem like a compelling reason not to do it, though
skaffman
We'll deploy this same .war to multiple customers with different configurations. This is why we need an external configuration file.
ScArcher2
+2  A: 

As Rod Johnson explains it in this thread:

You can use the classpath: prefix to load from the classpath, with the normal Spring listener or startup servlet. This is made possible by Spring's Resource abstraction. You can freely mix and match resources from WEB-INF and classpath.

So, put the configuration files somewhere in the classpath outside the webapp and declare the following in the web.xml:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:springContext*.xml</param-value>
</context-param>

I think that relying on the classpath is more portable than using an absolute file path.

Pascal Thivent
That would definitely be a good approach for standalone app. For webapp, though, in order to have something in classpath but outside of webapp folder you'd have to make your context available to entire server which may not be ideal (unless the app in question is the only one).
ChssPly76
To be more precise, this means making the context files available to all webapps (not sure this is what you meant with "entire server"). But I agree that this may indeed not be ideal.
Pascal Thivent