spring整合cxf架構,springcxf架構
CXF是webService的架構,能夠和spring無縫整合
##服務端編寫
1.建立動態web項目
2.匯入cxf和spring相關jar包(CXF核心包:cxf-2.4.2.jar)
3.在web.xml中配置CXF架構的核心Servlet
1 <servlet> 2 <servlet-name>cxf</servlet-name> 3 <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 4 <init-param> 5 <param-name>config-location</param-name> 6 <param-value>classpath:applicationContext.xml</param-value> 7 </init-param> 8 </servlet> 9 <servlet-mapping>10 <servlet-name>cxf</servlet-name>11 <url-pattern>/webservice/*</url-pattern>12 </servlet-mapping>
4.提供spring架構的設定檔applicationContext.xml
applicationContext.xml的約束:
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"xmlns:soap="http://cxf.apache.org/bindings/soap"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
5.開發一個服務類
注:服務類必須加註解 @WebService
6.在spring中設定檔中註冊服務
<jaxws:endpoint id="" address="/hello" implementor=""></jaxws:endpoint><!-- id為服務的id,任意填寫 address 為訪問地址 implementor為服務類的全類名-->
啟動web工程,瀏覽器訪問
###用戶端
(用wsdl2java命令產生本地代碼調用)
1,在wsdl2java.bat命令所在的檔案夾下開啟命令視窗,輸入:wsdl2java -d . 路徑
(路徑為service發布後頁面的wsdl的全路徑,service訪問的路徑名加?wsdl),斷行符號後會在當前檔案夾下組建檔案夾
2.把檔案夾複製到項目中
(用spring檔案註冊代理對象調用)
1.建立項目,可以不是web項目,匯入jar包
2.將產生的介面複製到項目中,
3.建立applicationContext.xml檔案中配置代理對象
<jaxws:client id="" address = "" serviceClass =""></jaxws:client><!-- id值隨意, adress的值為wsdl的路徑值,當不在本機是,須要修改ip serviceClass為介面的全路徑-->
4.編寫實作類別(如下為例子)
public static void main(String[] args) { //建立工廠對象 ClassPathXmlApplicationContext cts = new ClassPathXmlApplicationContext("applicationContext.xml"); Fun1 proxy = (Fun1) cts.getBean("myclient"); String string = proxy.sayHello("呵呵", 12); System.out.println(string); }