JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

Spring MVC – Application Context vs Web Application Context

April 7, 2015 by Krishna Srinivasan Leave a Comment

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 + Hibernate Integration
  • Spring Aspect Oriented Programming
  • Spring MVC – component-scan Vs annotation-config Vs annotation-driven

Spring application has two types of context configuration files for Spring MVC module:

  1. ApplicationContext (default name for this file is applicationContext.xml)
  2. 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:

[code lang=”java”]
<?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>
[/code]

Filed Under: Spring Framework Tagged With: Spring MVC

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved