ContextLoadListener 和 DispatcherServlet [英] ContextLoadListener and DispatcherServlet

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

问题描述

在 web.xml 中定义 ContextLoadListener 和 DispatcherServlet 是否可以,或者它们是互斥的?

Is it ok to define both the ContextLoadListener and DispatcherServlet in the web.xml or are they mutually exclusive?

推荐答案

不,它们并不相互排斥.

No, they're not mutually exclusive.

ContextLoaderListener 管理与整个 webapp 相关联的 appcontext.DispatcherServlet 管理与该特定 servlet 关联的上下文.webapp 上下文是 servlet appcontext 的父",并且 webapp appcontext 中的所有 bean 对 servlet appcontext 中的 bean 都是可见的.

ContextLoaderListener manages an appcontext which is associated with the whole webapp. DispatcherServlet manages a context associated with that specific servlet. The webapp context is the "parent" of the servlet appcontext, and all beans in the webapp appcontext are visible to beans in the servlet appcontext.

如果您只有一个 servlet,那么使用 ContextLoaderListener 的理由并不多.如果您有多个 servlet,最好在可行的情况下将共享 bean 放在 webbapp 上下文中,以便它们可以重复使用.

If you have only one servlet, there's not many reasons to use ContextLoaderListener. If you have multiple servlets, it's good to put the shared beans in the webbapp context where practical, so they can be reused.

这篇关于ContextLoadListener 和 DispatcherServlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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