
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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. | Key | DispatcherServlet | ContextLoaderListener |
---|---|---|---|
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 |
Advertisements