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 ?
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).
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 .
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.
Just add a @Bean method which constructs the RequestContextListener . Spring Boot will do the rest.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With