Difference between ApplicationContext and WebApplicationContext in Spring MVC

This tutorial explains the difference between Application Context vs Web Application Context in spring mvc. The ApplicationContext and WebApplicationContext both are almost same thing but there are some basic differences related to the web aware environment. In Spring ApplicationContext instances can be scoped. In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext (i.e own *-servlet.xml), which inherits all the beans already defined in the root WebApplicationContext. Yoc can also override the inherited bean scope in the servlet-specific scope and also can define new scope-specific beans local to a given servlet instance.

So we can say that both ApplicationContext and WebApplicationContext are the spring containers where WebApplicationContext is child of the ApplicationContext interface.

public interface WebApplicationContext extends ApplicationContext {
    ServletContext getServletContext();
}

WebApplicationContext has javax.servlet.ServletContext that means it’s able to communicate with the container.

ApplicationContext (i.e. Root Application Context)
In spring mvc for every web application applicationContext.xml file used as the root context configuration. Spring loads this file and creates the ApplicationContext for whole application. File applicationContext.xml is loaded by ContextLoaderLoaderLinstner which is configured into web.xml file as the context configuration. The default location and name of the Root Application Context are under WEB-INF folder and applicationContext.xml respectively and throw FileNotFoundException if it could not find this file in this location. Otherwise we have to declare explicitly the context configuration file name in web.xml using the contextConfigLocation param. There will be only one application context per web application.

WebApplicationContext
WebApplicationContext in Spring is web aware ApplicationContext i.e it has Servlet Context information. In single web application there can be multiple WebApplicationContext. That means each DispatcherServlet associated with single WebApplicationContext. The WebApplicationContext configuration file *-servlet.xml is specific to the DispatcherServlet and a web application can have more than one DispatcherServlet configured to handle the requests and each DispatcherServlet would have a separate *-servlet.xml file to configure. But, applicationContext.xml will be common for all the servlet configuration files. By default DispatcherServlet loads file name servletName-servlet.xml from your webapps WEB-INF folder. If you want to change the name of that file name or change the location, add init-param with contextConfigLocation as param name.

ApplicationContext and WebApplicationContext

ContextLoaderListener
This listener is responsible to load the context configuration files. It performs the actual initialization work for the root application context. It reads a “contextConfigLocation” context-param and passes its value to the context instance. We can pass multiple files in the context configuration by commas or space separation. e.g. “WEB-INF/applicationContext.xml, WEB-INF/applicationContext-security.xml”.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0">
<!-- This is the root application context for whole web application. -->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/rootApplicationContext.xml</param-value>
</context-param
<listener>
  <listener-class>
    org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>
<servlet>
  <servlet-name>webapp1</servlet-name>
    <servlet-class>
       org.springframework.web.servlet.DispatcherServlet
    </servlet-class>

 <!-- We require this configuration when we want to change the default 
 name / location of the servlet specific configuration files -->
 <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/app1-servlet.xml</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
</servlet>

<servlet>
<servlet-name>webapp2</servlet-name>
<servlet-class>
    org.springframework.web.servlet.DispatcherServlet
</servlet-class>

<!-- We require this configuration when we want to change the default 
name / location of the servlet specific configuration files -->
<init-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/app2-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

   <servlet-mapping>
     <servlet-name>webapp1</servlet-name>
     <url-pattern>/webapp1</url-pattern>
   </servlet-mapping>
   <servlet-mapping>
     <servlet-name>webapp2</servlet-name>
     <url-pattern>/webapp2</url-pattern>
   </servlet-mapping>
</web-app>