Spring MVC

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.

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>

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago