Spring:web.xml中的命名空间与contextConfigLocation init参数 [英] Spring: namespace vs contextConfigLocation init parameters in web.xml

查看:148
本文介绍了Spring:web.xml中的命名空间与contextConfigLocation init参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Spring MVC的文档,我有一个关于init参数的问题。我正在使用Spring 3.2如果重要。 contextConfigLocation和命名空间有什么区别? contextConfigLocation是否仅用于指定上下文类可以找到XML定义的文件夹,命名空间属性是否用于指定文件名?

 < 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>

这是正确的吗?应该使用/WEB-INF/application-context.xml吗?你应该指定路径吗?

解决方案

TL; DR



只要您需要指定自定义配置文件,请设置 contextConfigLocation 的值。这样,您将同时指定配置文件名称及其位置。



命名空间本质上告诉Spring容器上下文加载器类什么配置文件要使用的替代方法。我从来不打扰它,但只要使用 contextConfigLocation ,只要我需要配置自定义配置文件。



这是一个例如我之前的Spring项目之一(为简洁起见,省略了一些配置):



web.xml

 <?xml version =1.0encoding =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应用程序示例< / display-name>

<! - 根应用程序上下文(父上下文)的配置 - >
< 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>

<! - DispatcherServlet应用程序上下文(子上下文)的配置 - >
< 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>






长回答


好的,首先让我们清楚一些重要的时刻。我们正在处理两种类型的上下文:


  1. 根上下文(父)

  2. 单个servlet上下文(child)

从Spring Framework API 3.2.2在写这个时候)为 WebApplicationContext (强调我的):


像通用应用程序上下文一样,Web应用程序上下文
分层。 每个应用程序有一个单独的根上下文,而
应用程序中的每个servlet(包括
MVC框架中的调度程序servlet)具有自己的子上下文


此处还有:上下文层次结构


例如,如果您正在开发Spring MVC Web应用程序,则
通常将通过Spring的
ContextLoaderListener 和子WebApplicationContext加载根WebApplicationContext通过
Spring的DispatcherServlet
。这导致父子上下文
层次结构,其中共享组件和基础设施配置是
,在根环境中声明,并在子上下文中由
在Web特定的组件中使用。


这里: 17.2 DispatcherServlet


ApplicationContext实例在春天可以范围。在Web MVC
框架中,每个DispatcherServlet都有自己的WebApplicationContext,
继承了已经在根
WebApplicationContext
中定义的所有bean。这些继承的bean可以在
servlet特定的范围内被覆盖,您可以为给定的Servlet实例定义新的范围特定的bean




现在让我们看看根应用程序上下文配置。以下是一个示例:

web.xml

 <?xml version =1.0encoding =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>



从官方的Spring文档(强调我的):

5.14.4 Web应用程序的方便的ApplicationContext实例


您可以声明性地创建ApplicationContext实例通过使用
,例如ContextLoader。当然你也可以通过使用
ApplicationContext实现程序来创建
ApplicationContext实例。



你可以使用ContextLoaderListener
(参见上面的示例)



