Understanding Spring Contexts in ES Web Apps

The Spring configuration in ES web apps is a little  complicated, and involves many XML files. This is an attempt to document how the config files work together.

In a typical ES web app, Spring is bootstrapped from the web.xml file. There are two Spring contexts initialized via web.xml:

The "root" application context.

There is a single "root" application context that applies to all servlets in the web app (a web app can contain multiple servlets, although the typical ES web app only has one servlet). The root context contains middle-tier services, data sources etc.

The root context is configured via the ContextLoaderListener (defined in the <listeners> section) and the contextConfigLocation parameter (defined in the <context-param> section):

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
    classpath*:applicationContext-common.xml,
    classpath*:applicationContext-onlinereg-email.xml,
    classpath*:applicationContext.xml,
    classpath*:applicationContext-onlinereg-security.xml,
    classpath*:applicationContext-web.xml,
    classpath*:applicationContext-container.xml
  </param-value>
</context-param>

The "servlet" application Context

Each servlet in the web app can have its own application context. These application contexts inherit all the beans defined in the root context. According to the Spring docs, the servlet context can override bean definitions from the root context. Typically, web-specific configuration (i.e. Spring MVC) is placed in the servlet context.

The servlet contexts are configured via a DispatcherServlet in the <servlet> section:

    <servlet>
        <servlet-name>onlinereg</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
        	<param-name>contextConfigLocation</param-name>
        	<param-value>classpath:servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

The default name for the servlet context's XML file is [SAISFRAMEWORK:servlet-name]-servlet.xml. In the ES web apps, where there is only one servlet, we tend to name this file servlet.xml.

  • No labels