Spring: namespace vs contextConfig Configuring initialization parameters in web.xml

I am reading documentation for Spring MVC and I have a question regarding init parameters. I am using Spring 3.2 if that matters. What is the difference between contextConfigLocation and namespace? Is contextConfigLocation a value only for specifying folders where the context class can find the XML definition and namespace attribute to specify 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> 

It is right? Should he use / WEB -INF / application-context.xml? And should you point the way?

+50
java spring java-ee spring-mvc dependency-injection
Apr 04 '13 at
source share
3 answers

TL; DR

Just set the values โ€‹โ€‹for contextConfigLocation when you need to specify custom configuration files. This way you specify the names of the configuration files and their locations.

namespace is essentially an alternative way to tell Spring the container container loader which configuration file to use. I never worry about this, but just use contextConfigLocation whenever I need to configure custom configuration files.

Here is an example from one of my previous Spring projects (some of the abbreviations are omitted for brevity):

web.xml

 <?xml version="1.0" encoding="UTF-8"?> <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"> <display-name>Spring Web Application example</display-name> <!-- Configurations for the root application context (parent context) --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/jdbc/spring-jdbc.xml /WEB-INF/spring/security/spring-security-context.xml </param-value> </context-param> <!-- Configurations for the DispatcherServlet application context (child context) --> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/mvc/spring-mvc-servlet.xml </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/admin/*</url-pattern> </servlet-mapping> </web-app> 



Long answer

OK, first let me know some important points. There are two types of contexts we deal with:

  • root context (parent)
  • individual servlet context (child)

Quote from the Spring Framework API (version 3.2.2 at the time of writing this) for WebApplicationContext (my emphasis):

Like general application contexts, web application contexts are hierarchical. There is one root context for each application, while each servlet in the application (including the dispatcher servlet in MVC) has its own child context .

Also here: Context Hierarchies :

For example, if you are developing a Spring MVC web application, you will usually have a root WebApplicationContext loaded through the SpringContextLoaderListener and a child WebApplicationContext loaded through the Spring DispatcherServlet . This leads to the context of the parent-child hierarchy, where the common components and infrastructure configuration are declared in the root context and consumed in the web components.

And here: 17.2 DispatcherServlet :

Spring ApplicationContext instances can be copied. In the web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all beans already defined in the root of the WebApplicationContext . These inherited beans can be overridden in the servlet, and you can define a new beans local area for this Servlet instance.


Now consider the configuration of the root context of the application . Here is an example:
web.xml

 <?xml version="1.0" encoding="UTF-8"?> <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"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/daoContext.xml /WEB-INF/spring/applicationContext.xml </param-value> </context-param> </web-app> 


From the official Spring documentation (my highlight):
5.14.4 A convenient instance of ApplicationContext for web applications :

You can instantiate ApplicationContext declaratively using, for example, ContextLoader. Of course, you can also create ApplicationContext Examples programmatically using one of the ApplicationContext Implementations.

You can register ApplicationContext with ContextLoaderListener (see example above)

The listener checks the contextConfigLocation parameter. If the parameter does not exist, the listener uses / WEB -INF / applicationContext.xml by default . When the parameter exists, the listener separates the line using predefined delimiters (comma, semicolon, and spaces) and uses values โ€‹โ€‹like where searches will be performed. Ant style patterns are also supported. Examples are / WEB -INF / * Context.xml for all files with names ending with "Context.xml" located in the "WEB-INF" directory and / WEB -INF / ** / * Context.xml, for all such files in any subdirectory "WEB-INF".


Quite often, the Spring configuration is split into multiple files. This is more logical and convenient, especially in large-scale projects. In our example, we explicitly defined two XML configuration files: daoContext.xml and applicationContext.xml in a custom location: /WEB-INF/spring/ . Again, if we did not define contextConfigLocation, ContextLoaderListener tried to find the default configuration file: /WEB-INF/applicationContext.xml .

Note:
The root context is optional . Also see this answer: stack overflow

So, if the default configuration file /WEB-INF/applicationContext.xml does not suit your needs, use ContextLoaderListener along with <context-param> contextConfigLocation, where you can define custom configuration files to determine the root context of the application .


Next, consider the individual (child) application context . From the official Spring documentation (my highlight):
17.2 DispatcherServlet

After initializing the DispatcherServlet, Spring MVC looks for a file named
[servlet-name] -servlet.xml in the WEB-INF directory of your web application and creates beans there, overriding the definitions of any beans defined with the same name in the global scope.

