ContextLoadListener 和 DispatcherServlet [英] ContextLoadListener and 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屋!