Open In App

Difference Between ApplicationContext and WebApplicationContext in Spring MVC

Last Updated : 01 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Spring MVC framework enables separation of modules namely Model, View, and Controller, and seamlessly handles the application integration. This enables the developer to create complex applications also using plain Java Classes. The model object can be passed between view and controller using maps. 

What is ApplicationContext?

Spring IoC container is responsible for instantiating, wiring, configuring, and managing the entire life cycle of objects. BeanFactory and ApplicationContext represent the Spring IoC Containers. ApplicationContext is the sub-interface of BeanFactory. BeanFactory provides basic functionalities and is recommended to use for lightweight applications like mobile and applets. ApplicationContext provides basic features in addition to enterprise-specific functionalities which are as follows:

  • Publishing events to registered listeners by resolving property files.
  • Methods for accessing application components.
  • Supports Internationalization.
  • Loading File resources in a generic fashion.

Must Read – Spring – ApplicationContext

What is WebApplicationContext?

WebApplicationContext in Spring is a web-aware ApplicationContext i.e it has Servlet Context information. In a single web application, there can be multiple WebApplicationContext. That means each DispatcherServlet is associated with a 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.

Must Read – WebApplicationContext in Spring MVC

Difference Between ApplicationContext and WebApplicationContext in Spring MVC

Factor 1: ApplicationContext is used to create standalone applications. But for the web applications, we have to deal with a container called WebApplicationContext.

A. Standalone App

// Creating container objects manually
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
// Destroying container object manually
context.close();

B. Web App

You don’t need to create and destroy the container object. The container object will be created automatically with the server startup and destroyed when we stop the server. 

Factor 2: One more interesting difference is both ApplicationContext and WebApplicationContext are the spring containers where WebApplicationContext is the child of the ApplicationContext interface.

public interface WebApplicationContext 
extends ApplicationContext 
{
  ...............
}

After having a brief idea about the purpose and standalone app and web app in order, to sum up, the differences between ApplicationContext and WebApplicationContext, it is depicted in tabular manner as shown below:

ApplicationContext 

WebApplicationContext 

ApplicationContext is used to create standalone applications. WebApplicationContext is used to create web applications.
ApplicationContext is the parent of the WebApplicationContext interface. WebApplicationContext is the child of the ApplicationContext interface.
In the case of ApplicationContext, we have to create and destroy the container objects manually. But in the case of WebApplicationContext, we don’t need to create and destroy the container object. The container object will be created automatically.
There is always a single ApplicationContext in an application. There can be multiple WebApplicationContexts for each of the dispatcher servlets.
ApplicationContext represents the Spring IoC Containers and it is the sub-interface of BeanFactory.  WebApplicationContext in Spring is a web-aware ApplicationContext i.e it has Servlet Context information.
ApplicationContext is used to inject all the middle-tier beans (Services, DAOs) which are instantiated using the “ContextLoaderListener” class configured in web.xml. WebApplicationContext is used to deal with the web-related components such as controllers and view resolvers, which is configured using “DispatcherServlet”.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads