spring是目前最流行的javaee framework,但是使用spring的spring-ws开发webservice却十分繁琐。xfire是一个简化webservice开发的开源项目,通过spring和xfire的结合可以大大简化基于spring framework的应用中的webservice开发。
spring和xfire可以通过多种方式结合,下文介绍的是笔者常用的一种简单而实用的方法。所用的spring版本为2.0,xfire版本为1.2.6。
1、配置xfire servlet
在web.xml中加入如下配置:
| <servlet> <servlet-name>xfireservlet</servlet-name> <servlet-class> org.codehaus.xfire.spring.xfirespringservlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>xfireservlet</servlet-name> <url-pattern>/servlet/xfireservlet/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>xfireservlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> |
2 配置spring的监听器,同基于spring的web项目一样spring的监听器是必不可少的。
| <context-param> <param-name>contextconfiglocation</param-name> <param-value> classpath:org/codehaus/xfire/spring/xfire.xml, /web-inf/applicationcontext.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.contextloaderlistener </listener-class> </listener> |
以下是完整的web.xml配置文件
| <?xml version="1.0" encoding="utf-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <context-param> <param-name>contextconfiglocation</param-name> <param-value> classpath:org/codehaus/xfire/spring/xfire.xml, /web-inf/applicationcontext.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.contextloaderlistener </listener-class> </listener> <servlet> <servlet-name>xfireservlet</servlet-name> <servlet-class> org.codehaus.xfire.spring.xfirespringservlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>xfireservlet</servlet-name> <url-pattern>/servlet/xfireservlet/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>xfireservlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app> |
3 定义接口及实现服务
定义接口,这个接口中定义要通过webservice暴露的方法
| package org.ccsoft; publicinterface hellows { public string sayhello(string sb); } |
实现服务
| package org.ccsoft; publicclass hellowsimp implements hellows { public string sayhello(string sb) { // todo auto-generated method stub return"hello "+sb; } } |
4 配置服务
将上文中实现的服务,加入到spring的配置文件中。
| <?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="hellows" class="org.ccsoft.hellowsimp"/> <bean name="helloservice" class="org.codehaus.xfire.spring.servicebean"> <property name="servicebean" ref="hellows"/> <property name="serviceclass" value="org.ccsoft.hellows"/> <property name="inhandlers"> <list> <ref bean="addressinghandler"/> </list> </property> </bean> <bean id="addressinghandler" class="org.codehaus.xfire.addressing.addressinginhandler"/> </beans> |
闽公网安备 35060202000074号