前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring MVC中,applicationContext.xml [ServletName]-servlet.xml配置文件在web.xml中的配置详解Spring MVC中,applicatio

Spring MVC中,applicationContext.xml [ServletName]-servlet.xml配置文件在web.xml中的配置详解Spring MVC中,applicatio

作者头像
一个会写诗的程序员
发布2018-08-20 10:54:29
1.4K0
发布2018-08-20 10:54:29
举报
文章被收录于专栏:一个会写诗的程序员的博客

Spring MVC中,applicationContext.xml [ServletName]-servlet.xml配置文件在web.xml中的配置详解

代码语言:javascript
复制
<!-- spring 配置 --><servlet><servlet-name>swork</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>swork</servlet-name><url-pattern>/*</url-pattern></servlet-mapping>

这样配置,SpringMVC会自动按照约定去找<servlet-name>swork</servlet-name>的swork-servlet.xml配置文件作为其servlet配置文件.

如果是显式指定Springmvc-servlet.xml, 加上

代码语言:javascript
复制
<init-param> <param-name>contextConfigLocation</param-name> <!-- <param-value>classpath*:config/Springmvc-servlet.xml</param-value> --> <param-value>/WEB-INF/classes/config/Springmvc-servlet.xml</param-value></init-param>

如下:

代码语言:javascript
复制
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- <param-value>classpath*:config/Springmvc-servlet.xml</param-value> --> <param-value>/WEB-INF/classes/config/Springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>

看web.xml中的配置情况。

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringMVC</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <!-- <param-value>classpath*:config/applicationContext.xml</param-value> --> <param-value>/WEB-INF/classes/config/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- <param-value>classpath*:config/Springmvc-servlet.xml</param-value> --> <param-value>/WEB-INF/classes/config/Springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>

三、下面是对配置文件的说明。

代码语言:javascript
复制
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

ContextLoaderListener是Spring的监听器,它的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。

代码语言:javascript
复制
<context-param> <param-name>contextConfigLocation</param-name> <!-- <param-value>classpath*:config/applicationContext.xml</param-value> --> <param-value>/WEB-INF/classes/config/applicationContext.xml</param-value> </context-param>

这段配置是用于指定applicationContext.xml配置文件的位置,可通过context-param加以指定: 这里需要搞清楚classpath是什么,以及classpath:和classpath*有何区别:

  1. 首先 classpath是指 WEB-INF文件夹下的classes目录
  2. classpath 和 classpath* 区别: classpath:只会到你的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.

如果applicationContext.xml配置文件存放在src目录下,就好比上面的代码结构中的存放位置,那么在web.xml中的配置就如下所示:

代码语言:javascript
复制
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>

如果applicationContext.xml配置文件存放在WEB-INF下面,那么在web.xml中的配置就如下所示:

代码语言:javascript
复制
<context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/applicationContext*.xml</param-value> </context-param>

需要注意的是,部署到应用服务器后,src目录下的配置文件会和class文件一样,自动copy到应用的 classes目录下,spring的 配置文件在启动时,加载的是web-info目录下的applicationContext.xml, 运行时使用的是web-info/classes目录下的applicationContext.xml。因此,不管applicationContext.xml配置文件存放在src目录下,还是存放在WEB-INF下面,都可以用下面这种方式来配置路径:

代码语言:javascript
复制
<context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/applicationContext*.xml</param-value> </context-param>

当有多个配置文件加载时,可采用下面代码来配置:

代码语言:javascript
复制
![复制代码](http://upload-images.jianshu.io/upload_images/1233356-61a1170cf5642d48.gif?imageMogr2/auto-orient/strip)
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:conf/spring/applicationContext_core*.xml, classpath*:conf/spring/applicationContext_dict*.xml, classpath*:conf/spring/applicationContext_hibernate.xml,...... </param-value> </context-param>
[图片上传中。。。(4)]

也可以用下面的这种方式:

代码语言:javascript
复制
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:**/applicationContext-*.xml</param-value> </context-param>

"/"表示的是任意目录; "/applicationContext-*.xml"表示任意目录下的以"applicationContext-"开头的XML文件。 Spring配置文件最好以"applicationContext-"开头,且最好把所有Spring配置文件都放在一个统一的目录下,也可以分模块创建。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017.01.14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Spring MVC中,applicationContext.xml [ServletName]-servlet.xml配置文件在web.xml中的配置详解
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档