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

二、再看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. python中表示False的一些内置对象

    By default, an object is considered true unless its class defines either a __bool__() method that re ...

  2. 【java多线程】队列系统之说说队列Queue

    转载:http://benjaminwhx.com/2018/05/05/%E8%AF%B4%E8%AF%B4%E9%98%9F%E5%88%97Queue/ 1.简介 Queue(队列):一种特殊的 ...

  3. 使用新标签兼容低版本IE

    HTML语义化 意义:根据内容的结构化(语义化),选择合适的标签,便于开发者阅读和写出更优雅的代码,同时让流浪器的爬虫和机器更好的解析. 尽可能少的使用无语义的标签 div 和 span 在语义不明显 ...

  4. Linux内核分析第七次作业

    分析Linux内核创建一个新进程的过程 Linux中创建进程一共有三个函数: 1. fork,创建子进程 2. vfork,与fork类似,但是父子进程共享地址空间,而且子进程先于父进程运行. 3. ...

  5. Eclipse Java EE IDE for Web Developers集成的Maven 3 指向自己安装的 Maven

    一.配置Maven环境 1.下载apache-maven文件,选择自己需要的版本,地址:Apache 官方下载地址是http://maven.apache.org/download.cgi 2.下载并 ...

  6. Linux中安装python3.6和第三方库

    Linux中安装python3.6和第三方库 如果本机安装了python2,尽量不要管他,使用python3运行python脚本就好,因为可能有程序依赖目前的python2环境,比如yum!!!!! ...

  7. TF(1): 基础理论

    TensorFlow最初由Google大脑的研究员和工程师开发出来,用于机器学习和神经网络方面的研究,于2015.10宣布开源,在众多深度学习框架中脱颖而出,在Github上获得了最多的Star量.T ...

  8. 黄聪:分享几个免费IP地址查询接口(API)

    淘宝IP地址库 提供的服务包括:1. 根据用户提供的IP地址,快速查询出该IP地址所在的地理信息和地理相关的信息,包括国家.省.市和运营商.2. 用户可以根据自己所在的位置和使用的IP地址更新我们的服 ...

  9. [UE4]Tile View

    一.Tile View也属于List View,Tile View以小方格的形式展示子控件. 二.Tile View.Entry Height.Tile View.Entry Width设置每个Til ...

  10. toString 和new String()区别

    public class NewStringTestDemo { public static void main(String[] args) { String s = "你好"; ...