Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>TL;DR</h1> <p>Just set the value(s) for the <code>contextConfigLocation</code> whenever you need to specify custom config files. This way you will be specifying both the config file names and their locations.</p> <p><strong>The <code>namespace</code> is essentially an alternative way</strong> of telling Spring container context loader class what config file to use. I never bother with it, but just use <code>contextConfigLocation</code> whenever I need to configure custom config files.</p> <p>Here is an example from one of my previous Spring projects (some of the configurations omitted for the sake of brevity):</p> <p><em>web.xml</em></p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt; &lt;display-name&gt;Spring Web Application example&lt;/display-name&gt; &lt;!-- Configurations for the root application context (parent context) --&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; /WEB-INF/spring/jdbc/spring-jdbc.xml /WEB-INF/spring/security/spring-security-context.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;!-- Configurations for the DispatcherServlet application context (child context) --&gt; &lt;servlet&gt; &lt;servlet-name&gt;spring-mvc&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;init-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; /WEB-INF/spring/mvc/spring-mvc-servlet.xml &lt;/param-value&gt; &lt;/init-param&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;spring-mvc&lt;/servlet-name&gt; &lt;url-pattern&gt;/admin/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;/web-app&gt; </code></pre> <hr> <h1>Long answer</h1> <p>OK, first let's get some important moments clear. There are two types of contexts we are dealing with:</p> <ol> <li><strong>root context</strong> (parent)</li> <li><strong>individual servlet context</strong> (child)</li> </ol> <p>Quote from the Spring Framework API (version 3.2.2 at the moment of writing this) for the <a href="http://docs.spring.io/spring/docs/3.2.2.RELEASE/javadoc-api/org/springframework/web/context/WebApplicationContext.html" rel="noreferrer">WebApplicationContext</a> (emphasis mine):</p> <blockquote> <p>Like generic application contexts, web application contexts are hierarchical. <strong>There is a single root context per application, while each servlet in the application (including a dispatcher servlet in the MVC framework) has its own child context</strong>.</p> </blockquote> <p>Also here: <a href="http://docs.spring.io/spring/docs/3.2.2.RELEASE/spring-framework-reference/html/testing.html#testcontext-ctx-management-ctx-hierarchies" rel="noreferrer">Context hierarchies</a>:</p> <blockquote> <p>For example, if you are developing a Spring MVC web application you will typically have a <strong>root WebApplicationContext loaded via Spring's ContextLoaderListener</strong> and a <strong>child WebApplicationContext loaded via Spring's DispatcherServlet</strong>. This results in a parent-child context hierarchy where shared components and infrastructure configuration are declared in the root context and consumed in the child context by web-specific components.</p> </blockquote> <p>And here: <a href="http://docs.spring.io/spring/docs/3.2.2.RELEASE/spring-framework-reference/html/mvc.html#mvc-servlet" rel="noreferrer">17.2 The DispatcherServlet</a>:</p> <blockquote> <p>ApplicationContext instances in Spring can be scoped. In the Web MVC framework, <strong>each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext</strong>. These inherited beans can be overridden in the servlet-specific scope, and you can define new scope-specific beans local to a given Servlet instance.</p> </blockquote> <p><br> Now let's see the <strong>root application context</strong> configuration. Here is an example:<br> <em>web.xml</em></p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; /WEB-INF/spring/daoContext.xml /WEB-INF/spring/applicationContext.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;/web-app&gt; </code></pre> <p><br /> From the official Spring documentation (emphasis mine):<br> <a href="http://docs.spring.io/spring/docs/3.2.2.RELEASE/spring-framework-reference/html/beans.html#context-create" rel="noreferrer">5.14.4 Convenient ApplicationContext instantiation for web applications</a>: </p> <blockquote> <p><strong>You can create ApplicationContext instances declaratively</strong> by using, for example, a ContextLoader. Of course you can also create ApplicationContext instances programmatically by using one of the ApplicationContext implementations.</p> <p>You can <strong>register an ApplicationContext using the ContextLoaderListener</strong> (see the example above)</p> <p>The listener inspects the contextConfigLocation parameter. <strong>If the parameter does not exist, the listener uses /WEB-INF/applicationContext.xml as a default</strong>. When the parameter does exist, the listener separates the String by using predefined delimiters (comma, semicolon and whitespace) and uses the values as locations where application contexts will be searched. Ant-style path patterns are supported as well. Examples are /WEB-INF/*Context.xml for all files with names ending with "Context.xml", residing in the "WEB-INF" directory, and /WEB-INF/**/*Context.xml, for all such files in any subdirectory of "WEB-INF".</p> </blockquote> <p><br /> 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: <em>daoContext.xml</em> and <em>applicationContext.xml</em> in the custom location: <code>/WEB-INF/spring/</code>. Again, had we not defined <em>contextConfigLocation</em>, the <em>ContextLoaderListener</em> would try to locate the default config file: <em><code>/WEB-INF/applicationContext.xml</code></em>.</p> <p>NOTE:<br> The <strong>root context is optional</strong>. Also see this answer: <a href="https://stackoverflow.com/a/7451389/814702">https://stackoverflow.com/a/7451389/814702</a></p> <p>So if the default <em><code>/WEB-INF/applicationContext.xml</code></em> config file does not suit your needs, use <em>ContextLoaderListener</em> along with <code>&lt;context-param&gt;</code> <em>contextConfigLocation</em> where you can define custom config file(s) <strong>to define root application context</strong>.</p> <p><br > Next let's see the <strong>individual (child) application context</strong>. From the official Spring documentation (emphasis mine):<br> <a href="http://docs.spring.io/spring/docs/3.2.2.RELEASE/spring-framework-reference/html/mvc.html#mvc-servlet" rel="noreferrer">17.2 The DispatcherServlet</a></p> <blockquote> <p><strong>Upon initialization of a DispatcherServlet, Spring MVC looks for a file named<br> [servlet-name]-servlet.xml in the WEB-INF directory</strong> of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.</p> <p>Consider the following DispatcherServlet Servlet configuration (in the web.xml file):</p> </blockquote> <pre class="lang-xml prettyprint-override"><code>&lt;web-app&gt; &lt;servlet&gt; &lt;servlet-name&gt;golfing&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;golfing&lt;/servlet-name&gt; &lt;url-pattern&gt;/golfing/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;/web-app&gt; </code></pre> <p><br></p> <h2>About contextConfigLocation and namespace</h2> <p>From the documentation (emphasis mine):</p> <blockquote> <p>With the above Servlet configuration in place, you will need to have a file called<br> <code>/WEB-INF/golfing-servlet.xml</code> in your application; this file will contain all of your Spring Web MVC-specific components (beans). You can change the exact location of this configuration file through a Servlet initialization parameter (see below for details).<br> ...<br> <strong>You can customize individual DispatcherServlet instances</strong> by adding Servlet initialization parameters (init-param elements) to the Servlet declaration in the web.xml file. See the following table for the list of supported parameters.</p> <ul> <li><p><strong><em>contextClass</em></strong>: Class that implements WebApplicationContext, which instantiates the context used by this Servlet. By default, the XmlWebApplicationContext is used.</p></li> <li><p><strong><em>contextConfigLocation</em></strong>: String that is passed to the context instance (specified by contextClass) to indicate where context(s) can be found. The string consists potentially of multiple strings (using a comma as a delimiter) to support multiple contexts. In case of multiple context locations with beans that are defined twice, the latest location takes precedence.</p></li> <li><p><strong><em>namespace</em></strong>: Namespace of the WebApplicationContext. Defaults to [servlet-name]-servlet.</p></li> </ul> </blockquote> <p><br /> Now let's research the API documentation for the related classes. The class <a href="http://docs.spring.io/spring/docs/3.2.2.RELEASE/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html" rel="noreferrer"><em>DispatcherServlet</em></a> extends abstract class <a href="http://docs.spring.io/spring/docs/3.2.2.RELEASE/javadoc-api/org/springframework/web/servlet/FrameworkServlet.html" rel="noreferrer"><em>FrameworkServlet</em></a>. From the <em>FrameworkServlet</em> API docs (emphasis mine):</p> <blockquote> <p>Passes a "contextConfigLocation" servlet init-param to the context instance, parsing it into potentially multiple file paths which can be separated by any number of commas and spaces, like<br> "test-servlet.xml, myServlet.xml". <strong>If not explicitly specified, the context implementation is supposed to build a default location from the namespace of the servlet</strong>.</p> <p>The default namespace is "'servlet-name'-servlet", e.g. "test-servlet" for a servlet-name "test" (leading to a "/WEB-INF/test-servlet.xml" default location with XmlWebApplicationContext). <strong>The namespace can also be set explicitly via the "namespace" servlet init-param</strong>.</p> </blockquote> <p>This is the excerpt from the <em>FrameworkServlet</em> source code:<br> <a href="http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.web.servlet/3.2.2/org/springframework/web/servlet/FrameworkServlet.java#FrameworkServlet" rel="noreferrer"><em>FrameworkServlet.java</em></a></p> <pre class="lang-java prettyprint-override"><code>.... /** * Suffix for WebApplicationContext namespaces. If a servlet of this class is * given the name "test" in a context, the namespace used by the servlet will * resolve to "test-servlet". */ public static final String DEFAULT_NAMESPACE_SUFFIX = "-servlet"; .... </code></pre> <p><br /> The default context class for <em>FrameworkServlet</em> is <a href="http://docs.spring.io/spring/docs/3.2.2.RELEASE/javadoc-api/org/springframework/web/context/support/XmlWebApplicationContext.html" rel="noreferrer"><em>XmlWebApplicationContext</em></a>. From the <em>XmlWebApplicationContext</em> API docs (emphasis mine):</p> <blockquote> <p>By default, the configuration will be taken from "/WEB-INF/applicationContext.xml" for the root context, and "/WEB-INF/test-servlet.xml" for a context with the namespace "test-servlet" (like for a DispatcherServlet instance with the servlet-name "test").</p> <p><strong>The config location defaults can be overridden via the "contextConfigLocation" context-param of ContextLoader and servlet init-param of FrameworkServlet</strong>. Config locations can either denote concrete files like "/WEB-INF/context.xml" or Ant-style patterns like "/WEB-INF/*-context.xml" (see PathMatcher javadoc for pattern details).</p> </blockquote> <p>Overriding default config locations using <code>contextConfigLocation</code> is the same as in the above example for the root application context.</p> <p>As for the <strong>overriding the default namespace</strong> there are some important moments. When you set a new namespace, <strong>don't prepend it with <code>/WEB-INF</code> and don't append <code>.xml</code> to it</strong>. The reason for that can be discovered if we look in the source file for the <em>XmlWebApplicationContext</em> class:<br> <a href="http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.web/3.2.2/org/springframework/web/context/support/XmlWebApplicationContext.java#XmlWebApplicationContext" rel="noreferrer"><em>XmlWebApplicationContext.java</em></a></p> <pre class="lang-java prettyprint-override"><code>... /** Default config location for the root context */ public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml"; /** Default prefix for building a config location for a namespace */ public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/"; /** Default suffix for building a config location for a namespace */ public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml"; ... /** * The default location for the root context is "/WEB-INF/applicationContext.xml", * and "/WEB-INF/test-servlet.xml" for a context with the namespace "test-servlet" * (like for a DispatcherServlet instance with the servlet-name "test"). */ @Override protected String[] getDefaultConfigLocations() { if (getNamespace() != null) { return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX}; } else { return new String[] {DEFAULT_CONFIG_LOCATION}; } } </code></pre> <p>As you can see, the source code says it all.</p> <p><br></p> <h2>Example of specifying custom namespace</h2> <p><em>web.xml</em></p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt; &lt;!-- Configurations for the DispatcherServlet application context (child context) --&gt; &lt;servlet&gt; &lt;servlet-name&gt;spring-mvc&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;init-param&gt; &lt;param-name&gt;namespace&lt;/param-name&gt; &lt;param-value&gt;spring/mvc/spring-mvc&lt;/param-value&gt; &lt;/init-param&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;spring-mvc&lt;/servlet-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;/web-app&gt; </code></pre> <p>The result is that, instead of using the default namespace for constructing the path to the config file, that would otherwise be <code>/WEB-INF/spring-mvc-servlet.xml</code>, the container will look for <code>/WEB-INF/spring/mvc/spring-mvc.xml</code>.</p> <p>NOTE:<br> The above explanations related to the setting custom namespace are for the default <a href="http://docs.spring.io/spring/docs/3.2.2.RELEASE/javadoc-api/org/springframework/web/context/support/XmlWebApplicationContext.html" rel="noreferrer"><em>XmlWebApplicationContext</em></a> context class. One can specify an alternative class, like <a href="http://docs.spring.io/spring/docs/3.2.2.RELEASE/javadoc-api/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.html" rel="noreferrer"><em>AnnotationConfigWebApplicationContext</em></a>, so there will be some special moments for that.</p> <hr> <h2>CONCLUSION</h2> <p>It is (IMHO) much more easier to use <code>contextConfigLocation</code> 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 <strong><code>&lt;context-param&gt;</code></strong> within <code>&lt;web-app&gt;</code> element, but NOT within a specific servlet (also don't forget the listener class). And for the child context you use <strong><code>&lt;init-param&gt;</code></strong> nested inside the <code>&lt;servlet&gt;</code> element <strong>for each specific servlet</strong>. See my example configurations (<em>web.xml</em>) in the very beginning of this post.</p> <p>Additional sources (as if the above was not enough :-)):</p> <ul> <li><a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/" rel="noreferrer">Spring Framework Reference Documentation</a></li> <li><a href="http://www.jroller.com/latenightdev/entry/spring_mvc_context_loading" rel="noreferrer">Spring MVC Context Loading</a> </li> </ul> <p>Also see these answers:</p> <ul> <li><a href="https://stackoverflow.com/a/3652125/814702">Difference between applicationContext.xml and spring-servlet.xml in Spring</a></li> <li><a href="https://stackoverflow.com/a/11709272/814702">What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?</a></li> <li><a href="https://stackoverflow.com/a/7451389/814702">Spring-MVC: What are a “context” and “namespace”?</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload