I am reading the documentation for Spring MVC and I have a question regarding the init params. I am using Spring 3.2 if it matter. What is the difference between the contextConfigLocation and namespace? Is the contextConfigLocation meant only for specifying the folders where the context class can find a XML definition and the namespace attribute is meant for specifying the file name?
<servlet>
<servlet-name>AppServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF</param-value>
</init-param>
<init-param>
<param-name>namespace</param-name>
<param-value>application-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Is this correct? Should it use /WEB-INF/application-context.xml? And should you specify paths?
Best Answer
TL;DR
Just set the value(s) for the
contextConfigLocation
whenever you need to specify custom config files. This way you will be specifying both the config file names and their locations.The
namespace
is essentially an alternative way of telling Spring container context loader class what config file to use. I never bother with it, but just usecontextConfigLocation
whenever I need to configure custom config files.Here is an example from one of my previous Spring projects (some of the configurations omitted for the sake of brevity):
web.xml
Long answer
OK, first let's get some important moments clear. There are two types of contexts we are dealing with:
Quote from the Spring Framework API (version 3.2.2 at the moment of writing this) for the WebApplicationContext (emphasis mine):
Also here: Context hierarchies:
And here: 17.2 The DispatcherServlet:
Now let's see the root application context configuration. Here is an example:
web.xml
From the official Spring documentation (emphasis mine):
5.14.4 Convenient ApplicationContext instantiation for web applications:
Quite often Spring configuration is split across multiple files. It's more logical and convenient, especially in the large-scale projects. In our example we explicitly defined two configuration XML files: daoContext.xml and applicationContext.xml in the custom location:
/WEB-INF/spring/
. Again, had we not defined contextConfigLocation, the ContextLoaderListener would try to locate the default config file:/WEB-INF/applicationContext.xml
.NOTE:
The root context is optional. Also see this answer: https://stackoverflow.com/a/7451389/814702
So if the default
/WEB-INF/applicationContext.xml
config file does not suit your needs, use ContextLoaderListener along with<context-param>
contextConfigLocation where you can define custom config file(s) to define root application context.Next let's see the individual (child) application context. From the official Spring documentation (emphasis mine):
17.2 The DispatcherServlet
About contextConfigLocation and namespace
From the documentation (emphasis mine):
Now let's research the API documentation for the related classes. The class DispatcherServlet extends abstract class FrameworkServlet. From the FrameworkServlet API docs (emphasis mine):
This is the excerpt from the FrameworkServlet source code:
FrameworkServlet.java
The default context class for FrameworkServlet is XmlWebApplicationContext. From the XmlWebApplicationContext API docs (emphasis mine):
Overriding default config locations using
contextConfigLocation
is the same as in the above example for the root application context.As for the overriding the default namespace there are some important moments. When you set a new namespace, don't prepend it with
/WEB-INF
and don't append.xml
to it. The reason for that can be discovered if we look in the source file for the XmlWebApplicationContext class:XmlWebApplicationContext.java
As you can see, the source code says it all.
Example of specifying custom namespace
web.xml
The result is that, instead of using the default namespace for constructing the path to the config file, that would otherwise be
/WEB-INF/spring-mvc-servlet.xml
, the container will look for/WEB-INF/spring/mvc/spring-mvc.xml
.NOTE:
The above explanations related to the setting custom namespace are for the default XmlWebApplicationContext context class. One can specify an alternative class, like AnnotationConfigWebApplicationContext, so there will be some special moments for that.
CONCLUSION
It is (IMHO) much more easier to use
contextConfigLocation
parameter to define custom config files, both for the root application context and for the individual contexts. The only difference is that for the root application context you use<context-param>
within<web-app>
element, but NOT within a specific servlet (also don't forget the listener class). And for the child context you use<init-param>
nested inside the<servlet>
element for each specific servlet. See my example configurations (web.xml) in the very beginning of this post.Additional resources (as if the above was not enough :-)):