Understanding the difference between ApplicationContext and WebApplicationContext for Spring developers is important. There are various context names available in the spring framework that are used based on the type of application. The most common ambiguity due to the two different context configuration files used for configuring the spring applications. The question comes to developers mind is that why would we need two different context configurations files and when these files are loaded by the spring container?. This tutorial summarizes some of the important facts about both the context configurations files.
also read:
Spring application has two types of context configuration files for Spring MVC module:
- ApplicationContext (default name for this file is applicationContext.xml)
- WebApplicationContext (default name for this file is xxx-servlet.xml where xxx is the DispatcherServlet name in web.xml)
ApplicationContext
- applicationContext.xml is the root context configuration for every web application.
- Spring loads applicationContext.xml file and creates the ApplicationContext for the whole application.
- There will be only one application context per web application.
- If you are not explicitly declaring the context configuration file name in web.xml using the contextConfigLocation param, Spring will search for the applicationContext.xml under WEB-INF folder and throw FileNotFoundException if it could not find this file.
WebApplicationContext
- Apart from ApplicationContext, there can be multiple WebApplicationContext in a single web application.
- In simple words, each DispatcherServlet associated with single WebApplicationContext.
- xxx-servlet.xml file is specific to the DispatcherServlet and a web application can have more than one DispatcherServlet configured to handle the requests.
- In such scenarios, each DispatcherServlet would have a separate xxx-servlet.xml configured. But, applicationContext.xml will be common for all the servlet configuration files.
- Spring will by default load file named “xxx-servlet.xml” from your webapps WEB-INF folder where xxx is the servlet name in web.xml.
- If you want to change the name of that file name or change the location, add initi-param with contextConfigLocation as param name.
ContextLoaderListener
- Performs the actual initialization work for the root application context.
- Reads a “contextConfigLocation” context-param and passes its value to the context instance, parsing it into potentially multiple file paths which can be separated by any number of commas and spaces, e.g. “WEB-INF/applicationContext1.xml, WEB-INF/applicationContext2.xml”.
- ContextLoaderListener is optional. Just to make a point here: you can boot up a Spring application without ever configuring ContextLoaderListener, just a basic minimum web.xml with DispatcherServlet.
Sample web.xml file with configurations:
<?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"> <display-name>Spring MVC</display-name> <!-- 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>webmvc1</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/mvc1-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>webmvc2</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/mvc2-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>webmvc1</servlet-name> <url-pattern>/webmvc1</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>webmvc2</servlet-name> <url-pattern>/webmvc2</url-pattern> </servlet-mapping> </web-app>