applicationContext.xml和dispatcher-servlet.xml的区别
来源:互联网 发布:无效的json字符串 编辑:程序博客网 时间:2024/06/06 00:45
在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>
- Spring的applicationContext.xml和dispatcher-servlet.xml的区别
- applicationContext.xml和dispatcher-servlet.xml的区别
- applicationContext.xml和dispatcher-servlet.xml的区别
- applicationContext.xml和dispatcher-servlet.xml的区别
- spring applicationcontext.xml 和 dispatcher-servlet.xml
- SpringMVC中的配置文件applicationContext.xml和dispatcher-servlet.xml
- xxx-servlet.xml和applicationContext.xml的区别
- applicationContext-XXX.xml和XXX-servlet.xml的区别
- 关于Spring MVC的详细步骤(包括web.xml;applicationContext.xml;dispatcher-servlet.xml)
- applicationContext.xml和dispatch-servlet.xml的区别及引发的问题和教训
- applicationContext.xml与xxx-servlet.xml的区别?
- Spring中xxx-servlet.xml和applicationContext.xml的认识
- applicationContext.xml和SpringMVC的区别
- dispatcher-servlet.xml
- mvc-dispatcher-servlet.xml
- applicationContex.xml 和 *-servlet.xml 的区别
- beans.xml 和 applicationContext.xml配置区别
- Spring中的applicationContext.xml与SpringMVC的xxx-servlet.xml的区别
- 有了服务器,域名,如何将静态网页放到服务器上,让所有人都能访看到?
- css中常见的问题
- redis-zset类型简单操作
- js函数节流和去抖动
- Java中的精确计算
- applicationContext.xml和dispatcher-servlet.xml的区别
- CentOS6.8下安装redis并配置开机自启动
- React使用
- OHUI v45 for Android
- 面试总结
- ACM括号配对问题
- Excel在统计分析中的应用—第八章—假设检验-总体方差未知且为大样本下的检验
- javawebday25(编码方式 获取资源路径)
- 图形学扫描线填充算法