部署到tomcat后,src目录下的配置文件会和class文件一样,自动copy到应用的 classes目录下

spring的 配置文件在启动时,加载的是web-info目录下的applicationContext.xml,
运行时使用的是web-info/classes目录下的applicationContext.xml。

配置web.xml使这2个路径一致:

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

在web.xml中通过contextConfigLocation配置spring,contextConfigLocation 参数定义了要装入的 Spring 配置文件。

如果想装入多个配置文件,可以在 <param-value> 标记中用逗号作分隔符。
<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,
    classpath*:conf/spring/applicationContext_staff*.xml,
    classpath*:conf/spring/applicationContext_security.xml
    classpath*:conf/spring/applicationContext_modules*.xml
    classpath*:conf/spring/applicationContext_cti*.xml
    classpath*:conf/spring/applicationContext_apm*.xml
  </param-value>
</context-param>

contextConfigLocation 参数定义了要装入的 Spring 配置文件。

首先与Spring相关的配置文件必须要以"applicationContext-"开头,要符合约定优于配置的思想,这样在效率上和出错率上都要好很多。
还有最好把所有Spring配置文件都放在一个统一的目录下,如果项目大了还可以在该目录下分模块建目录。这样程序看起来不会很乱。
在web.xml中的配置如下:
Xml代码
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:**/applicationContext-*.xml</param-value>
</context-param>

"**/"表示的是任意目录;
"**/applicationContext-*.xml"表示任意目录下的以"applicationContext-"开头的XML文件。
你自己可以根据需要修改。最好把所有Spring配置文件都放在一个统一的目录下,如:

<!-- Spring 的配置 -->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:/spring/applicationContext-*.xml</param-value>
</context-param>

web.xml中classpath:和classpath*:, 有什么区别?

classpath:只会到你的class路径中查找找文件;
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.

在web.xml里配置Listener

xml 代码如下: 
   <listener>   
        <listener-class>org.springframework.web.context.ContextLoaderListener<listener-class>   
   </listener>    

如果在web.xml里给该Listener指定要加载的xml,如:

xml代码如下:

<!-- spring config -->

<context-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>classpath:applicationContext.xml</param-value>

</context-param>

则会去加载相应的xml,而不会去加载/WEB-INF/下的applicationContext.xml。

但是,如果没有指定的话,默认会去/WEB-INF/下加载applicationContext.xml。

web.xml 通过contextConfigLocation配置spring 的方式的更多相关文章

  1. SpringMVC(十六):如何使用编程方式替代/WEB-INF/web.xml中的配置信息

    在构建springmvc+mybatis项目时,更常用的方式是采用web.xml来配置,而且一般情况下会在web.xml中使用ContextLoaderListener加载applicationCon ...

  2. J2EE进阶(五)Spring在web.xml中的配置

     J2EE进阶(五)Spring在web.xml中的配置 前言 在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制自动加载到容器中.在web ...

  3. Spring中,applicationContext.xml 配置文件在web.xml中的配置详解

    一.首先写一下代码结构. 二.再看web.xml中的配置情况. <?xml version="1.0" encoding="UTF-8"?> < ...

  4. 使用Spring时web.xml中的配置

    使用Spring时web.xml中的配置: <?xml version="1.0" encoding="UTF-8"?> <web-app x ...

  5. spring 和springmvc 在 web.xml中的配置

    (1)问题:如何在Web项目中配置Spring的IoC容器? 答:如果需要在Web项目中使用Spring的IoC容器,可以在Web项目配置文件web.xml中做出如下配置: <!-- Sprin ...

  6. web.xml常用元素配置

    tomcat服务器: tomcat是一个WEB服务器,所有的j2ee WEB程序可以在此处运行. tomcat服务器是一个符合j2ee标准的WEB服务器.则J2ee的EJB程序无法在此处运行. 如果要 ...

  7. web.xml的contextConfigLocation作用及自动加载applicationContext.xml

    web.xml的contextConfigLocation作用及自动加载applicationContext.xml 转自:http://blog.csdn.net/sapphire_aling/ar ...

  8. 关于web.xml的welcome-file-list 配置与tomcat的关系:

    关于web.xml的welcome-file-list 配置与tomcat的关系: 2018年04月18日 10:17:13 守望dfdfdf 阅读数:377 标签: welcome-file-lis ...

  9. web.xml的常见配置

    web.xml的常见配置 <!-- 配置全局的编码过滤器 --> <filter> <description>编码过滤器</description> & ...

随机推荐

  1. 6-1 如何读写csv数据

    >>> from urllib import urlretrieve >>> urlretrieve('http://table.finance.yahoo.com ...

  2. TMS320F28335——下载程序到flash中

    一.让CCS软件支持Flash烧写 添加F28335.cmd文件 如图屏蔽掉25335_RAM_lnk.cmd 2.支持从Flash中拷贝文件到RAM中 添加DSP2832x_MemCopy.c 在主 ...

  3. django模板传入参数的处理方式与反向生成url

    前端模板传入参数的处理方式 1.传入单个参数: 前端使用href="/sel-{{ row.0 }}.html, url使用url(r'sel-(.+).html',home.index), ...

  4. 搬家至csdn

    搬家至csdn https://blog.csdn.net/qq_42866164

  5. 免费申请 QQ 免费靓号

    打开网址:https://ssl.zc.qq.com/v3/index-chs.html?type=3 说明:靓号一般分为 9位靓号 or 带有寓意的号码 激活规则:获取的"靓号" ...

  6. Linux学习--第三天--linux文件目录、ls、mkdir、mv、rm、touch、cat、tac、more、less、head、tail、ln、chmod、chown、chgrp、umask

    文件目录 目录名 备注 bin 下面的命令所有人都可以运行 sbin 只有root才能运行,s代表super /mnt,/media,/misc 都是挂载目录,但一般只用mnt /opt 第三方软件安 ...

  7. <转>ThinkPHP的开发常用系统配置项

    /* 项目设定 */ ’APP_DEBUG’ => false, // 是否开启调试模式 ’APP_DOMAIN_DEPLOY’ => false, // 是否使用独立域名部署项目 ’AP ...

  8. APIO2019 题解

    APIO2019 题解 T1 奇怪装置 题目传送门 https://loj.ac/problem/3144 题解 很容易发现,这个东西一定会形成一个环.我们只需要求出环的长度就解决了一切问题. 设环的 ...

  9. @RequestBody、@RequestParam、@PathVariable区别与使用场景

    由于项目是前后端分离,因此后台使用的是spring boot,做成微服务,只暴露接口.接口设计风格为restful的风格,在get请求下,后台接收参数的注解为RequestBody时会报错:在post ...

  10. 郭盛华现身北京机场,颇有IT男的风范,网友:疑似被招安了

    郭盛华纵横互联网江湖数十年,他白手起家,凭着过人的勇敢.智慧和绝技,身经百战,显赫辉煌,成为中外闻名的互联网安全领域大师级人物. 郭盛华的网络技术指导方面经验丰富实力深厚.他是中国互联网安全领域的传奇 ...