在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的区别的更多相关文章

  1. Tomcat中的Web.xml和servlet.xml的学习

    Web.xml文件使用总结 作用: 存储项目相关的配置信息,保护servlet.解耦一些数据对程序的依赖 使用位置: 每个web项目中 Tomcat服务器中(在服务器目录conf目录中) 区别: We ...

  2. Tomcat配置文件之servlet.xml中选项介绍

    Servlet.xml 分为以下元素: server, service, Connector ( 表示客户端和service之间的连接), Engine ( 表示指定service 中的请求处理机,接 ...

  3. XML引入以及与html的区别

    1.1 引入 HTML: 负责网页的结构 CSS: 负责网页的样式(美观) Javascript: 负责在浏览器端与用户进行交互. 负责静态的网页制作的语言 HTML语言特点: 1)由标签组成. &l ...

  4. web.xml:<dispatcher>

    web.xml 的 <dispatcher> 是 <filter-mapping> 下的子标签,指定 Filter 对应的请求方式,其可选值如下: REQUEST 客户端在地址 ...

  5. tomcat中配置servlet.xml的JNDI或JDBC连接数据库【原】

    tomcat中配置servlet.xml的JNDI或JDBC连接数据库 一. JNDI 1. tomcat环境 找到X:\xxx\......\apache-tomcat-6.0.39\conf\se ...

  6. SpringBoot日志logback-spring.xml分环境log4j logback slf4j区别 springboot日志设置

    转载. https://blog.csdn.net/qianyiyiding/article/details/76565810 springboot按照profile进行打印日志log4j logba ...

  7. servlet.xml 出现 Referenced file contains errors(http://.......)

    问题描述: 打开Eclipse突然发现Web工程的servlet.xml突然报了红叉叉,错误信息如下: Referenced file contains errors (http://www.spri ...

  8. idea工程中web.xml报错Servlet should have a mapping

    搭建ssm工程过程中web.xml报错:Servlet should have a mapping ....但是mapping已经配置好了...如下图: 搜索无果,后来发现是工程的web.xml位置配 ...

  9. as3 XML类和XMLList类的区别

    一.XML类和XMLList类的区别       AS3.0中,处理XML主要用到两个主类,XML类和XMLList类,这两个类的很多内容是共通的.应该有人会问,XML和XMLList的区别是什么? ...

  10. XML中PCDATA与CDATA的区别

    XML中PCDATA与CDATA的区别 2011-02-10 19:27:25|  分类: XML |  标签:xml中pcdata与cdata的区别     字号:大中小 订阅 所有 XML 文档中 ...

随机推荐

  1. 创建Windows Azure内部负载均衡器

    与普通的负载均衡器一样,Windows Azure内部负载均衡器也是四层的.内部负载均衡器会被分配一个内网地址,只能从虚拟网络内部访问,包括VPN和ExpressRoute. 内部负载均衡器通常被用于 ...

  2. C++浅析——虚函数的动态和静态绑定

    源自一道面试题,觉得很有意思 class CBase { public: virtual void PrintData(int nData = 111); }; void CBase::PrintDa ...

  3. NET Core中实现一个Token base的身份认证

    NET Core中实现一个Token base的身份认证 注:本文提到的代码示例下载地址> How to achieve a bearer token authentication and au ...

  4. Android工程师入门(一)——这周入大门,挤时间,轻喷

    挤挤时间,入个门先. 一.环境搭建 略. 二.项目结构 在studio中,项目=Module: res:放置应用到的所有资源——基本决定了生成的APK的大小: java:java源程序: manife ...

  5. Codeforces 549D. Hear Features[贪心 英语]

    D. Haar Features time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  6. Vijos1053 Easy sssp[spfa 负环]

    描述 输入数据给出一个有N(2 <= N <= 1,000)个节点,M(M <= 100,000)条边的带权有向图. 要求你写一个程序, 判断这个有向图中是否存在负权回路. 如果从一 ...

  7. 使用mxmlc在命令行编译.as代码

    在cmd命令行环境下,敲mxmlc出现 提示Error: could not find JRE和"Error: could not find Java 2 Runtime Envi 解决办法 ...

  8. Eclipse代码追踪功能说明

    在使用Java编写复杂一些的程序时,你会不会常常对一层层的继承关系和一次次方法的调用感到迷惘呢?幸亏我们有了Eclipse这么好的IDE可以帮我们理清头绪--这就要使用Eclipse强大的代码追踪功能 ...

  9. 转:研读代码必须掌握的Eclipse快捷键

    总结的很不错,而且有相应的用法,推荐!!! from: http://www.cnblogs.com/yanyansha/archive/2011/08/30/2159265.html 研读代码必须掌 ...

  10. poj1573 Robot Motion

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12507   Accepted: 6070 Des ...