How to move my spring xml configuration outside my web application?

How to move my spring xml configuration outside my java web application?

I want to save my spring.xml outside of my web application, so I do not need to create a new assembly of my application to change the configuration.

What is the best way to do this?

+6
java spring web-applications
source share
3 answers

As Rod Johnson explains this in this thread :

You can use the classpath: prefix to load from the classpath using a regular Spring listener or start servlet. This is made possible by Spring's resource abstraction. You can freely mix and map resources with WEB-INF and classpath.

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

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

I think relying on a class path is more portable than using an absolute file path.

+7
source share

You can move it to a folder (outside the webapp structure) and explicitly specify the contextual location to point to the context in this folder in web.xml :

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

However, the best way to do this might not be to do it at all :-) and instead review how you are deploying your application (for example, you can create a “patch” or “update” ”deployment unit that contains changes, not completely exploded WARs.) Showing absolute paths tends to be more complicated than it costs.

+2
source share

Why is it difficult for you to do this when it is a deployment problem. Most containers deploy WARs in the form of "exploded," which means that somewhere in your file system there is a spring.xml file.

If you want to update this, you can just find the actual location and then copy the new spring.xml on top of the old one. However, at the same time, your WAR remains the most "source of truth."

WARs are generally very easy to use and deploy, so there is an advantage in combining your configuration as much as possible in a WAR.

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

Finally, having spring.xml outside of your WAR means that it is available for ALL of your WARS, and if you decide to add another WAR to your system later, it will probably be difficult to parse the two files because they are not tied to a specific war for a long time .

0
source share

All Articles