Open In App

ContextLoaderListener vs DispatcherServlet

Last Updated : 03 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

ContextLoaderListener and DispatcherServlet are two declarations that you should have seen in the web.xml file while configuring Spring MVC using XML. Let’s attempt to comprehend their distinctions and their usefulness inside the framework.ContextLoaderListener adds the web application’s root context to the ServletContext after creating it. Depending on the controller layer technology (Sprouts or Spring MVC), this context can be used to load and unload the beans that are handled by Spring.

ContextLoaderListener

A spring-web module listener of type org.springframework.web.context.ContextLoaderListener is responsible for managing the root web application context that was outlined in the preceding section Context.xml is where the listener loads an XML application context by default. But you may modify those defaults. For example, we may substitute XML with Java annotations. It is possible to set up this listener programmatically in Servlet 3.x environments or in the web app descriptor (web.xml file).

Using web.xml and an XML Application Context

We set up the listener as normal when using web.xml.

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

Using web.xml and a Java Application Context

To specify the kind of context to create for the listener, we apply the contextClass parameter:

<context-param>
    <param-name>contextClass</param-name>
    <param-value>
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
</context-param>

DispatcherServlet

Dispatcher Servlets are setup in Spring MVC applications in at least one instance. This servlet is responsible for receiving incoming requests, forwarding them to the relevant controller function, and providing the view back. There is an associated application context for every DispatcherServlet. Beans defined in these kinds of contexts define MVC objects like as view resolvers and controllers, as well as configure the servlet.

Using web.xml and an XML Application Context

We have set up the servlet as normal when using web.xml.

XML




<servlet>
    <servlet-name>normal-webapp</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>normal-webapp</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>


Using web.xml and a Java Application Context

In order to use an alternative context type, we provide a contextClass argument in addition to an appropriate contextConfigLocation:

XML




<servlet>
    <servlet-name>normal-webapp-annotations</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.baeldung.contexts.config.NormalWebAppConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>


Difference between ContextLoaderListener and DispatcherServlet

ContextLoaderListener

DispatcherServlet

ContextLoaderListener loads the singleton bean specified in the Spring configuration file after reading it and parsing it (using the value provided against contextConfigLocation in web.xml).

The DispatcherServlet’s job is to communicate requests to the designated Spring MVC controller.

ContextLoaderListener specified in the web.xml initialises the container called Application Context.

DispatcherServlet is responsible for creating its own WebApplicationContext. This context controls the handlers, controllers, and view-resolvers.

The ContextLoaderListener is not essential to a Spring application.

The DispatcherServlet is essential to a Spring application.

Application root context is created by ContextLoaderListener.

DispatcherServlet entries generate a single child application context for each item in the servlet tree.

Conclusion

All MVC beans i.e. views, controllers, etc. are defined in the DispatcherServlet context, and all cross-cutting beans i.e. security, transactions, services, etc. are defined by ContextLoaderListener in the root context. Since we never need to access any MVC beans i.e. from child context into security-related classes i.e. from root context, this solution usually functions well. As mentioned previously, security beans are used in MVC classes, and they have access to it.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads