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:
Develop the application's business logic.
Only the service implementation needs to be developed service provider endpoints.
The business logic for service consumers should be encapsulated in a Java class and not as part of the
main()
method.
Update the application's web.xml
file to load the Spring context listener and the
application's Spring configuration.
Create a Spring configuration file that explicitly loads all of the application's components and all of the required FUSE Services Framework components.
Package the application into a WAR file for deployment.
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><param-name>contextConfigLocation</param-name> <param-value>WEB-INF/beans.xml</param-value> </context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ... </web-app>
The XML in Example 6.5 does the following:
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.
Example 6.6. Configuration for a Consumer Deployed into a Servlet Container Using the Spring Context Listener
<beans ... > <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-jaxws.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-http-binding.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <jaxws:client id="funguy" address="http://localhost:9000/funguyTool" serviceClass="org.laughs.funGuyImpl" /> </beans>
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 |
---|---|
Importing the |
To deploy your application to a servlet container, you must build a WAR file. The WEB-INF
folder should include the following:
beans.xml
— the Spring configuration file configuring the application's beans.
web.xml
— the web application file that instructs the servlet container to load the
Spring context listener.
classes
— a folder including the Web service implementation class and any
other classes required to support the implementation.
wsdl
— a folder including the WSDL file that defines the service being
deployed.
lib
— a folder including any JARs required by the application.