Spring Integration with Axis2

There are several ways to load Spring application content in Axis2. Since our service deployments generally have access to the servlet context, it is much simpler to add a ContextLoaderListener to Axis2’s servlet listener descriptor and specify the location of the application context files in the web.xml using Spring’s contextConfigLocation init param.

Axis2 does provide a hook to access the application context via Axis2’s ApplicationContextHolder class. This class should be made available to the web service implementation and a working application context can be retrieved from it using familiar syntax. This setup is pretty straightforward and requires modification to the servlet’s web.xml along with the provision of a ApplicationContextHolder to the service implementation by way of Spring.

 

The first thing we need to do is modify the web service’s web.xml file to include a listener for Spring contexts. This will allow Axis2 to load Spring context files once the Axis2 service initializes itself. Spring will look for contexts in WEB-INF. So the various context files should be placed in this location.


Below is a snap of my web.xml document showing a ContextLoaderListener applied to Axis2’s servlet. Following the listener configuration is a list of context configuration locations. These values point to various “contexts” which my service requires. Separating contexts seems like a good approach since it allows us to break up context related material based on configuration or target environments. I’ve split my contexts across axis2, dozer, my application, and others. Where:

Dozer-context.xml – Contains configuration values for dozer mapping locations.

Axis2-context.xml – Contains configuration values for Axis2’s ApplicationContextHolder class definition.

<code>

<listener>
<listener-class>              org.springframework.web.context.ContextLoaderListener </listener-class>      </listener>    <context-param></context-param>
<param-name>contextConfigLocation</param-name>
<param-value>          /WEB-INF/application-context.xml
/WEB-INF/axis2-context.xml
/WEB-INF/dozer-context.xml </param-value>

</code>

 

The web service implementation will make use of Spring’s application context to retrieve instances of beans. This is enabled by a hook provided by Axis2’s import ApplicationContextHolder. This class can be found in:

org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder

The location of this class can be found in axis2-spring-xxx.jar.

By importing this class into the service implementation, the web service can retrieve a handle to Spring’s application context.

An example of this style of usage follows:

import org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder;

try {
ApplicationContext ctx = ApplicationContextHolder.getContext();
IService bean = (IService)ctx.getBean(“myService”);
bean.createOrder();
}catch(Exception e){

}