Thursday, 31 January 2008

Web Services for Spring with Axis2

WSO2 recently launched the beta of a new toolkit called "WSF/Spring" (Web Services Framework for Spring). I wanted to take a moment to discuss this and explain the differences between this and some other approaches.

The first question you might ask is "Don't Axis2 and WSO2 WSAS both already offer Spring support?". In other words - why something different.

Basically Axis2 and WSAS both offer a way of embedding Spring into them. So what happens is a request comes in, the Axis2 engine receives it, and when the time comes to execute some service code, the Axis2 engine uses a Spring application context to get access to a Bean and call that Bean. So effectively, Axis2 (or WSAS) is in control. All the WS-* handling (modules, Axis listeners and servlets, etc) are all still configured in Axis2 land.

Now, Spring is all about inversion of control, so let's invert the control pattern. Suppose Spring is in the hot seat now. How would that work? Well, you would use Spring to deploy a servlet, Spring to configure the mapping of requests to QoS (modules), Spring to configure Axis2, and Spring to wire up the request to the bean. Sounds a little more Spring-like! And this is what WSF/Spring is.

So what does this look like?

Here is an excerpt from the web.xml:

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

<context-param>
<param-name>contextConfigLocation</param-name>

<!--Point to the application context of the application -->
<param-value>/WEB-INF/applicationContext.xml</param-value>

</context-param>

<servlet>
<servlet-name>axis2</servlet-name>
<servlet-class>
org.wso2.spring.ws.servlet.SpringAxis2Servlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>axis2</servlet-name>
<url-pattern>/axis2/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>axis2</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

So this is basically a standard Spring style web.xml with a new Axis2 servlet defined.

Now you need to import the Axis2 configuration into your application context xml:

<import resource="axis2Config.xml"/>

What's in axis2Config.xml? Basically this is axis2.xml re-worked as Spring markup - e.g.:

<bean id="axisConfig" class="org.wso2.spring.ws.SpringAxisConfiguration">
<property name="parameters">
<list>
<bean id="hd" class="org.wso2.spring.ws.beans.ParameterBean">
<property name="name" value="hotdeployment"></property>
<property name="value" value="true"></property>

</bean>
<bean id="hu" class="org.wso2.spring.ws.beans.ParameterBean">
<property name="name" value="hotupdate"></property>
<property name="value" value="true"></property>
</bean>

etc.

Obviously you can edit this as needed.

Finally you have to configure some actual services, in your application context xml:
<bean id="services" class="org.wso2.spring.ws.WebServices">
<property name="services">
<list>
<bean id="bookService" class="org.wso2.spring.ws.SpringWebService">
<property name="serviceBean" ref="productManager"></property>
<property name="serviceName" value="TestProductManager"></property>
</bean>
</list>
</property>
</bean>

As you can see, the whole of the model is now integrated closely into the standard Spring/Tomcat model of exposing an application.

So how does this compare to Spring Web Services? Well, the first thing is that SWS is mainly about contract-first. And while contract first is an excellent practice, there are times when it is not appropriate - for example it may be simply too much effort for a simple first web service. WSF/Spring supports the POJO programming model simply and effectively, and generates the WSDL automatically from the beans you expose. (WSF/Spring does also expose contract-first). The second reason is simply that some users want to use Axis2. Axis2 is a very full featured and interoperable toolkit that does support some extra standards not yet available in SpringWS such as WS-SecureConversation, WS-Trust, WS-Policy and WS-ReliableMessaging. Axis2 also takes a very different approach to enabling these standards using the module approach rather than direct wiring of handlers.

I honestly don't believe WSF/Spring is "a competitor" to SpringWS. They offer different approaches and each have different benefits. Just as Spring users have a choice of different frameworks for databinding, MVC, etc, this adds one more framework to support Web Services into the excellent Spring framework. And they also share a lot of code - for example they both use Axiom and support many of the same databinding toolkits (JIBX, JAXB, XMLBeans).

One last note: WSF/Spring is still a little early code - so don't expect a fully released, beautifully documented release just yet. However, to look on the bright side --- this is a great opportunity to get your feedback in and contribute to the direction this takes!

3 comments:

Rajith Attapattu said...

Interesting..I remember we talked about a simillar model for synapse as well.
Btw, congrats on the release.

tim said...

Very good post. I've been looking around the web for a way to integrate axis2 web services within wicket (supports spring). I wish this project was further along in development, as I need something mature. Looks like I'll be creation a n axis2 service.

Paul Fremantle said...

Tim

We are about to post the 1.0 release. The team that developed this are very responsive, so please give it a go and we'll fix any issues you come across.

Paul