【黑馬Android】(15)cxf 介紹 以及ant 工具的使用/cxf總結

來源:互聯網
上載者:User

標籤:

cxf 介紹 以及ant 工具的使用


ant 工具:1、為什麼要用到ant這個工具呢?Ant做為一種工具已經廣泛被使用,並且曆史悠久。
使用ant的內建命令,可以編譯java源檔案(javac),運行java檔案(java),給class檔案打包(jar、war、ear),
也可以建立(mkdir)、刪除(del)、拷貝(copy),甚至可以使用ant執行sql檔案。
由於ant是用xml語言寫成的檔案,並取預設名為build.xml檔案。
所以,今後大家應該在見到名為build.xml檔案時知道這是一個ant的檔案。


ant 工具後面跟的是任務的名稱


ant server 運行了Server類,發布了一個webservice


ant client 調用已經發布的webservice 


ant clean 清除已經產生的class 檔案


ant war 將java 項目打成一個war 包


ant deploy -Dtomcat=true 把打成的war 拷貝到tomcat 的webapp 下面去。


ant undeploy -Dtomcat=true; 卸載tomcat 下面的項目..

cxf總結服務端:
用cxf 架構提供的類發布一個服務
使用cxf 提供 ServerFactoryBean 來發布webservice 
被發布的類當中可以不需要標註webservice 註解,類當中可以不包含有效方法,
如果沒有包含有效方法.它會提供一個空的服務.
//建立發布服務的類...
ServerFactoryBean bean=new ServerFactoryBean();
bean.setAddress("http://192.168.9.100:8080/server");//服務對外的訪問地址
bean.setServiceClass(CxfWebService.class);//設定服務類的介面類型,如果沒有介面則為當前類..
bean.setServiceBean(new CxfWebService());//設定服務類的實現
bean.create();//發布服務
第二種發布方式:
------------------------------------------------------------------------------------------
使用cxf 架構提供的類 jaxWsServerFactoryBean 發布webService
jaxWsServerFactoryBean 是 ServerFactoryBean 的子類... 
jaxWsServerFactoryBean bean=new jaxWsServerFactoryBean();
bean.setAddress("http://192.168.9.100:8080/server");//服務對外的訪問地址
bean.setServiceClass(CxfWebService.class);//設定服務類的介面類型,如果沒有介面則為當前類..
bean.setServiceBean(new CxfWebService());//設定服務類的實現
bean.create();//發布服務


-------------------------------------------------------------------------------------------
用戶端:
用cxf 架構提供的類調用服務.. (需要依賴一個介面,通過wsimport 產生的程式碼當中擷取...)
//建立調用webservice 服務的類...
ClientProxyFactoryBean bean=new ClientProxyFactoryBean();
bean.setAddress("http://192.168.9.100:8080/server");//設定訪問地址...
bean.setServiceClass(CxfWebServicePortType.class);//設定服務的介面...
//建立介面類型...
CxfWebServicePortType cxfWebServicePortType=(CxfWebServicePortType) bean.create();
cxfWebServicePortType.sayHello();
----------------------------------------------------------------------------------
使用cxf 提供類 JaxWsProxyFactoryBean 來調用 webservice 的服務端.......


JaxWsProxyFactoryBean 是  ClientProxyFactoryBean  的子類...
//建立調用服務的類...
JaxWsProxyFactoryBean bean=new JaxWsProxyFactoryBean();
//設定訪問地址
bean.setAddress("http://192.168.9.100:7418/userService");
//設定介面類型...
bean.setServiceClass(UserService.class);
UserService us=(UserService) bean.create();


String data=us.getUserById(1);


System.out.println(data);
---------------------------------------------
調用原則: 總結......
 服務端:       用戶端
 ServerFactoryBean ------------ClientProxyFactoryBean
 JaxWsServerFactoryBean----------------JaxWsProxyFactoryBean 
 JaxWsServerFactoryBean 發行就緒soap1.2 版本的協議....發布服務的時候,
 我們最好被發布的服務類要面向介面編程..


命令:wsdl2java  
      wsdl2java 是cxf 架構給我們提供的命令,這個命令的作用與wsimport 類似...

攔截器:
    cxf 架構中提供了攔截器的機制,我們可以通過攔截器擷取到用戶端與服務端進行互動的時候的資料格式
//建立發布服務的 類...
JaxWsServerFactoryBean  bean=new JaxWsServerFactoryBean();
//設定對外的訪問地址
bean.setAddress("http://192.168.9.100:7418/userService");
bean.setServiceClass(UserService.class);//設定介面類型...
bean.setServiceBean(new UserServiceImpl());//設定介面的實作類別...
//我們可以在發布服務的時候添加訊息攔截器
//攔截用戶端往服務端 發送的請求的訊息
bean.getInInterceptors().add(new LoggingInInterceptor());
//攔截服務端往用戶端返回的訊息...
bean.getOutInterceptors().add(new LoggingOutInterceptor());
bean.create();



【黑馬Android】(15)cxf 介紹 以及ant 工具的使用/cxf總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.