9

What is the difference between DispatcherServlet and ContextLoaderListener in Spring framework? Do we need to configure both of them in web.xml when we are using spring framework?

Ashish Kumar
  • 916
  • 2
  • 15
  • 32
user222
  • 587
  • 3
  • 10
  • 31

3 Answers3

20

AFAIK each DispatcherServlet will have a WebApplicationContext. By default the DispatcherServlet looks for a spring configuration file named [appname]-servlet.xml under WEB-INF folder.

Do we need to configure DispatcherServlet?

Yes, every spring application should configure DispatcherServlet as it is the one through which all the requests are routed. It decides the appropriate method of the controller class to handle the request. Once controller returns the model along with the logical view, DispatcherServlet takes the help of ViewResolver to resolve the view (generally JSPs) and will pass the model data to the view, which is finally rendered on the browser.

Do we need to configure ContextLoaderListener?

No, this is not mandatory. Spring applications can live with out ContextLoaderListener.

Why do we need ContextLoaderListener?

Usually when we build multi-tier applications we don't want to clutter all the beans in one config file [appname]-servlet.xml. For example if you configure spring security you wanted to include all those beans in security-context.xml, in the same way all the beans belonging to service layer are configured in applicationContext.xml and some would like to configure beans belonging to DAO layer in dao-context.xml. So when you configure all these beans in different context files, you need to let know spring that these files exist as spring only knows about [appname]-servlet.xml. ContextLoaderListener will help spring recognize all the other context files.

Hope this helps!

Aditya
  • 913
  • 2
  • 7
  • 18
  • 1
    This should be the accepted answer. You made it really clear. Thanks! – LppEdd Apr 17 '18 at 13:47
  • 1
    I could not apprehend the accepted answer that well. The simplicity of this answer makes it is crystal clear as to what is the motive behind having ContextLoaderListener and how DispatcherServlet has its role to play. Great explanation. Thanks. – Aniket Jun 24 '21 at 20:53
4

The root WebApplicationContext is a Spring Application Context shared across the application.

A DispatcherServlet instance actually has its own WebApplicationContext.

One can have multiple DispatcherServlet instances in an application, and each will have its own WebApplicationContext.

The root WebApplicationContext is shared across the application, so if you have a root WebApplicationContext and multiple DispatcherServlets, the DispatcherServlets will share the root WebApplicationContext.

However, for a simple Spring MVC application, one can even have a situation where there is no need to have a root WebApplicationContext. A DispatcherServlet would still have its own WebApplicationContext, but it doesn’t actually need to have a parent root WebApplicationContext.

So, which beans should go in the root Web Application Context and which beans should go in the DispatcherServlet’s Web Application Context? Well, general beans such as services and DAOs make their way in root Web Application Context, and more web-specific beans such as controllers are included in DispatcherServlet’s Web Application Context.

Mahesh
  • 407
  • 5
  • 4
0

When DispatcherServlet starts up, it creates a Spring application context and starts loading it with beans declared in the configuration files or classes that it’s given. But in Spring web applications, there’s often another application context. This other application context is created by 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. These beans are typically the middle-tier and data-tier components that drive the back end of the application.

Good luck!