gpt4 book ai didi

spring - ContextLoaderListener 和 RequestContextListener 是做什么的?

转载 作者:行者123 更新时间:2023-12-04 11:21:36 24 4
gpt4 key购买 nike

我有一个应用程序,我正在使用 Spring。
在我的 web.xml 中,我使用下面的行

<web-app>
....
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
....
</web-app>

这些是什么 ?
它们是强制性的吗?

最佳答案

org.springframework.web.context.ContextLoaderListener是 Spring 框架中的一个类。因为它实现了 ServletContextListener接口(interface),servlet 容器在 Web 应用程序启动(contextInitialized)和关闭(contextDestroyed)时通知它。
它专门负责引导(和有序关闭)Spring ApplicationContext。
引用:javadoc 说:

Bootstrap listener to start up and shut down Spring's root WebApplicationContext. Simply delegates to ContextLoader as well as to ContextCleanupListener.

org.springframework.web.context.request.RequestContextListener是来自同一框架的另一个类。它的 javadoc 说:

Servlet 2.4+ listener that exposes the request to the current thread, through both LocaleContextHolder and RequestContextHolder. To be registered as listener in web.xml.

Alternatively, Spring's RequestContextFilter and Spring's DispatcherServlet also expose the same request context to the current thread. In contrast to this listener, advanced options are available there (e.g. "threadContextInheritable").

This listener is mainly for use with third-party servlets, e.g. the JSF FacesServlet. Within Spring's own web support, DispatcherServlet's processing is perfectly sufficient.


所以它通常不在 Spring MVC 应用程序中使用,但允许在 JSF 应用程序中使用 Spring ApplicationContext 请求或 session 范围的 bean

关于spring - ContextLoaderListener 和 RequestContextListener 是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35652457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com