XFire簡介:html
XFire是新一代的Java Web服務引擎,XFire使得在JavaEE應用中發佈Web服務變得垂手可得。和其餘Web服務引擎相比,XFire的配置很是簡單,能夠很是容易地和Spring集成,它使得Java開發人員終於能夠得到和.Net開發人員同樣的開發效率。具體內容請訪問:http://baike.baidu.com/view/920041.htmljava
WebService簡介:web
它是一種構建應用程序的廣泛模型,能夠在任何支持網絡通訊的操做系統中實施運行;它是一種新的webspring
![webservice](http://static.javashuo.com/static/loading.gif)
webservice服務器
應用程序分支,是自包含、自描述、模塊 化的應用,能夠發佈、定位、經過web調用。Web Service是一個應用組件,它邏輯性的爲其餘應用程序提供數據與服務.各應用程序經過網絡協議和規定的一些標準數據格式(Http,XML,Soap)來訪問Web Service,經過Web Service內部執行獲得所需結果.Web Service能夠執行從簡單的請求到複雜商務處理的任何功能。一旦部署之後,其餘Web Service應用程序能夠發現並調用它部署的服務。網絡
具體內容請訪問:http://baike.baidu.com/view/837392.htmlapp
使用這三種技術主要是在客戶端調用服務器端的方法,屬於遠程調用。eclipse
先看服務器端:maven
新建一個java project項目,按下圖創建包結構:ide
![1 1](http://static.javashuo.com/static/loading.gif)
其實要寫的代碼很是簡單,或許你看到了會不相信,在SOAPService.java中代碼以下:
public
interface
SOAPService
{
![](http://static.javashuo.com/static/loading.gif)
String sayHi(String x);
int add(int x,int y);
int sendmsm(String context,int to);
}
![](http://static.javashuo.com/static/loading.gif)
在SOAPServiceImpl.java中代碼以下:
public
class
SOAPServiceImpl
implements
SOAPService
{
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
public String sayHi(String x)
{
return("Hello my friend, " + x + "! Glad to see you!");
}
![](http://static.javashuo.com/static/loading.gif)
public int add(int x,int y)
{
return x+y;
}
![](http://static.javashuo.com/static/loading.gif)
public int sendmsm(String context, int to)
{
return 0;
}
}
![](http://static.javashuo.com/static/loading.gif)
簡單吧?重要的是配置,pom.xml是將所用到的jar包的dependency放進去,運行在dos窗口下找到這個項目而後執行這兩條語句:mvn eclipse:clean ,mvn eclipse:eclipse語句就會將文件中所須要的jar包下載下來,固然你的電腦上必須配置maven,具體怎麼配置我就說了,若是你沒有配置的話你能夠將所須要的jar包下載下來,而後添加到build path中。在web項目最重要的就是web.xml文件中的配置,在文件中配置以下:
<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>
![](http://static.javashuo.com/static/loading.gif)
<
context-param
>
<
param-name
>
contextConfigLocation
</
param-name
>
<
param-value
>
/WEB-INF/classes/Spring-*.xml
</
param-value
>
</
context-param
>
![](http://static.javashuo.com/static/loading.gif)
<!--
編碼設置
-->
<
filter
>
<
filter-name
>
SetCharacterEncoding
</
filter-name
>
<
filter-class
>
org.springframework.web.filter.CharacterEncodingFilter
</
filter-class
>
<
init-param
>
<
param-name
>
encoding
</
param-name
>
<
param-value
>
UTF-8
</
param-value
>
</
init-param
>
</
filter
>
<
filter-mapping
>
<
filter-name
>
SetCharacterEncoding
</
filter-name
>
<
url-pattern
>
/*
</
url-pattern
>
</
filter-mapping
>
![](http://static.javashuo.com/static/loading.gif)
<!--
xfire 設置WS
-->
<
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
>
*.Service
</
url-pattern
>
</
servlet-mapping
>
![](http://static.javashuo.com/static/loading.gif)
對於xfire項目這個xfire-servlet.xml也是很重要的,在xfire-servlet.xml文件中配置下:
![](http://static.javashuo.com/static/loading.gif)
<
beans
>
<!--
引入XFire預配置信息
-->
<
import
resource
="classpath:org/codehaus/xfire/spring/xfire.xml"
/>
<!--
定義訪問的url
-->
<
bean
class
="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
>
<
property
name
="urlMap"
>
<
map
>
<
entry
key
="/Services/Hello.Service"
>
<
ref
bean
="WsHelloService"
/>
</
entry
>
</
map
>
</
property
>
</
bean
>
<!--
使用XFire導出器
-->
<
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實例
-->
<
property
name
="xfire"
ref
="xfire"
/>
</
bean
>
<
bean
id
="WsHelloService"
parent
="baseWebService"
>
<!--
業務服務bean
-->
<
property
name
="serviceBean"
ref
="WsHelloImpl"
/>
<!--
業務服務bean的窄接口類
-->
<
property
name
="serviceClass"
value
="com.cmcc.interfaces.SOAPService"
/>
</
bean
>
</
beans
>
![](http://static.javashuo.com/static/loading.gif)
上面的功能寫的很清楚了。
在spring-content.xml文件中的配置以下:
<
beans
>
<
import
resource
="spring/Spring-Service.xml"
/>
</
beans
>
它的功能鏈接spring-servicie.xml文件中的內容,就是一箇中間橋樑的做用。在spring-service.xml文件中的配置以下:
<
beans
>
<
bean
id
="WsHelloImpl"
class
="com.cmcc.impl.SOAPServiceImpl"
>
<!--
<property name="userService" ref="UserService"></property>
-->
</
bean
>
</
beans
>
![](http://static.javashuo.com/static/loading.gif)
這個配置文件就是經過spring和實體的程序鏈接到一塊兒。
看客戶端,客戶端有兩種形式,一個是自動生成的,新建一個web service project
看下圖勾選
![1 1](http://static.javashuo.com/static/loading.gif)
而後選擇finish,而後再服務器開啓的的狀態下,右擊這個項目新建一個web service client
![2 2](http://static.javashuo.com/static/loading.gif)
在wsdl中寫上訪問服務器端的url,而後finish,而後就完成了客戶端的程序,你能夠在客戶端的程序中調用服務器的方法。
![3 3](http://static.javashuo.com/static/loading.gif)
另外一種客戶端生成的形式就是本身來寫了,新建一個XFireWSDemoClient的project,而後創建以下包結構:
SOAPService.java的包路徑必定要和服務器端的包路徑相同,在WsFactory.JAVA中是用來生成service的,代碼以下:
public
class
WsFactory
{
private static ArrayList l;
private static HashMap props;
private static ObjectServiceFactory serviceFactory;
![](http://static.javashuo.com/static/loading.gif)
/** *//**
* 上傳信息
* */
![](http://static.javashuo.com/static/loading.gif)
public static SOAPService getSOAPService()
{
//調用xfire的遠程方法,將接收到的xml信息解析後發送給服務器�?
String url = "http://localhost:8080/Services/Hello.Service";
serviceFactory = new ObjectServiceFactory();
l = new ArrayList();
l.add(Integer.class.getName());
props = new HashMap();
props.put(AegisBindingProvider.WRITE_XSI_TYPE_KEY, Boolean.TRUE);
props.put(AegisBindingProvider.READ_XSI_TYPE_KEY, Boolean.TRUE);
props.put(AegisBindingProvider.OVERRIDE_TYPES_KEY, l);
Service serviceModel = serviceFactory.create(SOAPService.class,props);
SOAPService service = null;
![](http://static.javashuo.com/static/loading.gif)
try
{
service = (SOAPService) new XFireProxyFactory().create(serviceModel,url);
![](http://static.javashuo.com/static/loading.gif)
} catch (MalformedURLException e)
{
e.printStackTrace();
}
return service;
}
}
![](http://static.javashuo.com/static/loading.gif)
wstest.java只是一個簡單的測試類,代碼以下:
public
class
WSTest
{
![](http://static.javashuo.com/static/loading.gif)
public static void main(String[]args)
{
SOAPService service = WsFactory.getSOAPService();
System.out.println(service.sayHi("馮魁"));
}
}
![](http://static.javashuo.com/static/loading.gif)
這樣就會調用服務器端的方法了!
本人也是初學,只是會簡單的使用原理懂的很少,請各位大蝦批評指正!