Java ContextLoaderListener 和 RequestContextListener 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18725559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between ContextLoaderListener and RequestContextListener?
提问by Juns
I have googled it but not found satisfactory answer, it would be great if you guys can explain the difference between ContextLoaderListener
and RequestContextListener
.
我用Google搜索,但没有找到满意的答案,这将是巨大的,如果你们能解释的区别ContextLoaderListener
和RequestContextListener
。
回答by Rahul Tripathi
contextloaderlistener:-Bootstrap listener to start up Spring's root WebApplicationContext. Simply delegates to ContextLoader.
contextloaderlistener:-Bootstrap 监听器来启动 Spring 的根 WebApplicationContext。简单地委托给 ContextLoader。
requestcontextlistener :-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.
requestcontextlistener :-此侦听器主要用于第三方 servlet,例如 JSF FacesServlet。在 Spring 自己的 web 支持中,DispatcherServlet 的处理是完全足够的。
回答by Mikhail
I have read that If you use the ContextLoaderListener you don't need the RequestContextListener or Filter. It registers the current request(attributes) in a thread local so that the scoped proxies can use it.
我读过如果您使用 ContextLoaderListener,则不需要 RequestContextListener 或 Filter。它在本地线程中注册当前请求(属性),以便作用域代理可以使用它。
回答by AlphaBetaGamma
ContextLoaderListener is a bootstrap listener to startup Spring's root WebApplicationContext.
ContextLoaderListener 是启动 Spring 的根 WebApplicationContext 的引导侦听器。
RequestContextListener is used when you want the attributes in your request thread to stay alive.
当您希望请求线程中的属性保持活动状态时,将使用 RequestContextListener。
The point to be noted here is that, the inheritable flag is set to false in the RequestContextListener.
这里需要注意的一点是,RequestContextListener中的inheritable标志设置为false。
So If you want that a child thread inherits the request attributes then you should try using RequestContextFilter or RequestContextHolder.
因此,如果您希望子线程继承请求属性,那么您应该尝试使用 RequestContextFilter 或 RequestContextHolder。
回答by SRK
If you use a Servlet 2.5 web container, with requests processed outside of Spring's DispatcherServlet (for example, when using JSF or Struts), you need to register the org.springframework.web.context.request.RequestContextListenerServletRequestListener. For Servlet 3.0+, this can be done programmatically via the WebApplicationInitializer interface. Alternatively, or for older containers, add the following declaration to your web application's web.xml file:
如果您使用 Servlet 2.5 Web 容器,并且请求在 Spring 的 DispatcherServlet 之外处理(例如,使用 JSF 或 Struts 时),则需要注册org.springframework.web.context.request.RequestContextListenerServletRequestListener。对于 Servlet 3.0+,这可以通过 WebApplicationInitializer 接口以编程方式完成。或者,或者对于较旧的容器,请将以下声明添加到您的 Web 应用程序的 web.xml 文件中:
<listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> </web-app>
<listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> </web-app>