Spring中DispatcherServlet和ContextLoaderListener的区别



#### 在Spring中DispatcherServlet和ContextLoaderListener的区别是什么?
<hr>


1.[Stackoverflow的问题1](http://stackoverflow.com/questions/9016122/contextloaderlistener-or-not)
>A standard spring web application (created by Roo or "Spring MVC Project" Template) create a web.xml with `ContextLoaderListener` and `DispatcherServlet`. **Why do they not only use the `DispatcherServlet` and make it to load the complete configuration?**
>
>I understand that the ContextLoaderListener should be used to load the stuff that is not web relevant and the DispatcherServlet is used to load the web relevant stuff (Controllers,...). And this result in two contexts: a parent and a child context.
>
Background:
>
>I was doing it this standard way for several years.
>
> ```
><context-param>
>    <param-name>contextConfigLocation</param-name>
>    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
></context-param>
>
><!-- Creates the Spring Container shared by all Servlets and Filters -->
><listener>
>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
></listener>
>
><!-- Handles Spring requests -->
><servlet>
>    <servlet-name>roo</servlet-name>
>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
>    <init-param>
>        <param-name>contextConfigLocation</param-name>
>        <param-value>WEB-INF/spring/webmvc-config.xml</param-value>
>    </init-param>
>    <load-on-startup>1</load-on-startup>
></servlet>
>```
>
>This often caused problems with the two contexts and the dependencies between them. In the past I was >always able to find a solution, and I have the strong feeling that this makes the software >structure/architecture always better. But now I am facing a [problem with the events of the both >contexts]>(http://stackoverflow.com/questions/8534222/how-to-bridge-spring-application-context-events-to->an-other-context).


>-- However this makes my rethink this two context pattern, and I am asking myself: why should I bring >myself into this trouble, why not loading all spring configuration files with one `DispatcherServlet` and >removing the `ContextLoaderListener` completely. (I still will to have different configuration files, but >only one context.)
>
>Is there any reason not to remove the `ContextLoaderListener`?


翻译过来就是:
一个标准的Spring Web项目("Spring MVC Project"模板或者Roo创建的)会在web.xml中包含`ContextLoaderListener`和`DispathcerServlet`.**为什么有些可以只引用`DispatcherServlet`就可以加载所有的配置?**  
我知道 ContextLoaderListener 是应该用在加载与web无关的bean, DispatcherServlet用来加载web相关的bean(Controller相关的等等)。这样就会导致两个 Context:一个父级的和一个还自己的。  


背景:  
我一直按照标准的做法做了很多年。  
***代码部分***  
这经常引起两个context之间的依赖关系问题。在过去我总是可以找个对应的解决办法,并且我很强烈觉得这个机制可以使软件架构更好。但是我现在面临一个问题[problem with the events of the both >contexts](http://stackoverflow.com/questions/8534222/how-to-bridge-spring-application-context-events-to->an-other-context).  


然而这个问题也让我重新思考两个context的模式,我自己问自己:为什么我要把自己带到这个麻烦之中,为什么不把Spring所有的配置放在一个DispatcherServlet文件并且已出掉ContextLoaderListener。(我也会有不同的配置文件,但是只有一个context)。  
有什么理由可以支持移除`ContextLoaderListener`吗?


(以下是回答)
>In your case, no, there's no reason to keep the ContextLoaderListener and applicationContext.xml. If your app works fine with just the servlet's context, that stick with that, it's simpler.
>
>Yes, the generally-encouraged pattern is to keep non-web stuff in the webapp-level context, but it's nothing more than a weak convention.
>
>The only compelling reasons to use the webapp-level context are:
>
>If you have multiple DispatcherServlet that need to share services
>If you have legacy/non-Spring servlets that need access to Spring-wired services
>If you have servlet filters that hook into the webbapp-level context (e.g. Spring Security's DelegatingFilterProxy, OpenEntityManagerInViewFilter, etc)
>None of these apply to you, so the extra complexity is unwarranted.
>
>Just be careful when adding background tasks to the servlet's context, like scheduled tasks, JMS connections, etc. If you forget to add <load-on-startup> to your web.xml, then these tasks won't be started until the first access of the servlet.


翻译过来就是:  
在你这种情况下,没有理由在去使用ContextLoaderListener和applicationContext.xml配置文件。如果你的app只使用DispatcherServlet就能很好的工作,那么就应该坚持只用这个,这个简单。  
是的,一般推荐的方式是把web不相关的东西放到 webapp级别的 context,但是这除了让有点不方便什么都没有。  
需要强烈使用webapp的理由如下:  
* 如果你有多个需要共享服务的DispatcherServlet
* 如果你有逻辑关系需要访问Spring内部的service
* 如果你有需要hook 在webapp级别的context的Servlet filters.


如果上述情形都不适合你,那么你完全不必增加复杂度。  
有一点需要关注的是,当增加后台任务到servlet的context是,比如调度任务、JMS连接等等,如果你忘了添加<load-on-startup>,这些任务将不会第一个起来。


[问题2:Difference between applicationContext.xml and spring-servlet.xml in Spring Framework](http://stackoverflow.com/questions/3652090/difference-between-applicationcontext-xml-and-spring-servlet-xml-in-spring-frame)
>Are applicationContext.xml and spring-servlet.xml related anyhow in Spring Framework?
Will the properties files declared in applicationContext.xml be available to DispatcherServlet?
On a related note, why do I need a *-servlet.xml at all? Why is applicationContext.xml alone insufficient?


(Below is the answer:)


>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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值