web.xml 通过contextConfigLocation配置spring 的方式
部署到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
如果在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 的方式的更多相关文章
- SpringMVC(十六):如何使用编程方式替代/WEB-INF/web.xml中的配置信息
在构建springmvc+mybatis项目时,更常用的方式是采用web.xml来配置,而且一般情况下会在web.xml中使用ContextLoaderListener加载applicationCon ...
- J2EE进阶(五)Spring在web.xml中的配置
J2EE进阶(五)Spring在web.xml中的配置 前言 在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制自动加载到容器中.在web ...
- Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
一.首先写一下代码结构. 二.再看web.xml中的配置情况. <?xml version="1.0" encoding="UTF-8"?> < ...
- 使用Spring时web.xml中的配置
使用Spring时web.xml中的配置: <?xml version="1.0" encoding="UTF-8"?> <web-app x ...
- spring 和springmvc 在 web.xml中的配置
(1)问题:如何在Web项目中配置Spring的IoC容器? 答:如果需要在Web项目中使用Spring的IoC容器,可以在Web项目配置文件web.xml中做出如下配置: <!-- Sprin ...
- web.xml常用元素配置
tomcat服务器: tomcat是一个WEB服务器,所有的j2ee WEB程序可以在此处运行. tomcat服务器是一个符合j2ee标准的WEB服务器.则J2ee的EJB程序无法在此处运行. 如果要 ...
- web.xml的contextConfigLocation作用及自动加载applicationContext.xml
web.xml的contextConfigLocation作用及自动加载applicationContext.xml 转自:http://blog.csdn.net/sapphire_aling/ar ...
- 关于web.xml的welcome-file-list 配置与tomcat的关系:
关于web.xml的welcome-file-list 配置与tomcat的关系: 2018年04月18日 10:17:13 守望dfdfdf 阅读数:377 标签: welcome-file-lis ...
- web.xml的常见配置
web.xml的常见配置 <!-- 配置全局的编码过滤器 --> <filter> <description>编码过滤器</description> & ...
随机推荐
- input 限制 中文输入
ime-mode:disabled是什么? 解决: 1. ime-mode版本:IE5+专有属性 继承性:无 语法: ime-mode : auto | active | ina ...
- Python 循环异或对文件进行加解密
# -* -coding: UTF-8 -* - # 功能:异或方式对文件进行加密和解密 import os import datetime # 主函数 def main(): getInput() ...
- 关于代码手写UI,xib和StoryBoard
代码手写UI 这种方法经常被学院派的极客或者依赖多人合作的大型项目大规模使用.Geek们喜欢用代码构建UI,是因为代码是键盘敲出来的,这样可以做到不开IB,手不离开键盘就完成工作,可以专注于编码环境, ...
- Docker 安装、卸载、启动、停止
1.1 查看当前系统的内核版本 查看当前系统的内核版本是否高于 3.10 英文文档:https://docs.docker.com/ 中文文档:https://docs.docker-cn.com/ ...
- redHat 安装mono 错误
make[6]: * [do-install] Error 2 make[6]: Leaving directory /root/lindexi/mono-2.11.3/mcs/class/Syste ...
- CodeBlocks17.12+汉化包下载及用法
本文已迁移至:https://blog.csdn.net/COCO56/article/details/95228780
- VB中preserve的用法
注:本文转载自:http://zhidao.baidu.com/question/161401549.html ReDim 语句用来定义或重定义原来已经用带空圆括号(没有维数下标)的 Private. ...
- The Battle of Chibi(数据结构优化dp,树状数组)
The Battle of Chibi Cao Cao made up a big army and was going to invade the whole South China. Yu Zho ...
- 写了一个简单的 Mybatis
写了一个简单的 Mybatis,取名 SimpleMybatis . 具备增删改查的基本功能,后续还要添加剩下的基本数据类型和Java集合类型的处理. 脑图中有完整的源码和测试的地址 http://n ...
- 关于数据库抛出异常:Incorrect string value: '\xE1\x...' for column '字段名' at row 1 问题的解决方法
打开sql,进行语句编辑 ENGINE=InnoDB DEFAULT CHARSET=utf8;字符集设置utf-8编码