DispatcherServlet and ContextLoaderListener in Spring MVC Explained

No comments

There are times when you might have been thinking what is this ContextLoaderListener and why do we use it or should we use it or not while configuring your spring web MVC application like we explained in our previous article . In this article we will try to answer all these questions.

In an spring MVC application there are usually two bean containers.

1. One is Application Context which is created by ContextLoaderListener
2. Another is Web Application Context which is created by the DispatcherServlet

Both of these bean containers gets their bean configurations from the xml files defined in web.xml file, or if we are using java based configuration with Servlet 3.0 or above we define the configuration in java configuration file inside a WebInitializer class like we explained in our previous article

n this article we will see what is the difference between these two bean containers, what is the relation between these bean containers and which one should be used when ?

1. Application Context Created by ContextLoaderListener

Configuration and use of ContextLoaderListener

<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:app-context.xml</param-value>
</context-param>


As we can see we define the configuration file for loading beans for Application Context created by ContextLoaderListener as the context param in web.xml, which is app-context.xml in above example in the classpath.

There is only one application context per application and we should define the beans in this container which are common and we want them to be accessible throughout our application. This is considered a good practice to define data tier beans and service tier beans in Application Context.

There are some applications where we might need more than one Dispatcher Servlet then this Application Context can be used to get the common beans for the Dispatcher Servlets because it serves the whole application.

2. Application Context DispatcherServlet

Configuration and use of DispatcherServlet

<servlet>
 <servlet-name>user-servlet</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:user-servlet.xml</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
</servlet>


In an application each dispatcher servlet creates its own bean container which is Web Application Context.

We define the path of configuration file for Web Application Context to load the beans as init param of the servlet as we have defined above.

Note : Important thing here is this that the name of the configuration file must be of form - *-servlet.xml

Web Application Context is a child context of Application Context created by ContextLoaderListener

Because Web Application Context is a child context of the Application Context created by ContextLoaderListener therefore beans in the application context can be referred by Web Application Context, this is the reason it is recommended to put common beans like data-tier beans and service tier beans in Application so that any Web Application Context can use them

Loading Configuration for DispatcherSrvlet and ContextLoaderListener without web.xml and with Servlet 3.0 or above

If you are using Servlet API 3.0 or above you can choose to skip the use of web.xml file. We create a Initializer class which extends AbstractAnnotationConfigDispatcherServletInitializer and then implementing its method we can define the configuration java files to load beans.


public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

 @Override
 protected Class[] getRootConfigClasses() {
  return new Class []{RootConfig.class};
 }

 @Override
 protected Class[] getServletConfigClasses() {
  return new Class []{WebConfig.class};
 }

 @Override
 protected String[] getServletMappings() {
  return new String[] {"/"};
 }

}

In above class method getRootConfigClasses() returns an array of java configuration classes which will be used by ContextLoaderListener to load the beans which is RootConfig.class in the example above.
getServletConfigClasses() returns an array of java configuration classes which will be used by DispatcherServlet to load the beans which is WebConfig.class in the example above.

For getting more detailed information about how to setup an Spring MVC application without the use of web.xml , click on this article

Also in case of any doubt or query feel free to leave a comment below, Thanks !


No comments :

Post a Comment