ContextLoaderListener 是否?
标准 Spring Web 应用程序(由 Roo 或“Spring MVC 项目”模板创建)使用 ContextLoaderListener 和 DispatcherServlet 创建一个 web.xml。 为什么他们不只使用 DispatcherServlet
并使其加载完整的配置?
我知道 ContextLoaderListener 应该用于加载与 Web 无关的内容,并且DispatcherServlet 用于加载与 Web 相关的内容(控制器,...)。这会产生两个上下文:父上下文和子上下文。
背景:
多年来我一直以这种标准方式来做这件事。
<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>
这通常会导致两个上下文以及它们之间的依赖关系出现问题。过去我总是能够找到解决方案,并且我有一种强烈的感觉,这使得软件结构/架构总是更好。但现在我面临 事件问题两种上下文的。
-- 然而,这让我重新思考这两个上下文模式,我问自己:为什么我要让自己陷入这个麻烦,为什么不使用一个 DispatcherServlet
加载所有 spring 配置文件并删除 ContextLoaderListener 完全。 (我仍然会拥有不同的配置文件,但只有一个上下文。)
有什么理由不删除 ContextLoaderListener 吗?
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.
-- 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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就您而言,不,没有理由保留
ContextLoaderListener
和applicationContext.xml
。如果您的应用程序仅适用于 servlet 上下文,那么坚持下去,它会更简单。是的,普遍鼓励的模式是将非 Web 内容保留在 Web 应用程序级别的上下文中,但这只不过是一个弱约定。
使用 webapp 级上下文的唯一令人信服的理由是:
DispatcherServlet
这些都不适合您,因此额外的复杂性是毫无根据的。
在将后台任务添加到 servlet 上下文时要小心,例如计划任务、JMS 连接等。如果您忘记将
添加到web.xml< /code>,那么这些任务直到servlet第一次访问时才会启动。
In your case, no, there's no reason to keep the
ContextLoaderListener
andapplicationContext.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:
DispatcherServlet
that need to share servicesDelegatingFilterProxy
,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 yourweb.xml
, then these tasks won't be started until the first access of the servlet.您也可以以相反的方式配置应用程序上下文。例如,为了使 OpenEntityManagerInViewFilter 工作。设置 ContextLoaderListener,然后配置您的 DispatcherServlet:
只需确保 contextConfigLocation 参数值为空。
You can configure the application context the other way around as well. E.g. in order to make the OpenEntityManagerInViewFilter work. Setup the ContextLoaderListener and then configure your DispatcherServlet with:
Just make sure that the contextConfigLocation parameter value is empty.
我想分享我在 Spring-MVC 应用程序上所做的事情:
在
we-mvc-config.xml
上,我仅添加了用 @Controller 注释的类:在
applicationContext.xml
文件上我添加了其余所有内容:I want to share what I've done on my Spring-MVC application:
On the
we-mvc-config.xml
I added just the classes annotated with @Controller:On the
applicationContext.xml
files I added all the rest: