Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do ContextLoaderListener and RequestContextListener do?

I have an application, where i am using Spring. And in my web.xml i use lines below

<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>

What are they ? Are they mandatory ?

like image 319
mooksel Avatar asked Feb 26 '16 13:02

mooksel


People also ask

What is the use of ContextLoaderListener?

ContextLoaderListener creates a root web-application-context for the web-application and puts it in the ServletContext. This context can be used to load and unload the spring-managed beans ir-respective of what technology is being used in the controller layer(Struts or Spring MVC).

What is the use of RequestContextListener in spring?

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

What is DispatcherServlet and ContextLoaderListener?

Whereas DispatcherServlet is expected to load beans containing web components such as controllers, view resolvers, and handler mappings, ContextLoaderListener is expected to load the other beans in your application.

Where do I put RequestContextListener in spring boot?

Just add a @Bean method which constructs the RequestContextListener . Spring Boot will do the rest.


2 Answers

org.springframework.web.context.ContextLoaderListener is a class from Spring framework. As it implements the ServletContextListener interface, the servlet container notifies it at startup (contextInitialized) and at shutdown (contextDestroyed) of a web application.

It is specifically in charge of bootstrapping (and orderly shutdown) the Spring ApplicationContext.

Ref: javadoc says:

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 is another class from same framework. Its javadoc says:

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.

So it is normally not used in a Spring MVC application, but allows request or session scoped bean in a JSF application using a Spring ApplicationContext

like image 143
Serge Ballesta Avatar answered Sep 16 '22 15:09

Serge Ballesta


Listeners, in general, are a way for the container to notify your app of events, instead of just web requests.

For example, to be notified when a session is going to time out, you'd extend HttpSessionListener and implement the sessionDestroyed() method. The container would then call that on expiration of the session and you could log it alongside the login time for that user.

For ContextLoaderListener, this lets you kick off non-web related parts of your app, that you want on container startup, instead of waiting on someone to hit one of your spring components. It is using the context-param contextConfigLocation set earlier in your web.xml to know what to start.

For RequestContextListener, you get notified of request creation and deletion .

Whether they're necessary depends on the architecture of your app.

like image 34
Scott Edgar Avatar answered Sep 16 '22 15:09

Scott Edgar