搭建spring框架的时候,web.xml中的spring相关配置,可以不用配置ContextLoaderListener(即只配DispatcherServlet)吗?
2016-11-24 11:13
597 查看
搭建spring框架的时候,web.xml中的spring相关配置,可以不用配置org.springframework.web.context.ContextLoaderListener吗(即只配org.springframework.web.servlet.DispatcherServlet)?
可以,前提是,你不使用spring的控制反转(即Ioc),把spring mvc当Struts用,即只映射url和action的关系。
==============网上相同提问,及解答过程============
http://bbs.csdn.net/topics/391076893?page=1
是这样的,在网上大多数spring和springMVC的整合中,都需要在web.xml里面配置两样东西:1、对spring配置listener,如图:2、对springMVC配置servlet,如图:上面这样的配置确实可以运行,我已经测试过。但是,在我公司里面,也采用了spring和springMVC的整合,奇怪的是在web.xml中只看到了对了springMVC的servlet配置,却没有见到对spring的listener的配置,如下图,公司的web.xml配置:web.xml的行数太多,截不完,但是我仔细找过了,确实没有spring的listenner配置!难道spring和springMVC整合后,可以取消掉spring的listener配置吗?
精彩解答:
1
ContextLoaderListener 是在上下文加载的时候调用一些配置进行初始化,比如你配置的<context-param>之类的标签。 |
2
1. 如果只有 Spring mvc 的一个 Servlet,listener 可以不用。2. 但是如果用了Shiro 等,Shiro 用到的 Spring 的配置必须在 listener 里加载。3. 一般 Dao, Service 的 Spring 配置都会在 listener 里加载,因为可能会在多个 Servlet 里用到,因为父子 Context 的可见性问题,防止重复加载所以在 listener 里加载。所以,有时可用可不用,有时必用,具体看情况。 |
3
估计你们项目里没和Ioc容器一起使用。一般配置spring监听器都是为了配合Ioc容器一起使用的。spring源代码里可以看到,启动web服务器的时候会调用监听器的上下文初始化方法,然后去初始化Ioc容器。 |
相关文章推荐
- 搭建spring框架的时候,web.xml中的spring相关配置,可以不用配置ContextLoaderListener(即只配DispatcherServlet)吗?
- 在搭建Spring框架时,web.xml文件的配置之DispatcherServlet&ContextLoaderListener
- [Spring]web.xml中配置ContextLoaderListener监听器的作用
- spring通过在web.xml 中配置ContextLoaderListener 来加载co...
- 160329(一)、在web.xml文件里配置org.springframework.web.context.ContextLoaderListener
- 原博文补充: web.xml配置中ContextLoaderListener和DispatcherServlet的区别: - ContextLoaderListener是在我们的web容器启动的时候启
- web.xml配置详解 org.springframework.web.context.ContextLoaderListener
- [Spring]web.xml中配置ContextLoaderListener监听器的作用
- Spring配置web.xml中<listener>标签(ContextLoaderListener类解析)解析
- web.xml文件中配置spring的ContextLoaderListener后报错
- spring项目中,web.xml中的 ContextLoaderListener监听器的原理
- web.xml配置文件中ContextLoaderListener的作用
- web.xml文件加载顺序 一、 1 、启动一个 WEB 项目的时候, WEB 容器会去读取它的配置文件 web.xml ,读取 <listener> 和 <context-param>
- [key]严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener(Spring配置异常)
- Spring 配置问题还是代码问题?Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
- 【web.xml】报错java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- 05、Spring_web.xml_ContextLoaderListener
- SpringMVC源码 1 ContextLoaderListener和Servlet容器web.xml配置
- Spring源码学习之一 ContextLoaderListener及XmlWebApplicationContext
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-002- 在xml中引用Java配置文件,声明DispatcherServlet、ContextLoaderListener