Consider the following configuration of the DispatcherServlet servlets (in the web.xml file):

 <web-app> <servlet> <servlet-name>golfing</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>golfing</servlet-name> <url-pattern>/golfing/*</url-pattern> </servlet-mapping> </web-app> 


About the ConfigLocation Context and Namespace

From the documentation (my emphasis):

With the above servlet configuration, you will need a file named
/WEB-INF/golfing-servlet.xml in your application; this file will contain all your Spring Web MVC components (beans). You can change the exact location of this configuration file through the servlet initialization parameter (see below for more details).
...
You can configure individual instances of DispatcherServlet by adding servlet initialization parameters (init-param elements) for the servlet declaration in the web.xml file. See the following table for a list of supported options.

  • contextClass : a class that implements WebApplicationContext that creates the context used by this servlet. The default is XmlWebApplicationContext.

  • contextConfigLocation : a string that is passed to the context instance (specified by contextClass) to indicate where context (s) can be found. A line consists of several lines (using a comma as a separator) to support multiple contexts. In the case of multiple contextual locations with beans that are defined twice, the last place takes precedence.

  • namespace . WebApplicationContext namespace. By default, the [servlet name] server is used.


Now let's look at the API documentation for related classes. The DispatcherServlet class extends the abstract FrameworkServlet class. In the API FrameworkServlet docs (focus):

Pass the init-param of the "contextConfigLocation" servlet to the instance context, parse it into potentially several file paths, which can be separated by any number of commas and spaces, for example, "Test-servlet.xml, myServlet.xml". Unless explicitly specified, the implementation context should create a default location from the servlet namespace .

The default namespace is "servlet-name'-servlet", for example. The "test servlet" for the "test" servlet (resulting in "/WEB-INF/test-servlet.xml", the default location using XmlWebApplicationContext). The namespace can also be set explicitly through the "namespace" servlet init-param .

This is an excerpt from the source code for FrameworkServlet:
FrameworkServlet.java

 .... /** * 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"; .... 


The default context class for FrameworkServlet is XmlWebApplicationContext . From the XmlWebApplicationContext API documents (focus):

By default, the configuration will be taken from "/WEB-INF/applicationContext.xml" for the root context and "/WEB-INF/test-servlet.xml" for the context with the namespace "test-servlet" (for example, for a DispatcherServlet instance with servlet -name "test").

The default settings for the configuration can be overridden using the contextConfigLocation context parameter ContextLoader and the init-param servlet for FrameworkServlet . Configuration locations can either denote specific files like "/WEB-INF/context.xml" or Ant-like patterns, such as "/WEB-INF/*-context.xml" (see PathMatcher javadoc for a template for details).

Overriding the default configuration locations using contextConfigLocation is the same as in the above example for the context of the root application.

Regarding the redefinition of the default namespace , there are a few important points. When you install a new namespace, do not add it with /WEB-INF and do not add .xml to it . The reason for this can be discovered if we look in the source file for the XmlWebApplicationContext class:
XmlWebApplicationContext.java

 ... /** 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}; } } 

As you can see, the source code says it all.


User Namespace Example

web.xml

 <?xml version="1.0" encoding="UTF-8"?> <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"> <!-- Configurations for the DispatcherServlet application context (child context) --> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>namespace</param-name> <param-value>spring/mvc/spring-mvc</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app> 

As a result, instead of using the default namespace to build the path to the configuration file, which otherwise would be /WEB-INF/spring-mvc-servlet.xml , the container will look for /WEB-INF/spring/mvc/spring-mvc.xml .

Note:
The above explanations for setting up a custom namespace are related to the XmlWebApplicationContext context class . You can specify an alternative class, for example AnnotationConfigWebApplicationContext , so there will be special points for this.




Conclusion

This (IMHO) is much easier to use the contextConfigLocation parameter to define custom configuration files, both for the context of the root application and for individual contexts. The only difference is that for the root context of the application, you use <context-param> inside the <web-app> element, but NOT inside a specific servlet (also do not forget the listener class). And for the child context, you use the <init-param> nested inside the <servlet> element for each specific servlet . See My configuration examples (web.xml) at the very beginning of this post.

Additional sources (as if it were not enough :-)):

Also see the answers:

  • Difference between applicationContext.xml and spring -servlet.xml in Spring
  • What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?
  • Spring-MVC: what is โ€œcontextโ€ and โ€œnamespaceโ€?
+155
Apr 05 '13 at 3:04
source share

I think the LuckyLuke answer has a lot of useful information, but it does not answer the question. In particular, how do the "namespace" and "contextConfigLocation" parameters work?

The only place I could find a specific answer is the source code:

  • the namespace sets a custom namespace that will be used to build the default contextual location. Parameter
  • contextConfigLocation explicitly sets the contextual location of the context, rather than relying on the default location built from the namespace
+4
Aug 14 '13 at 20:05
source share

What is the difference between contextConfigLocation and namespace? contextConfigLocation is used to specify the path to the spring configuration files, i.e. they will be initialized. namespace is used to specify the path and name of the DispatcherServlet from spring MVC. the default is [Dispatcher_name]-servlet.xml , here is an example:

 <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>namespace</param-name> <param-value>config/spring-mvc</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> 

Spring will look for a file that will be used as the mvc configuration along the path /WEB-INF/config/spring-mvc.xml .
The contextConfigLocation value is intended only to indicate folders where the context class can find the XML definition

 <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/app-*.xml</param-value> </context-param> 

The above code shows that when you start the spring application, all files whose names begin with "app-" and end with ".xml" in the WEB-INF / config directory.

Should I use / WEB -INF / application-context.xml? And should you point the way?
In the above example, we can know that when configuring spring we need to specify the full name of the path and the shared file, and when SpringMVC we need to specify only the path (if it is in the directory, and not in the WEB-INF directory) and the name (not including the extension )
Hope to help you :)

+2
Apr 04 '13 at 18:45
source share



All Articles