Difference between DispatcherServlet and ContextLoaderListener in Spring


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

DispatcherServlet creates its own WebApplicationContext and the handlers/controllers/view-resolvers are managed by this context.

Sr. No.KeyDispatcherServletContextLoaderListener
1
Basic
The task of the DispatcherServlet  is to send request to the specific Spring MVC controller 
ContextLoaderListener  reads the Spring configuration file (with value given against contextConfigLocation in web.xml ), parses it and loads the singleton bean defined in that config file. So we initialize the web application with ContextLoaderListener  so that we read/parse/validate the config file in advance and whenever we can to inject dependency we can straightaway do it without any delay
2
Optional
It is optional. Spring application can’t live without the DispatcherServlet
It is mandatory. Spring application can live without the ContextLoaderListner
3
Container
 DispatcherServlet creates its own WebApplicationContext. The handlers/controllers/view-resolvers are managed by this context
Application Context is the container initialized by a ContextLoaderListener defined in the web.xml
4
Beans
 Web-specific beans such as controllers are included in DispatcherServlet’s Web Application Context
General beans such as services and DAOs make their way in root Web Application Context

Updated on: 09-Sep-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements