ApplicationContextcontains the FlightServiceimplementation, and is named
applicationContext.xml. This ApplicationContextmust be initialized before the web-specific
resources are started because it provides services required by the web application.
To create the root ApplicationContext, there are two choices. The first is to use
the ContextLoaderListener, responding to the contextInitialized()callback of a
ServletContextListener. This class is the recommended method for root
ApplicationContextcreation, but is only viable for Servlet 2.3 or higher containers.
■CautionSome Servlet 2.3 containers do not initialize listeners before servlets, which is incorrect behavior.
If this is the case for your container, you must use the ContextLoaderServlet.
If you are using a Servlet 2.2 container, or if your Servlet 2.3 container does not follow
the correct order for loading components, you will need to use the ContextLoaderServlet.
However, for the rest of the example, we will use and illustrate the ContextLoaderListener.
Configure the ContextLoaderListenerby defining a
file (refer to Listing 4-12), with a
ContextLoaderListener. Upon initialization, it will attempt to use the default location of
/WEB-INF/applicationContext.xmlto find the configuration file for the root ApplicationContext
(of course, the file location is configurable). Once the ApplicationContextis built it will be
placed into the application scope of the web application so that the entire application may
access it. However, because of Dependency Injection, it is rare to ever need to access an
ApplicationContextdirectly.
Listing 4-12.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>JumpIntoSpringMVC</display-name>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
CHAPTER 4 ■JUMP INTO SPRING MVC 59