一、首先写一下代码结构。

二、再看web.xml中的配置情况。

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

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

  <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderListener是Spring的监听器,它的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。
  <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中的配置就如下所示:

    <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

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

    <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下面,都可以用下面这种方式来配置路径:

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

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

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

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

    <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:**/applicationContext-*.xml</param-value>
</context-param>

"**/"表示的是任意目录;

"**/applicationContext-*.xml"表示任意目录下的以"applicationContext-"开头的XML文件。

Spring配置文件最好以"applicationContext-"开头,且最好把所有Spring配置文件都放在一个统一的目录下,也可以分模块创建。

Spring中,applicationContext.xml 配置文件在web.xml中的配置详解的更多相关文章

  1. (转)python中调用R语言通过rpy2 进行交互安装配置详解

    python中调用R语言通过rpy2 进行交互安装配置详解(R_USER.R_HOME配置) 2018年11月08日 10:00:11 luqin_ 阅读数:753   python中调用R语言通过r ...

  2. Nginx中的root&alias文件路径及索引目录配置详解

    这篇文章主要介绍了Nginx中的root&alias文件路径及索引目录配置,顺带讲解了root和alias命令的用法,需要的朋友可以参考下     root&alias文件路径配置ng ...

  3. web自动化开发环境配置详解

    1.安装 nodejs Grunt和所有grunt插件都是基于nodejs来运行的, https://nodejs.org/ 安装完成之后在终端 node -v 查看安装版本 2.安装 grunt-C ...

  4. 跟我学Spring Cloud(Finchley版)-20-Spring Cloud Config-Git仓库配置详解 原

    在跟我学Spring Cloud(Finchley版)-19-配置中心-Spring Cloud Config 一节中,已实现使用Git仓库作为Config Server的后端存储,本节详细探讨如何配 ...

  5. zookeeper 配置文件conf目录下 zoo文件 配置详解

    clientPort:客户端尝试连接server的端口号 (对外服务端口) 通常为2181 dataDir:存放快照文件snapshot的目录.(储存数据的目录) dataLogDir:事务日志存放位 ...

  6. Servlet中Web.xml的配置详解

    1 定义头和根元素 部署描述符文件就像所有XML文件一样,必须以一个XML头开始.这个头声明可以使用的XML版本并给出文件的字符编码. DOCYTPE声明必须立即出现在此头之后.这个声明告诉服务器适用 ...

  7. Spring 入门 web.xml配置详解

    Spring 入门 web.xml配置详解 https://www.cnblogs.com/cczz_11/p/4363314.html https://blog.csdn.net/hellolove ...

  8. Servlet中Web.xml配置详解(二)

    5.2 分配JSP初始化参数给JSP页面提供初始化参数在三个方面不同于给servlet提供初始化参数.1)使用jsp-file而不是servlet-class.因此,WEB-INF/web.xml文件 ...

  9. 释放SQL Server占用的内存 .Net 读取xml UrlReWriter 在web.config中简单的配置

    释放SQL Server占用的内存   由于Sql Server对于系统内存的管理策略是有多少占多少,除非系统内存不够用了(大约到剩余内存为4M左右),Sql Server才会释放一点点内存.所以很多 ...

随机推荐

  1. node.js创建简单服务测试请求数据

    工具:安装node: 1,创建文件夹 server, 2 ,在server文件夹下分别创建server.js 和 package.json 文件 3,server.js 代码: var express ...

  2. Oracle使用备忘

    初学Oracle,很多语句记不住,写在这里备忘. 1.查看某表空间的数据文件 select file_name 文件名, tablespace_name 表空间名, bytes 已使用大小M, max ...

  3. MySQL Error--The Table is full

    问题描述 在MySQL 错误日志中发下以下错误信息:[ERROR] /export/servers/mysql/bin/mysqld: The table '#sql-xxxx-xxx' is ful ...

  4. DevExpress GridView 整理(转)

    DevExpress GridView 那些事儿 1:去除 GridView 头上的 "Drag a column header here to group by that column&q ...

  5. Centos6.8通过yum安装mysql5.7 centos7.5适用

    1.安装mysql的yum源 a.下载配置mysql的yum源的rpm包 根据上面3张图片中的操作下载下来的rpm文件可以通过如下命令获取: wget https://dev.mysql.com/ge ...

  6. Spring Boot使用单元测试

    一.Service层单元测试: 代码如下: package com.dudu.service;import com.dudu.domain.LearnResource;import org.junit ...

  7. nginx的启动与停止

    参考 :http://www.cnblogs.com/codingcloud/p/5095066.html 启动: /usr/local/nginx/sbin/nginx -c /usr/local/ ...

  8. JVM优化系列之一(-Xss调整Stack Space的大小)

    Java程序中,每个线程都有自己的Stack Space(堆栈).这个Stack Space不是来自Heap的分配.所以Stack Space的大小不会受到-Xmx和-Xms的影响,这2个JVM参数仅 ...

  9. word文档内容如何防止被复制

    word2016 审阅->限制编辑->1格式设置编辑 and 2编辑限制->3是,启动强制保护->输入秘密

  10. note 0 Python介绍及Python IDE环境安装 Spyder with Anaconda

    高级语言分类 编译型语言(C/C++等) 解释型语言(BASIC.Python等) Python 诞生于1989年,创始人为吉多 范罗苏姆(Guido van Rossum) Python 语言特点 ...