---
title: "使用XFire+Spring构建Web Service"
author: "duyunfengsolo"
date: "2010-11-10T17:54:25Z"
tags: []
categories: ["编程语言"]
slug: "xfire-spring-web-service"
draft: false
lastmod: "2010-11-10T17:56:29Z"
id: 807877
url: "https://solodu.iteye.com/blog/807877"
---

XFire是与Axis 2并列的新一代Web Service框架，通过提供简单的API支持Web Service各项标准协议，帮助你方便快速地开发Web Service应用。

相 对于Axis来说，目前XFire相对受欢迎，加上其提供了和Spring集成的支持，在目前的Web Service开源社区拥有众多的追随者。并且因为XFire为Spring提供的支持，使得我们可以很容易在Spring中使用XFire构建Web Service应用。

XFire与Axis2相比具有如下特征：

l 支持一系列Web Service的新标准--JSR181、WSDL2.0 、JAXB2、WS-Security等；

l 使用Stax解释XML,性能有了质的提高。XFire采用Woodstox 作Stax实现；

l 容易上手，可以方便快速地从pojo发布服务；

l 支持Spring、Pico、Plexus、Loom等容器；

l 灵活的Binding机制，包括默认的Aegis,xmlbeans,jaxb2,castor；

l 高性能的SOAP 栈设计；

l 支持Spring、Pico、Plexus、Loom等容器。

XFire与Axis1性能的比较如下：

l XFire比Axis1.3快2-6倍；

l XFire的响应时间是Axis1.3的1/2到1/5。

XFire在WebService框架中开始较晚，它从现有的框架中借鉴了许多优秀的理念，力争将Web Service的应用开发难度降到最低。此外，还提供了各种绑定技术、支持多种传输协议，对WebService体系中许多新的规范提供了支持。

下面让我们来看一个XFire于Spring集成的helloWorld的简单例子。

一．实现的功能和特点

本例具有如下功能和特点：

1） 基于J2EE平台的Web Service服务；

2） 开发方便，配置简单；

3） 与spring无缝集成。

XFire是完全基于流数据处理进行工作的系统，这意味着XFire不是将整个SOAP文档缓存在内存中，而是以管道的方式接收SOAP流数据。这种工作方式的转变带来了可观的性能回报，同时节省了内存的占用。

　　XFire从 管道中接收一个SOAP请求到返回一个SOAP响应，会经历一系列的阶段。在管道调用的任何一个阶段，XFire都可以添加一些额外的Handler，在 对消息进行加工处理后再传入到下一个阶段中。在SOAP请求消息对Web Service发起真正调用之前，分别会经过传输、预转发、转发、策略实施、用户信息处理、预调用、服务调用等阶段。

二．开发环境

笔者的开发环境描述如下：

1） jdk: 1.5版本；

2） Tomcat：5.5.20；

3） MyEclipse:5.1.1 GA。

三．开发步骤

1\. 工程与环境的建立

在MyEclipse 中新建Web工程，名为webservice\_helloworld。选择该工程后，点击右键选择MyEclipse-&gt;Add Web Service Capabilities，弹出Add Web Service Capabilities对话框，点击“Next”，弹出Project Library Configuration对话框，默认选择Core Libraries，点击“Finish”按钮，完成XFire核心包的添加。为了后续的客户端的测试，还需读者加入commons- httpclient.jar包到WEB-INF/lib下。

部署后可看到此时WEB-INF/lib的jar包列表如下：

activation-1.1.jar、commons -beanutils-1.7.0.jar、commons-codec-1.3.jar、commons-httpclient.jar、 commons-logging-1.0.4.jar、jaxen-1.1-beta-9.jar、jaxws-api-2.0.jar、jdom- 1.0.jar、jsr173\_api-1.0.jar、mail-1.4.jar、saaj-api-1.3.jar、saaj-impl- 1.3.jar、spring-1.2.6.jar、stax-api-1.0.1.jar、wsdl4j-1.5.2.jar、wstx-asl- 3.0.1.jar、xbean-2.1.0.jar、xbean-spring-2.5.jar、xfire-aegis-1.2.2.jar、 xfire-annotations-1.2.2.jar、xfire-core-1.2.2.jar、xfire-java5-1.2.2.jar、 xfire-jaxws-1.2.2.jar、xfire-jsr181-api-1.0-M1.jar、xfire-spring- 1.2.2.jar、XmlSchema-1.1.jar

为了后续的开发和测试，在src目录下分别建立test和webservice目录，分别用于存放测试文件和webservice的相关类。

2．Web Service实现的编写

在本例中，我们只是做一个helloWorld的简单例子。Web Service服务端提供一个根据输入的名字信息回复相应的helloWorld信息的。例如，当名字为“阿蜜果”时，恢复信息为“hello，阿蜜果”。下面让我们一步一步来开始进行编码。

1）web.xml的配置

一 般情况下，我们通过HTTP作为Web Service的传输协议，这样我们只需启动一个Web服务器(如Tomcat，在本例中使用的是Tomcat5.5.20)，这样客户端就可以通过 HTTP访问到Web Service服务。为了集成Spring容器，XFire专门提供一个XFireSpringServlet，我们可以在web.xml中配置该 Servlet，将Spring容器中定义的Web Service在某个URI下发布。

为了能正确使用XFire，需在web.xml中进行相应配置，在该文件中配置XFire的servlet和servlet-mapping。同时因为本实例需要将XFire集成到Spring中，因而需要在web.xml文件中加载Spring的相应配置文件。在本实例中，我们首先在WEB-INF下建立两个配置Spring配置文件，一个为applicationContext.xml，该文件用来定义本工程的bean，一个为xfire-servlet.xml，用来配置XFire的相关bean。修改后的web.xml的内容如下所示：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;web-app xmlns="[http://java.sun.com/xml/ns/j2ee](http://java.sun.com/xml/ns/j2ee)" xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)" version="2.4" xsi:schemaLocation="[http://java.sun.com/xml/ns/j2ee](http://java.sun.com/xml/ns/j2ee) [http://java.sun.com/xml/ns/j2ee/web-app\_2\_4.xsd](http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd)"&gt;  
&lt;display-name&gt;XFireService&lt;/display-name&gt;  
&lt;!-- begin Spring配置 --&gt;  
&lt;context-param&gt;  
&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;  
&lt;param-value&gt;/WEB-INF/applicationContext.xml,/WEB-INF/xfire-servlet.xml&lt;/param-value&gt;  
&lt;/context-param&gt;  
&lt;listener&gt;  
&lt;listener-class&gt; org.springframework.web.context.ContextLoaderListener  
&lt;/listener-class&gt;  
&lt;/listener&gt;  
&lt;listener&gt;  
&lt;listener-class&gt; org.springframework.web.util.IntrospectorCleanupListener  
&lt;/listener-class&gt;  
&lt;/listener&gt;  
&lt;!-- end Spring配置 --&gt;

&lt;!-- begin XFire 配置 --&gt;  
&lt;servlet&gt;  
&lt;servlet-name&gt;xfire&lt;/servlet-name&gt;  
&lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;  
&lt;/servlet&gt;  
&lt;servlet-mapping&gt;  
&lt;servlet-name&gt;xfire&lt;/servlet-name&gt;  
&lt;url-pattern&gt;\*.ws&lt;/url-pattern&gt;  
&lt;/servlet-mapping&gt;  
&lt;servlet&gt;  
&lt;!-- 配合Spring容器中XFire一起工作的Servlet--&gt;  
&lt;servlet-name&gt;xfireServlet&lt;/servlet-name&gt;  
&lt;servlet-class&gt;org.codehaus.xfire.spring.XFireSpringServlet&lt;/servlet-class&gt;  
&lt;/servlet&gt;  
&lt;servlet-mapping&gt;  
&lt;servlet-name&gt;xfireServlet&lt;/servlet-name&gt;  
&lt;!-- 在这个URI下开放Web Service服务 --&gt;  
&lt;url-pattern&gt;/service/\*&lt;/url-pattern&gt;  
&lt;/servlet-mapping&gt;  
&lt;!-- end XFire 配置 --&gt;  
&lt;/web-app&gt;

2）Web Service的接口类HelloWorld.java和对应实现类HelloWorldImpl.java

为了用Web Service完成HelloWorld功能，我们首先在src/webservice目录下建立接口类HelloWold.java。它仅包含一个sayHelloWorld(String name)的方法，其详细内容如下：

package webservice;

/\** \*//\**  
\*HelloWorld的接口类.  
\*/  
publicinterface HelloWorld {  
/\** \*//\**  
\*对名字为name的人打招呼.  
[\*@paramname](mailto:*@paramname)名字  
[\*@return](mailto:*@return)返回打招呼的字符串  
\*/  
String sayHelloWorld(String name);  
}  
我们还需要建立一个对应的实现类，来实现sayHelloWorld的功能，该实现类即为HelloWorldImpl.java。该类的详细内容如下：  
package webservice;  
/\** \*//\**  
\*HelloWorld的实现类.  
\*/  
publicclass HelloWorldImpl implements HelloWorld {  
public String sayHelloWorld(String name) {  
String helloWorld = "hello," + name;  
return helloWorld;  
}  
}

3）Spring配置文件applicationContext.xml和xfire-servlet.xml的配置

首先我们在applicationContext.xml文件中配置对应的bean——HelloWorldBean，该xml文件的内容如下：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"  
"[http://www.springframework.org/dtd/spring-beans.dtd](http://www.springframework.org/dtd/spring-beans.dtd)"&gt;  
&lt;beans&gt;  
&lt;bean id="HelloWorldBean" class="webservice.HelloWorldImpl"/&gt;  
&lt;/beans&gt;  
这个配置文件很简单，在此不详述。

XFire为Spring 提供了方便易用的导出器XFireExporter，借助该导出器的支持，我们可以在Spring容器中将一个POJO导出为Web Service。HelloWorld是业务服务类，在此拥有一个sayHelloWorld的方法，我们希望将此方法开放为Web Service。在实际应用中，如果某个类具有众多的方法，而其中的某些方法不需要开放为Web Service的情况下，我们可以定义一个窄接口，该接口中只需定义那些开放为Web Service的业务方法。

将一个业务类所有需要开放为Web Service的方法通过一个窄接口来描述是值得推荐的作法，这让Web Service的接口显得很“干净”。其次，XFire的导出器也需要服务接口的支持，因为它采用基于接口的动态代理技术。

窄接口中的方法在真实的系统中可能需要引用其它的业务类或DAO获取数据库中的真实数据，为了简化实例，我们在此简化了实例。

下面让我们看看在xfire-servlet.xml文件中导出器的设置，该文件内容如下：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"  
"[http://www.springframework.org/dtd/spring-beans.dtd](http://www.springframework.org/dtd/spring-beans.dtd)"&gt;  
&lt;beans&gt;  
&lt;!-- 引入XFire预配置信息 --&gt;  
&lt;import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /&gt;  
&lt;!—定义访问的url--&gt;  
&lt;bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt;  
&lt;property name="urlMap"&gt;  
&lt;map&gt;  
&lt;entry key="/HelloWorldService.ws"&gt;  
&lt;ref bean="HelloWorldService" /&gt;  
&lt;/entry&gt;  
&lt;/map&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;!-- 使用XFire导出器 --&gt;  
&lt;bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true"&gt;  
&lt;!-- 引用xfire.xml中定义的工厂 --&gt;  
&lt;property name="serviceFactory" ref="xfire.serviceFactory" /&gt;  
&lt;!-- 引用xfire.xml中的xfire实例 --&gt;  
&lt;property name="xfire" ref="xfire" /&gt;  
&lt;/bean&gt;  
&lt;bean id="HelloWorldService" parent="baseWebService"&gt;  
&lt;!-- 业务服务bean --&gt;  
&lt;property name="serviceBean" ref="HelloWorldBean" /&gt;  
&lt;!-- 业务服务bean的窄接口类 --&gt;  
&lt;property name="serviceClass" value="webservice.HelloWorld" /&gt;  
&lt;/bean&gt;  
&lt;/beans&gt;

在 上面的配置中，我们可以看到，在该配置文件中引入了xfire.xml这个Spring配置文件。它是在XFire核心JAR包中拥有一个预定义的 Spring配置文件，它定义了XFire在Spring中必须用到的一些Bean和资源，需要引入这个预定义的配置文件。从该配置文件中可以看出，我们 通过XFireExporter将业务类导出为Web Service，对于任何导出器，我们都需要引入XFire环境，即serviceFactory和xfire，这是标准的配置。ServiceFactory是XFire的核心类，它可以将一个POJO生成为一个Web Service。

在本实例中，我们通过定义一个baseWebService，其余的webService配置都将该bean作为父bean，这样可以简化Spring的配置，不需要多次引入serviceFactory和xfire。

3\. Web Service的测试

在上一步操作完成之后，我们的这个简单的Web Service已经编写完毕，下面让我们来看看自己的劳动成果吧。

在浏览器中输入地址：[http://localhost:8080/webservice\_helloworld/HelloWorldService.ws?wsdl](http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl)，我们可以看到HelloWorldService对应的WSDL信息，阅读这个WSDL文档，我们可以知道HelloWorld的sayHelloWorld方法已经被成功地发布为Web Service了。只要拿到这个WSDL就可以开发相应的客户端调用程序了。

XFire为访问服务端Web Service提供了各种方便的方式：我们一般根据服务地址和窄接口类创建客户调用程序。

　　在不能获得服务窄接口类的情况下，XFire允许我们通过WSDL文件生成客户端调用程序，通过指定服务接口的方式调用服务。

1）通过WSDL文件生成客户端调用程序

首先我们通过[http://localhost:8080/webservice\_helloworld/HelloWorldService.ws?wsdl](http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl)我们可以获得WSDL文件HelloWorldService.wsdl，并将其放在src目录下面，接着我们通过程序访问该WSDL文件，并调用需测试的方法。此时测试类WebServiceClientTest.java的内容如下所示：

package test;

import org.codehaus.xfire.client.Client;  
import org.springframework.core.io.ClassPathResource;  
import org.springframework.core.io.Resource;  
import webservice.HelloWorld;

/\** \*//\**  
\*Copyright2007GuangZhouAmigo.  
\*Allrightreserved.  
\*HelloWorld的webservice的测试类.  
[mailto:\*@author%3Ca](mailto:*@author%3Ca) href="[mailto:xiexingxing1121@126.com%22%3EAmigoXie%3C/a](mailto:xiexingxing1121@126.com%22%3EAmigoXie%3C/a)&gt;  
[\*@version1.0](mailto:*@version1.0)  
\*Creationdate:2007-9-16-下午05:36:05  
\*/  
publicclass WebServiceClientTest {  
HelloWorld helloWorld = null;

publicstaticvoid main(String\[] args) throws Exception {  
WebServiceClientTest test = new WebServiceClientTest();  
test.testClient();  
}

publicvoid testClient() throws Exception {  
String wsdl = "HelloWorldService.wsdl"; //对应的WSDL文件  
Resource resource = new ClassPathResource(wsdl);  
Client client = new Client(resource.getInputStream(), null); //根据WSDL创建客户实例

Object\[] objArray = new Object\[1];  
objArray\[0] = "阿蜜果";  
//调用特定的Web Service方法  
Object\[] results = client.invoke("sayHelloWorld", objArray);  
System.out.println("result: " + results\[0]);  
}  
}  
运行该类，可得到如下输出结果：

result: hello,阿蜜果

可看出运行结果正确。

2）根据服务地址创建客户端调用程序

接着让我们来看一个根据服务地址创建客户端调用程序的例子。我们可以通过测试类来测试Web Service的正确性，下面让我们来看一个简单的测试类，首先我们在src/test目录建立WebServiceClientTest.java文件，并在src目录下建立客户端调用的Spring配置文件client.xml。在client.xml配置文件中我们定义了一个testWebService的bean，该bean访问wsdlDocumentUrl为[http://localhost:8080/webservice\_helloworld/HelloWorldService.ws?wsdl](http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl)的WSDL。该xml文件的详细内容如下：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"  
"[http://www.springframework.org/dtd/spring-beans.dtd](http://www.springframework.org/dtd/spring-beans.dtd)"&gt;  
&lt;beans&gt;  
&lt;bean id="testWebService" class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean"&gt;  
&lt;property name="serviceClass"&gt;  
&lt;value&gt;webservice.HelloWorld&lt;/value&gt;  
&lt;/property&gt;  
&lt;property name="wsdlDocumentUrl"&gt;  
&lt;value&gt;http://localhost:8080/webservice\_helloworld/HelloWorldService.ws?wsdl&lt;/value&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
&lt;/beans&gt;

在WebServiceClientTest.java文件中获得HelloWorld，并调用它的sayHelloWorld方法来完成测试，该类的详细内容如下所示：

package test;

import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
import webservice.HelloWorld;

/\** \*//\**  
\*HelloWorld的webservice的测试类.  
\*/  
publicclass WebServiceClientTest {  
HelloWorld helloWorld = null;

publicstaticvoid main(String\[] args) {  
WebServiceClientTest test = new WebServiceClientTest();  
test.testClient();  
}

publicvoid testClient() {  
ApplicationContext ctx = new ClassPathXmlApplicationContext(  
"client.xml");  
helloWorld = (HelloWorld) ctx.getBean("testWebService");  
System.out.println(helloWorld.sayHelloWorld("阿蜜果"));  
}  
}

在启动webservice\_helloworld工程的情况下，运行WebServiceClientTest类，可看到控制台包含如下信息：

hello,阿蜜果

由此可看出调用Web Service成功。

四．总结

与Axis 相比，在实施Web Service时XFire更加简洁高效，并且XFire对Spring提供了强大的支持，可以非常方便地在Spring中使用XFire实施Web Service，因此XFire在短短的时间里成为了受Web Service开发者喜爱的框架。

　 　XFire为客户端提供了多种访问Web Service的方式，如果可以获取客户端的窄接口类，则可以采用窄接口类调用Web Service。如果仅能获取WSDL，XFire也可以采用动态反射的机制调用Web Service。XFire为Eclipse提供了一个可以根据WSDL生成客户端存根代码的插件，相信XFire也将为其它非Java语言提供类似的插 件。

在Axis2中也开始使用Stax来处理XML，还有Xfire不支持WSDL2.0, 而Axis2支持WSDL2.0。