【发布时间】:2010-07-13 05:58:39
【问题描述】:
是否可以在 web.xml 中同时定义 ContextLoadListener 和 DispatcherServlet 或者它们是互斥的?
【问题讨论】:
是否可以在 web.xml 中同时定义 ContextLoadListener 和 DispatcherServlet 或者它们是互斥的?
【问题讨论】:
不,它们不是相互排斥的。
ContextLoaderListener 管理与整个 webapp 关联的 appcontext。 DispatcherServlet 管理与该特定 servlet 关联的上下文。 webapp 上下文是 servlet appcontext 的“父级”,webapp appcontext 中的所有 bean 对 servlet appcontext 中的 bean 都是可见的。
如果您只有一个 servlet,那么使用ContextLoaderListener 的理由并不多。如果您有多个 servlet,最好将共享 bean 放在 webbapp 上下文中,以便可以重复使用。
【讨论】: