applicationContext.xml和dispatcher-servlet.xml的区别
在SpringMVC项目中我们一般会引入applicationContext.xml和dispatcher-servlet.xml两个配置文件,这两个配置文件具体的区别是什么呢?
Spring 官方文档介绍如下:
Spring lets you define multiple contexts in a parent-child hierarchy.
The applicationContext.xml defines the beans for the "root webapp context", i.e. the context associated with the webapp.
The spring-servlet.xml (or whatever else you call it) defines the beans for one servlet's app context. There can be many of these in a webapp, one per Spring servlet (e.g. spring1-servlet.xml for servlet spring1, spring2-servlet.xml for servlet spring2).
Beans in spring-servlet.xml can reference beans in applicationContext.xml, but not vice versa.
All Spring MVC controllers must go in the spring-servlet.xml context.
In most simple cases, the applicationContext.xml context is unnecessary. It is generally used to contain beans that are shared between all servlets in a webapp. If you only have one servlet, then there's not really much point, unless you have a specific use for it.
可见, applicationContext.xml 和 dispatch-servlet.xml形成了两个父子关系的上下文。
1) 一个bean如果在两个文件中都被定义了(比如两个文件中都定义了component scan扫描相同的package), spring会在application context和 servlet context中都生成一个实例,他们处于不同的上下文空间中,他们的行为方式是有可能不一样的。
2) 如果在application context和 servlet context中都存在同一个 @Service 的实例, controller(在servlet context中) 通过 @Resource引用时, 会优先选择servlet context中的实例。
不过最好的方法是:在applicationContext和dispatcher-servlet定义的bean最好不要重复, dispatcher-servlet最好只是定义controller类型的bean。
----------------------------------------------------------------------------------------------------------------------------------------
ApplicationContext.xml 是spring 全局配置文件,用来控制spring 特性的
dispatcher-servlet.xml 是spring mvc里面的,控制器、拦截uri转发view
使用applicationContext.xml文件时是需要在web.xml中添加listener的:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
applicationContext.xml和dispatcher-servlet.xml的区别的更多相关文章
- Tomcat中的Web.xml和servlet.xml的学习
Web.xml文件使用总结 作用: 存储项目相关的配置信息,保护servlet.解耦一些数据对程序的依赖 使用位置: 每个web项目中 Tomcat服务器中(在服务器目录conf目录中) 区别: We ...
- Tomcat配置文件之servlet.xml中选项介绍
Servlet.xml 分为以下元素: server, service, Connector ( 表示客户端和service之间的连接), Engine ( 表示指定service 中的请求处理机,接 ...
- XML引入以及与html的区别
1.1 引入 HTML: 负责网页的结构 CSS: 负责网页的样式(美观) Javascript: 负责在浏览器端与用户进行交互. 负责静态的网页制作的语言 HTML语言特点: 1)由标签组成. &l ...
- web.xml:<dispatcher>
web.xml 的 <dispatcher> 是 <filter-mapping> 下的子标签,指定 Filter 对应的请求方式,其可选值如下: REQUEST 客户端在地址 ...
- tomcat中配置servlet.xml的JNDI或JDBC连接数据库【原】
tomcat中配置servlet.xml的JNDI或JDBC连接数据库 一. JNDI 1. tomcat环境 找到X:\xxx\......\apache-tomcat-6.0.39\conf\se ...
- SpringBoot日志logback-spring.xml分环境log4j logback slf4j区别 springboot日志设置
转载. https://blog.csdn.net/qianyiyiding/article/details/76565810 springboot按照profile进行打印日志log4j logba ...
- servlet.xml 出现 Referenced file contains errors(http://.......)
问题描述: 打开Eclipse突然发现Web工程的servlet.xml突然报了红叉叉,错误信息如下: Referenced file contains errors (http://www.spri ...
- idea工程中web.xml报错Servlet should have a mapping
搭建ssm工程过程中web.xml报错:Servlet should have a mapping ....但是mapping已经配置好了...如下图: 搜索无果,后来发现是工程的web.xml位置配 ...
- as3 XML类和XMLList类的区别
一.XML类和XMLList类的区别 AS3.0中,处理XML主要用到两个主类,XML类和XMLList类,这两个类的很多内容是共通的.应该有人会问,XML和XMLList的区别是什么? ...
- XML中PCDATA与CDATA的区别
XML中PCDATA与CDATA的区别 2011-02-10 19:27:25| 分类: XML | 标签:xml中pcdata与cdata的区别 字号:大中小 订阅 所有 XML 文档中 ...
随机推荐
- 理解 OpenStack + Ceph (2):Ceph 的物理和逻辑结构 [Ceph Architecture]
本系列文章会深入研究 Ceph 以及 Ceph 和 OpenStack 的集成: (1)安装和部署 (2)Ceph RBD 接口和工具 (3)Ceph 物理和逻辑结构 (4)Ceph 的基础数据结构 ...
- [转]asp.net c# 网上搜集面试题目(附答案)
本文转自:http://www.cnblogs.com/hndy/articles/2234188.html 1.String str=new String("a")和String ...
- BUCK-BOOST反激变压器设计
Buck-Boost电路中,最低电压为其最恶劣情况 以下图为例: 注:1.Np为初级绕组匝数,Ns为次级绕组匝数: 2.Vmos为MOS最大耐压值,1为整流管压降,Vl为漏,Vl=100V,Vmos选 ...
- AC日记——过滤多余的空格 1.7 23
23:过滤多余的空格 总时间限制: 1000ms 内存限制: 65536kB 描述 一个句子中也许有多个连续空格,过滤掉多余的空格,只留下一个空格. 输入 一行,一个字符串(长度不超过200), ...
- 能跨域和跨浏览器的flashcookie for jquery插件
对于写网站时需要跨域和跨浏览器的可以看看这个. 引入jquery 和 swfstore.min.js 就可以了,蛮简单好用的,会jquery基础就可以咯. mySwfStore.set('myKey ...
- 逗号分隔的字符串转换为行数据(collection)
逗号分隔的字符串转换为行数据(collection) CREATE OR REPLACE FUNCTION "GET_STR_TAB" (v_str in varchar2) re ...
- ubuntu下nginx+php5的部署
ubuntu下nginx+php5环境的部署和centos系统下的部署稍有不同,废话不多说,以下为操作记录:1)nginx安装root@ubuntutest01-KVM:~# sudo apt-get ...
- 使用 JavaScript File API 实现文件上传
概述 以往对于基于浏览器的应用而言,访问本地文件都是一件头疼的事情.虽然伴随着 Web 2.0 应用技术的不断发展,JavaScript 正在扮演越来越重要的角色,但是出于安全性的考虑,JavaScr ...
- fMRI数据分析处理原理及方法
来源: 整理文件的时候翻到的,来源已经找不到了囧感觉写得还是不错,贴在这里保存. 近年来,血氧水平依赖性磁共振脑功能成像(Blood oxygenation level-dependent funct ...
- hadoop资料收集
大数据时代——为什么用hadoop hadoop应用场景 Hadoop一般用在哪些业务场景? Hadoop虽然强大,但不是万能的