栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring:Web.xml中的命名空间与contextConfigLocation初始化参数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring:Web.xml中的命名空间与contextConfigLocation初始化参数

TL; DR
只需

contextConfigLocation
在需要指定自定义配置文件时为设置值。这样,你将同时指定配置文件名称及其位置。

namespace
本质上讲,这是告诉Spring容器上下文加载器类要使用哪个配置文件的另一种方法。我从不理会它,只
contextConfigLocation
在需要配置自定义配置文件时使用。

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

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>/adminpublic static final String DEFAULT_NAMESPACE_SUFFIX = "-servlet";....

frameworkServlet 的默认上下文类是XmlWebApplicationContext。从XmlWebApplicationContext API文档(重点是我的):

默认情况下,配置将从“ /WEB-INF/applicationContext.xml”获取根上下文,从“ /WEB-INF/test-servlet.xml”获取具有名称空间“ test-servlet”的上下文(例如Servlet名称为“ test”的DispatcherServlet实例)。

可以通过ContextLoader的“ contextConfigLocation”上下文参数和frameworkServlet的servlet init-param覆盖配置位置的默认值。配置位置可以表示诸如“ /WEB-INF/context.xml”之类的具体文件,也可以表示诸如“ /WEB-INFpublic 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";...@Overrideprotected 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 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>

结果是,

/WEB-INF/spring-mvc-servlet.xml
容器将寻找,而不是使用默认的名称空间来构造配置文件的路径
/WEB-INF/spring/mvc/spring-mvc.xm
l。

注意:
与设置自定义名称空间有关的上述说明适用于默认XmlWebApplicationContext上下文类。可以指定一个替代类,例如AnnotationConfigWebApplicationContext,因此会有一些特殊的时刻。

结论
(IMHO)使用

contextConfigLocation
参数定义根目录应用程序上下文和单个上下文的自定义配置文件要容易得多。唯一的区别是,对于根应用程序上下文,你可以
<context-param>
<web-app>
元素内使用,但不能在特定的servlet内使用(也不要忘记监听器类)。对于子上下文,你对每个特定的servlet使用
<init-param>
嵌套在
<servlet>
元素内。请参阅本文开头的示例配置(web.xml)。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/398694.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号