SpringMVC中的配置文件applicationContext.xml和dispatcher-servlet.xml
来源:互联网 发布:em算法,知乎 编辑:程序博客网 时间:2024/06/07 07:56
原文链接:https://www.cnblogs.com/parryyang/p/5783399.html
在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>
- SpringMVC中的配置文件applicationContext.xml和dispatcher-servlet.xml
- spring applicationcontext.xml 和 dispatcher-servlet.xml
- springmvc中的spring-servlet.xml 和 applicationContext.xml
- applicationContext.xml和dispatcher-servlet.xml的区别
- applicationContext.xml和dispatcher-servlet.xml的区别
- Spring的applicationContext.xml和dispatcher-servlet.xml的区别
- applicationContext.xml和dispatcher-servlet.xml的区别
- Springmvc之dispatcher-servlet.xml
- SpringMVC 4.2.2 - Web.xml,Dispatcher-Servlet及ApplicationContext配置笔记
- 【SpringMVC之配置文件(一)】spring 里面的ApplicationContext.xml 和 xxx-servlet.xml分别的作用是什么?
- 【SpringMVC之配置文件(一)】spring 里面的ApplicationContext.xml 和 xxx-servlet.xml分别的作用是什么?
- 【SpringMVC之配置文件(一)】spring 里面的ApplicationContext.xml 和 xxx-servlet.xml分别的作用是什么?
- springmvc配置文件:springmvc-servlet.xml
- spring中的applicationContext.xml和xxx-servlet.xml
- SpringMVC之ApplicationContext.xml配置文件解析
- SpringMVC(1):基础配置文件web.xml 和 <servlet-name>-servlet.xml / springmvc.xml
- 关于Spring MVC的详细步骤(包括web.xml;applicationContext.xml;dispatcher-servlet.xml)
- Spring中的applicationContext.xml与SpringMVC的xxx-servlet.xml的区别
- 关于RISC-V成为印度国家指令集的一些看法
- 如何设置,使IntelliJ IDEA智能提示忽略大小写
- C++连接CTP接口实现简单量化交易
- 同步以太网与时钟协议
- 再谈扁平化
- SpringMVC中的配置文件applicationContext.xml和dispatcher-servlet.xml
- 原创视频|手把手教你用Axure做一个微信高保真原型(一)
- 第一阶段-入门详细图文讲解tensorflow1.4 -(三)TensorFlow 编程基础知识
- html5 模板
- 你是一个有价值的产品经理吗?
- C初识数组
- 牛顿法、指数分布
- 文章标题
- Android Volley完全解析(一),初识Volley的基本用法