LibraryLink ToToggle FramesPrintFeedback

Using the Spring Context Listener

An alternative approach to instantiating endpoints inside a servlet container is to use the Spring context listener. The Spring context listener provides more flexibility in terms of how an application is wired together. It uses the application's Spring configuration to determine what object to instantiate and loads the objects into the application context used by the servlet container.

The added flexibility adds complexity. The application developer must know exactly what application components need to loaded. They also must know what Fuse Services Framework components need to be loaded. If any component is missing, the application will not not load properly and the desired endpoints will not be created.

The following steps are involved in building and packaging a Web application that uses the Spring context listener:

The servlet container looks in the WEB-INF/web.xml file to determine what classes are needed to activate the Web application. When deploying a Fuse Services Framework based application using the Spring context listener, the servlet container needs to load the org.springframework.web.context.ContextLoaderListener class. This is specified using the listener element and its listener-class child.

The org.springframework.web.context.ContextLoaderListener class uses a context parameter called contextConfigLocation to determine the location of the Spring configuration file. The context parameter is configured using the context-parameter element. The context-param element has two children that specify parameters and their values. The param-name element specifies the parameter's name. The param-value element specifies the parameter's value.

Example 6.5 shows a web.xml file that configures the servlet container to load the Spring listener and a Spring configuration file.

Example 6.5. Web Application Configuration for Loading the Spring Context Listener

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <context-param> 1
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/beans.xml</param-value>
  </context-param>

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

The XML in Example 6.5 does the following:

1

Specifies that the Spring context listener will load the application's Spring configuration from WEB-INF/beans.xml.

2

Specifies that the servlet container should load the Spring context listener.

The Spring configuration file for a application using the Spring context listener is similar to a standard Fuse Services Framework configuration file. It uses all of the same endpoint configuration elements described in Configuring JAX-WS Endpoints. It can also contain standard Spring beans.

The difference between a typical Fuse Services Framework configuration file and a configuration file for using the Spring context listener is that the Spring context listener configuration must import the configuration for all of the Fuse Services Framework runtime components used by the endpoint's exposed by the application. These components are imported into the configuration as resources using an import element for each component's configuration.

Example 6.6 shows the configuration for a simple consumer endpoint being deployed using the Spring context listener.


The import elements at the beginning of Example 6.6 import the required Fuse Services Framework component configuration. The required Fuse Services Framework component configuration files depends on the features being used by the endpoints. At a minimum, an application in a servlet container will need the components shown in Example 6.6.

[Tip]Tip

Importing the cxf-all.xml configuration file will automatically import all of the Fuse Services Framework components.

To deploy your application to a servlet container, you must build a WAR file. The WEB-INF folder should include the following: