XFire是與Axis 2并列的新一代Web Service框架,通過提供簡單的API支持Web Service各項(xiàng)標(biāo)準(zhǔn)協(xié)議,幫助你方便快速地開發(fā)Web Service應(yīng)用。
相對(duì)于Axis來說,目前XFire相對(duì)受歡迎,加上其提供了和Spring集成的支持,在目前的Web Service開源社區(qū)擁有眾多的追隨者。并且因?yàn)閄Fire為Spring提供的支持,使得我們可以很容易在Spring中使用XFire構(gòu)建Web Service應(yīng)用。
XFire與Axis2相比具有如下特征:
l 支持一系列Web Service的新標(biāo)準(zhǔn)--JSR181、WSDL2.0 、JAXB2、WS-Security等;
l 使用Stax解釋XML,性能有了質(zhì)的提高。XFire采用Woodstox 作Stax實(shí)現(xiàn);
l 容易上手,可以方便快速地從pojo發(fā)布服務(wù);
l 支持Spring、Pico、Plexus、Loom等容器;
l 靈活的Binding機(jī)制,包括默認(rèn)的Aegis,xmlbeans,jaxb2,castor;
l 高性能的SOAP 棧設(shè)計(jì);
l 支持Spring、Pico、Plexus、Loom等容器。
XFire與Axis1性能的比較如下:
l XFire比Axis1.3快2-6倍;
l XFire的響應(yīng)時(shí)間是Axis1.3的1/2到1/5。
XFire在WebService框架中開始較晚,它從現(xiàn)有的框架中借鑒了許多優(yōu)秀的理念,力爭將Web Service的應(yīng)用開發(fā)難度降到最低。此外,還提供了各種綁定技術(shù)、支持多種傳輸協(xié)議,對(duì)WebService體系中許多新的規(guī)范提供了支持。
下面讓我們來看一個(gè)XFire于Spring集成的helloWorld的簡單例子。
一.實(shí)現(xiàn)的功能和特點(diǎn)
本例具有如下功能和特點(diǎn):
1) 基于J2EE平臺(tái)的Web Service服務(wù);
2) 開發(fā)方便,配置簡單;
3) 與spring無縫集成。
XFire是完全基于流數(shù)據(jù)處理進(jìn)行工作的系統(tǒng),這意味著XFire不是將整個(gè)SOAP文檔緩存在內(nèi)存中,而是以管道的方式接收SOAP流數(shù)據(jù)。這種工作方式的轉(zhuǎn)變帶來了可觀的性能回報(bào),同時(shí)節(jié)省了內(nèi)存的占用。
XFire從管道中接收一個(gè)SOAP請(qǐng)求到返回一個(gè)SOAP響應(yīng),會(huì)經(jīng)歷一系列的階段。在管道調(diào)用的任何一個(gè)階段,XFire都可以添加一些額外的Handler,在對(duì)消息進(jìn)行加工處理后再傳入到下一個(gè)階段中。在SOAP請(qǐng)求消息對(duì)Web Service發(fā)起真正調(diào)用之前,分別會(huì)經(jīng)過傳輸、預(yù)轉(zhuǎn)發(fā)、轉(zhuǎn)發(fā)、策略實(shí)施、用戶信息處理、預(yù)調(diào)用、服務(wù)調(diào)用等階段。
二.開發(fā)環(huán)境
筆者的開發(fā)環(huán)境描述如下:
1) jdk: 1.5版本;
2) Tomcat:5.5.20;
3) MyEclipse:5.1.1 GA。
三.開發(fā)步驟
1. 工程與環(huán)境的建立
在MyEclipse中新建Web工程,名為webservice_helloworld。選擇該工程后,點(diǎn)擊右鍵選擇MyEclipse->Add Web Service Capabilities,彈出Add Web Service Capabilities對(duì)話框,點(diǎn)擊“Next”,彈出Project Library Configuration對(duì)話框,默認(rèn)選擇Core Libraries,點(diǎn)擊“Finish”按鈕,完成XFire核心包的添加。為了后續(xù)的客戶端的測試,還需讀者加入commons-httpclient.jar包到WEB-INF/lib下。
部署后可看到此時(shí)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
為了后續(xù)的開發(fā)和測試,在src目錄下分別建立test和webservice目錄,分別用于存放測試文件和webservice的相關(guān)類。
2.Web Service實(shí)現(xiàn)的編寫
在本例中,我們只是做一個(gè)helloWorld的簡單例子。Web Service服務(wù)端提供一個(gè)根據(jù)輸入的名字信息回復(fù)相應(yīng)的helloWorld信息的。例如,當(dāng)名字為“阿蜜果”時(shí),恢復(fù)信息為“hello,阿蜜果”。下面讓我們一步一步來開始進(jìn)行編碼。
1)web.xml的配置
一般情況下,我們通過HTTP作為Web Service的傳輸協(xié)議,這樣我們只需啟動(dòng)一個(gè)Web服務(wù)器(如Tomcat,在本例中使用的是Tomcat5.5.20),這樣客戶端就可以通過HTTP訪問到Web Service服務(wù)。為了集成Spring容器,XFire專門提供一個(gè)XFireSpringServlet,我們可以在web.xml中配置該Servlet,將Spring容器中定義的Web Service在某個(gè)URI下發(fā)布。
為了能正確使用XFire,需在web.xml中進(jìn)行相應(yīng)配置,在該文件中配置XFire的servlet和servlet-mapping。同時(shí)因?yàn)楸緦?shí)例需要將XFire集成到Spring中,因而需要在web.xml文件中加載Spring的相應(yīng)配置文件。在本實(shí)例中,我們首先在WEB-INF下建立兩個(gè)配置Spring配置文件,一個(gè)為applicationContext.xml,該文件用來定義本工程的bean,一個(gè)為xfire-servlet.xml,用來配置XFire的相關(guān)bean。修改后的web.xml的內(nèi)容如下所示: 2)Web Service的接口類HelloWorld.java和對(duì)應(yīng)實(shí)現(xiàn)類HelloWorldImpl.java
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="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/web-app_2_4.xsd">
<display-name>XFireService</display-name>
<!-- begin Spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/xfire-servlet.xml</param-value>
</context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class> org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
</listener>
<!-- end Spring配置 -->

<!-- begin XFire 配置 -->
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>*.ws</url-pattern>
</servlet-mapping>
<servlet>
<!-- 配合Spring容器中XFire一起工作的Servlet-->
<servlet-name>xfireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfireServlet</servlet-name>
<!-- 在這個(gè)URI下開放Web Service服務(wù) -->
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<!-- end XFire 配置 -->
</web-app>

為了用Web Service完成HelloWorld功能,我們首先在src/webservice目錄下建立接口類HelloWold.java。它僅包含一個(gè)sayHelloWorld(String name)的方法,其詳細(xì)內(nèi)容如下:
3)Spring配置文件applicationContext.xml和xfire-servlet.xml的配置
package webservice;

/**
*HelloWorld的接口類.
*/
publicinterface HelloWorld {
/**
*對(duì)名字為name的人打招呼.
*@paramname名字
*@return返回打招呼的字符串
*/
String sayHelloWorld(String name);
}
我們還需要建立一個(gè)對(duì)應(yīng)的實(shí)現(xiàn)類,來實(shí)現(xiàn)sayHelloWorld的功能,該實(shí)現(xiàn)類即為HelloWorldImpl.java。該類的詳細(xì)內(nèi)容如下:
package webservice;
/**
*HelloWorld的實(shí)現(xiàn)類.
*/
publicclass HelloWorldImpl implements HelloWorld {
public String sayHelloWorld(String name) {
String helloWorld = "hello," + name;
return helloWorld;
}
}

首先我們?cè)?/span>applicationContext.xml文件中配置對(duì)應(yīng)的bean——HelloWorldBean,該xml文件的內(nèi)容如下: 這個(gè)配置文件很簡單,在此不詳述。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="HelloWorldBean" class="webservice.HelloWorldImpl"/>
</beans>
XFire為Spring提供了方便易用的導(dǎo)出器XFireExporter,借助該導(dǎo)出器的支持,我們可以在Spring容器中將一個(gè)POJO導(dǎo)出為Web Service。HelloWorld是業(yè)務(wù)服務(wù)類,在此擁有一個(gè)sayHelloWorld的方法,我們希望將此方法開放為Web Service。在實(shí)際應(yīng)用中,如果某個(gè)類具有眾多的方法,而其中的某些方法不需要開放為Web Service的情況下,我們可以定義一個(gè)窄接口,該接口中只需定義那些開放為Web Service的業(yè)務(wù)方法。
將一個(gè)業(yè)務(wù)類所有需要開放為Web Service的方法通過一個(gè)窄接口來描述是值得推薦的作法,這讓W(xué)eb Service的接口顯得很“干凈”。其次,XFire的導(dǎo)出器也需要服務(wù)接口的支持,因?yàn)樗捎没诮涌诘膭?dòng)態(tài)代理技術(shù)。
窄接口中的方法在真實(shí)的系統(tǒng)中可能需要引用其它的業(yè)務(wù)類或DAO獲取數(shù)據(jù)庫中的真實(shí)數(shù)據(jù),為了簡化實(shí)例,我們?cè)诖撕喕藢?shí)例。
下面讓我們看看在xfire-servlet.xml文件中導(dǎo)出器的設(shè)置,該文件內(nèi)容如下:serviceFactory和xfire,這是標(biāo)準(zhǔn)的配置。ServiceFactory是XFire的核心類,它可以將一個(gè)POJO生成為一個(gè)Web Service。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 引入XFire預(yù)配置信息 -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<!—定義訪問的url-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/HelloWorldService.ws">
<ref bean="HelloWorldService" />
</entry>
</map>
</property>
</bean>

<!-- 使用XFire導(dǎo)出器 -->
<bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true">
<!-- 引用xfire.xml中定義的工廠 -->
<property name="serviceFactory" ref="xfire.serviceFactory" />
<!-- 引用xfire.xml中的xfire實(shí)例 -->
<property name="xfire" ref="xfire" />
</bean>
<bean id="HelloWorldService" parent="baseWebService">
<!-- 業(yè)務(wù)服務(wù)bean -->
<property name="serviceBean" ref="HelloWorldBean" />
<!-- 業(yè)務(wù)服務(wù)bean的窄接口類 -->
<property name="serviceClass" value="webservice.HelloWorld" />
</bean>
</beans>

在上面的配置中,我們可以看到,在該配置文件中引入了xfire.xml這個(gè)Spring配置文件。它是在XFire核心JAR包中擁有一個(gè)預(yù)定義的Spring配置文件,它定義了XFire在Spring中必須用到的一些Bean和資源,需要引入這個(gè)預(yù)定義的配置文件。從該配置文件中可以看出,我們通過XFireExporter將業(yè)務(wù)類導(dǎo)出為Web Service,對(duì)于任何導(dǎo)出器,我們都需要引入XFire環(huán)境,即
在本實(shí)例中,我們通過定義一個(gè)baseWebService,其余的webService配置都將該bean作為父bean,這樣可以簡化Spring的配置,不需要多次引入serviceFactory和xfire。
3. Web Service的測試
在上一步操作完成之后,我們的這個(gè)簡單的Web Service已經(jīng)編寫完畢,下面讓我們來看看自己的勞動(dòng)成果吧。
在瀏覽器中輸入地址:http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl,我們可以看到HelloWorldService對(duì)應(yīng)的WSDL信息,閱讀這個(gè)WSDL文檔,我們可以知道HelloWorld的sayHelloWorld方法已經(jīng)被成功地發(fā)布為Web Service了。只要拿到這個(gè)WSDL就可以開發(fā)相應(yīng)的客戶端調(diào)用程序了。
XFire為訪問服務(wù)端Web Service提供了各種方便的方式:我們一般根據(jù)服務(wù)地址和窄接口類創(chuàng)建客戶調(diào)用程序。
在不能獲得服務(wù)窄接口類的情況下,XFire允許我們通過WSDL文件生成客戶端調(diào)用程序,通過指定服務(wù)接口的方式調(diào)用服務(wù)。
1)通過WSDL文件生成客戶端調(diào)用程序
首先我們通過http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl我們可以獲得WSDL文件HelloWorldService.wsdl,并將其放在src目錄下面,接著我們通過程序訪問該WSDL文件,并調(diào)用需測試的方法。此時(shí)測試類WebServiceClientTest.java的內(nèi)容如下所示:
運(yùn)行該類,可得到如下輸出結(jié)果:
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的測試類.
*@author<a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
*@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"; //對(duì)應(yīng)的WSDL文件
Resource resource = new ClassPathResource(wsdl);
Client client = new Client(resource.getInputStream(), null); //根據(jù)WSDL創(chuàng)建客戶實(shí)例
Object[] objArray = new Object[1];
objArray[0] = "阿蜜果";
//調(diào)用特定的Web Service方法
Object[] results = client.invoke("sayHelloWorld", objArray);
System.out.println("result: " + results[0]);
}
}
result: hello,阿蜜果
可看出運(yùn)行結(jié)果正確。
2)根據(jù)服務(wù)地址創(chuàng)建客戶端調(diào)用程序
接著讓我們來看一個(gè)根據(jù)服務(wù)地址創(chuàng)建客戶端調(diào)用程序的例子。我們可以通過測試類來測試Web Service的正確性,下面讓我們來看一個(gè)簡單的測試類,首先我們?cè)趕rc/test目錄建立WebServiceClientTest.java文件,并在src目錄下建立客戶端調(diào)用的Spring配置文件client.xml。在client.xml配置文件中我們定義了一個(gè)testWebService的bean,該bean訪問wsdlDocumentUrl為http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl的WSDL。該xml文件的詳細(xì)內(nèi)容如下: 在WebServiceClientTest.java文件中獲得HelloWorld,并調(diào)用它的sayHelloWorld方法來完成測試,該類的詳細(xì)內(nèi)容如下所示: 在啟動(dòng)webservice_helloworld工程的情況下,運(yùn)行WebServiceClientTest類,可看到控制臺(tái)包含如下信息:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="testWebService" class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
<property name="serviceClass">
<value>webservice.HelloWorld</value>
</property>
<property name="wsdlDocumentUrl">
<value>http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl</value>
</property>
</bean>
</beans>

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("阿蜜果"));
}
}

hello,阿蜜果
由此可看出調(diào)用Web Service成功。
四.總結(jié)
與Axis相比,在實(shí)施Web Service時(shí)XFire更加簡潔高效,并且XFire對(duì)Spring提供了強(qiáng)大的支持,可以非常方便地在Spring中使用XFire實(shí)施Web Service,因此XFire在短短的時(shí)間里成為了受Web Service開發(fā)者喜愛的框架。
XFire為客戶端提供了多種訪問Web Service的方式,如果可以獲取客戶端的窄接口類,則可以采用窄接口類調(diào)用Web Service。如果僅能獲取WSDL,XFire也可以采用動(dòng)態(tài)反射的機(jī)制調(diào)用Web Service。XFire為Eclipse提供了一個(gè)可以根據(jù)WSDL生成客戶端存根代碼的插件,相信XFire也將為其它非Java語言提供類似的插件。
技術(shù)可用性的一個(gè)很大的標(biāo)準(zhǔn)是它是否方便測試,XFire提供多種方式進(jìn)行Web Service的測試,簡單方便,給Web Service開發(fā)人員的測試工作帶來了福音。
在本文中,筆者通過一個(gè)簡單的helloWorld的Web Service例子,詳細(xì)地說明了用XFire+Spring構(gòu)建Web Service時(shí)配置文件的相關(guān)配置,以及測試的各種方法,也讓讀者見識(shí)了XFire與Spring的無縫集成,希望對(duì)讀者學(xué)習(xí)XFire有點(diǎn)幫助。
原文出處:http://www.cnblogs.com/ksuifeng/archive/2010/05/18/1738749.html