ContextLoaderListener 与否? [英] ContextLoaderListener or not?

查看:24
本文介绍了ContextLoaderListener 与否?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个标准的 Spring Web 应用程序(由 Roo 或Spring MVC 项目"模板创建)使用 ContextLoaderListenerDispatcherServlet 创建一个 web.xml.为什么他们不仅使用 DispatcherServlet 并且让它加载完整的配置?

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?

我知道 ContextLoaderListener 应该用于加载与网络无关的内容,而 DispatcherServlet 用于加载与网络相关的内容(控制器,...).这导致了两个上下文:父上下文和子上下文.

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.

背景:

我用这种标准方式做了好几年.

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.

-- 然而,这让我重新思考这两种上下文模式,我问自己:为什么我要给自己带来这个麻烦,为什么不用一个 DispatcherServlet 加载所有 spring 配置文件并删除ContextLoaderListener 完全.(我仍然会有不同的配置文件,但只有一个上下文.)

-- 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.)

是否有任何理由不删除 ContextLoaderListener?

Is there any reason not to remove the ContextLoaderListener?

推荐答案

在您的情况下,不,没有理由保留 ContextLoaderListenerapplicationContext.xml.如果您的应用程序仅使用 servlet 的上下文就可以正常工作,那么坚持下去,就更简单了.

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.

是的,普遍鼓励的模式是将非网络内容保留在 web 应用程序级别的上下文中,但这只不过是一种弱约定.

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.

使用 webapp 级上下文的唯一令人信服的理由是:

The only compelling reasons to use the webapp-level context are:

  • 如果你有多个DispatcherServlet需要共享服务
  • 如果您有需要访问 Spring-wired 服务的遗留/非 Spring servlet
  • 如果您有挂钩到 web 应用程序级上下文的 servlet 过滤器(例如 Spring Security 的 DelegatingFilterProxyOpenEntityManagerInViewFilter 等)
  • 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 webapp-level context (e.g. Spring Security's DelegatingFilterProxy, OpenEntityManagerInViewFilter, etc)

这些都不适用于您,因此没有必要增加额外的复杂性.

None of these apply to you, so the extra complexity is unwarranted.

在将后台任务添加到 servlet 的上下文时要小心,例如计划任务、JMS 连接等.如果您忘记将 添加到您的 web.xml,那么这些任务直到servlet第一次访问时才会开始.

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 与否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