Spring是目前最流行的JavaEE Framework但是使用Spring的SpringWS开发WebService却十分繁琐XFire是一个简化WebService开发的开源项目通过Spring和XFire的结合可以大大简化基于Spring Framework的应用中的Web Service开发
Spring和XFire可以通过多种方式结合下文介绍的是笔者常用的一种简单而实用的方法所用的Spring版本为XFire版本为
配置XFire Servlet
在webxml中加入如下配置
<servlet><servletname>XFireServlet</servletname>
<servletclass>
orgcodehausxfirespringXFireSpringServlet
</servletclass>
</servlet>
<servletmapping>
<servletname>XFireServlet</servletname>
<urlpattern>/servlet/XFireServlet/*</urlpattern>
</servletmapping>
<servletmapping>
<servletname>XFireServlet</servletname>
<urlpattern>/services/*</urlpattern>
</servletmapping>
配置Spring的监听器同基于spring的Web项目一样Spring的监听器是必不可少的
<contextparam><paramname>contextConfigLocation</paramname>
<paramvalue>
classpath:org/codehaus/xfire/spring/xfirexml
/WEBINF/applicationContextxml
</paramvalue>
</contextparam>
<listener>
<listenerclass>
orgspringframeworkwebcontextContextLoaderListener
</listenerclass>
</listener>
以下是完整的webxml配置文件
<?xml version= encoding=UTF?><webapp version=
xmlns=http://javasuncom/xml/ns/jee
xmlns:xsi=http://wwwworg//XMLSchemainstance
xsi:schemaLocation=http://javasuncom/xml/ns/jee
http://javasuncom/xml/ns/jee/webapp__xsd>
<contextparam>
<paramname>contextConfigLocation</paramname>
<paramvalue>
classpath:org/codehaus/xfire/spring/xfirexml
/WEBINF/applicationContextxml
</paramvalue>
</contextparam>
<listener>
<listenerclass>
orgspringframeworkwebcontextContextLoaderListener
</listenerclass>
</listener>
<servlet>
<servletname>XFireServlet</servletname>
<servletclass>
orgcodehausxfirespringXFireSpringServlet
</servletclass>
</servlet>
<servletmapping>
<servletname>XFireServlet</servletname>
<urlpattern>/servlet/XFireServlet/*</urlpattern>
</servletmapping>
<servletmapping>
<servletname>XFireServlet</servletname>
<urlpattern>/services/*</urlpattern>
</servletmapping>
</webapp>
定义接口及实现服务
定义接口这个接口中定义要通过WebService暴露的方法
package orgccsoft;
publicinterface HelloWS {
public String sayHello(String sb);
}
实现服务
package orgccsoft;
publicclass HelloWSImp implements HelloWS {
public String sayHello(String sb) {
// TODO Autogenerated method stub
returnHello +sb;
}
}
配置服务
将上文中实现的服务加入到spring的配置文件中
<?xml version= encoding=UTF?>
<beans xmlns=http://wwwspringframeworkorg/schema/beans
xmlns:xsi=http://wwwworg//XMLSchemainstance
xsi:schemaLocation=
http://wwwspringframeworkorg/schema/beans
http://wwwspringframeworkorg/schema/beans/springbeansxsd>
<bean id=helloWS class=orgccsoftHelloWSImp/>
<bean name=helloService class=orgcodehausxfirespringServiceBean>
<property name=serviceBean ref=helloWS/>
<property name=serviceClass value=orgccsoftHelloWS/>
<property name=inHandlers>
<list>
<ref bean=addressingHandler/>
</list>
</property>
</bean>
<bean id=addressingHandler
class=orgcodehausxfireaddressingAddressingInHandler/>
</beans>