侦听器检查contextConfigLocation参数。 如果
参数不存在,则侦听器将使用
/WEB-INF/applicationContext.xml作为默认值
。当参数
存在时,监听器使用预定义的
分隔符(逗号,分号和空格)分隔String,并将值作为
将应用程序上下文搜索的位置。支持Ant样式路径
模式。示例是$ W
的所有文件的名称以Context.xml结尾的文件的/WEB-INF/*Context.xml,位于
WEB-INF目录和/ WEB-INF / **对于WEB-INF的任何子目录中的所有这样的文件
,/*Context.xml。




很多时候,Spring配置是跨多个文件分割的。更合理,更方便,特别是在大型项目中。在我们的示例中,我们在自定义位置中明确定义了两个配置XML文件: daoContext.xml applicationContext.xml / WEB-INF / spring / 。再次,如果我们没有定义contextConfigLocation ,ContextLoaderListener 将尝试找到默认的配置文件: /WEB-INF/applicationContext.xml



注意:

根上下文是可选的。另请参阅此回答: http://stackoverflow.com/a/7451389/814702



因此,如果默认的 /WEB-INF/applicationContext.xml 配置文件不符合您的需要,请使用 ContextLoaderListener 以及< context-param> contextConfigLocation 可以在其中定义自定义配置文件定义根应用程序环境





接下来,我们来看看个人(子)应用程序上下文。从官方的Spring文档(强调我的):

17.2 DispatcherServlet


初始化一个DispatcherServlet,Spring MVC在
Web应用程序的WEB-INF目录
中查找名为

[servlet-name] -servlet.xml的文件,并创建在那里定义的bean,覆盖在全局
范围内以相同名称定义的任何bean的
定义。



考虑以下DispatcherServlet Servlet配置(在$ b中$ b web.xml文件):




 < 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>




关于contextConfigLocation和命名空间



从文档(强调我的):


使用上述Servlet您将需要在您的应用程序中有一个名为

/WEB-INF/golfing-servlet.xml 的文件;这个
文件将包含所有的Spring Web MVC特定的组件
(beans)。您可以通过Servlet初始化参数来更改此配置文件
的确切位置(有关详细信息,请参阅下文)。

...

您可以自定义个人通过将
Servlet初始化参数(init-param元素)添加到web.xml文件中的Servlet
声明中,将DispatcherServlet实例
。有关支持参数的
的列表,请参见下表。




  • contextClass :实现WebApplicationContext的类,它实例化了此Servlet使用的上下文。默认情况下,使用XmlWebApplicationContext。


  • contextConfigLocation :传递给上下文实例的字符串(由contextClass指定)以指示可以找到上下文的位置。该字符串可能包含多个字符串(使用逗号作为分隔符)来支持多个上下文。
    如果具有两次定义的bean的多个上下文位置,则最新的位置优先。


  • 命名空间 :WebApplicationContext的命名空间。默认为[servlet-name] -servlet。





现在让我们研究相关类的API文档。 DispatcherServlet 扩展了抽象类 FrameworkServlet 。从 FrameworkServlet API文档(强调我的):


将contextConfigLocationservlet init-param传递给上下文
实例,将其解析成潜在的多个文件路径,可以通过任意数量的逗号和空格分隔
,如

test-servlet.xml,
myServlet .XML。 如果没有明确指定,上下文
实现应该从servlet的
命名空间
构建一个默认位置。



默认命名空间是'servlet-name'-servlet,例如test-servlet
用于servlet-nametest(导致带有XmlWebApplicationContext的/WEB-INF/test-servlet.xml
默认位置)。 命名空间
也可以通过namespaceservlet init-param
显式设置。


这是摘自 FrameworkServlet 源代码:

FrameworkServlet.java

  .... 
/ **
* WebApplicationContext命名空间的后缀。如果这个类的servlet是
*,在上下文中给出了名称test,则servlet使用的命名空间将
*解析为test-servlet。
* /
public static final String DEFAULT_NAMESPACE_SUFFIX =-servlet;
....



默认的上下文类为 FrameworkServlet XmlWebApplicationContext 。从 XmlWebApplicationContext API文档(强调我的):


默认情况下,配置将从
/WEB-INF/applicationContext.xml用于根上下文,
/WEB-INF/test-servlet.xml用于名称空间
test-servlet(像
servlet-nametest的DispatcherServlet实例一样。



配置位置默认值可以通过
ContextLoader的ContextConfigLocationcontext-param和FrameworkServlet的servlet
init-param
。配置位置可以表示
具体文件,如/WEB-INF/context.xml或Ant样式模式,如
/WEB-INF/*-context.xml(请参阅​​PathMatcher javadoc for pattern
详细资料)


使用 contextConfigLocation 覆盖默认配置位置对于根应用程序上下文,与上述示例相同。



对于覆盖默认命名空间的有一些重要的时刻。当您设置新的命名空间时,不要使用 / WEB-INF 添加它,并且不附加 .xml 。如果我们查看源文件中的 XmlWebApplicationContext 类,可以发现原因:

XmlWebApplicationContext.java

  ... 

/ **默认配置根上下文的位置* /
public static final String DEFAULT_CONFIG_LOCATION =/WEB-INF/applicationContext.xml;

/ **为命名空间构建配置位置的默认前缀* /
public static final String DEFAULT_CONFIG_LOCATION_PREFIX =/ WEB-INF /;

/ **为命名空间构建配置位置的默认后缀* /
public static final String DEFAULT_CONFIG_LOCATION_SUFFIX =.xml;

...

/ **
*根上下文的默认位置为/WEB-INF/applicationContext.xml,
*和/WEB-INF/test-servlet.xml,用于名称空间test-servlet
*的上下文(如同具有servlet-nametest的DispatcherServlet实例)。
* /
@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};
}
}

如您所见,源代码说这一切。






指定自定义命名空间的示例



web.xml

 <?xml版本=1.0encoding =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>


<! - DispatcherServlet应用程序上下文(子上下文)的配置 - >
< 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>

结果是,而不是使用默认命名空间来构建配置文件的路径,否则将是 /WEB-INF/spring-mvc-servlet.xml ,容器将寻找 / WEB-INF / spring / mvc / spring -mvc.xml



注意:

与设置自定义命名空间相关的上述说明是默认的< a href =http://docs.spring.io/spring/docs/3.2.2.RELEASE/javadoc-api/org/springframework/web/context/support/XmlWebApplicationContext.html =noreferrer> XmlWebApplicationContext 上下文类。可以指定一个替代类,如 AnnotationConfigWebApplicationContext ,所以会有一些特别的时刻。






结论



(IMHO)更容易使用 contextConfigLocation 参数来定义自定义配置文件,用于根应用程序上下文和各个上下文。唯一的区别是,对于根应用程序上下文,您可以使用< web-app>中的 < context-param> ; 元素,但不在特定的servlet中(也不要忘记监听器类)。而对于子上下文,您可以使用嵌套在< servlet> < init-param> $ c>元素每个特定的servlet 。在这篇文章的最开始,请参阅我的示例配置( web.xml )。



其他来源(如上所述还不够: - )):





另请参阅这些答案:




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?

解决方案

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 use contextConfigLocation 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

<?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's get some important moments clear. There are two types of contexts we are dealing with:

  1. root context (parent)
  2. individual servlet context (child)

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

Like generic application contexts, web application contexts are hierarchical. 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.

Also here: Context hierarchies:

For example, if you are developing a Spring MVC web application you will typically have a root WebApplicationContext loaded via Spring's ContextLoaderListener and a child WebApplicationContext loaded via Spring's DispatcherServlet. 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.

And here: 17.2 The DispatcherServlet:

ApplicationContext instances in Spring can be scoped. In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. 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.


Now let's see the root application context configuration. 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 (emphasis mine):
5.14.4 Convenient ApplicationContext instantiation for web applications:

You can create ApplicationContext instances declaratively by using, for example, a ContextLoader. Of course you can also create ApplicationContext instances programmatically by using one of the ApplicationContext implementations.

You can register an ApplicationContext using the ContextLoaderListener (see the example above)

The listener inspects the contextConfigLocation parameter. If the parameter does not exist, the listener uses /WEB-INF/applicationContext.xml as a default. 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".


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: http://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

Upon initialization of a DispatcherServlet, Spring MVC looks for a file named
[servlet-name]-servlet.xml in the WEB-INF directory
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.

Consider the following DispatcherServlet Servlet configuration (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 contextConfigLocation and namespace

From the documentation (emphasis mine):

With the above Servlet configuration in place, you will need to have a file called
/WEB-INF/golfing-servlet.xml 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).
...
You can customize individual DispatcherServlet instances 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.

  • contextClass: Class that implements WebApplicationContext, which instantiates the context used by this Servlet. By default, the XmlWebApplicationContext is used.

  • contextConfigLocation: 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.

  • namespace: Namespace of the WebApplicationContext. Defaults to [servlet-name]-servlet.


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):

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
"test-servlet.xml, myServlet.xml". If not explicitly specified, the context implementation is supposed to build a default location from the namespace of the servlet.

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). The namespace can also be set explicitly via the "namespace" servlet init-param.

This is the excerpt from the FrameworkServlet source code:
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 docs (emphasis mine):

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").

The config location defaults can be overridden via the "contextConfigLocation" context-param of ContextLoader and servlet init-param of FrameworkServlet. 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).

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

...

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


Example of specifying custom namespace

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>

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 sources (as if the above was not enough :-)):

Also see these answers:

这篇关于Spring:web.xml中的命名空间与contextConfigLocation init参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