天天看點

在 Spring中內建CXF--->示範webservice服務的釋出及調用

一.前言:

CXF的使用:主要是完成傳統webService的增強

WebService主要作用:1.是服務的釋出        2.是使用服務完成用戶端。

架構和Spring內建的核心: 把架構的核心類(Service服務),交給Spring管理

二.功能實作:spring中內建CXF----->服務的釋出

  第一步:建立web項目

  第二步:搭建spring環境

    2.1 導spring 的jar包(本節隻導必須包/基礎包且為3.0版本)

com.springsource.net.sf.cglib-2.1.3

com.springsource.org.aopalliance-1.0.0

com.springsource.org.apache.commons.logging-1.1.1

com.springsource.org.aspectj.tools-1.6.6.RELEASE

commons-dbcp-1.3

commons-pool-1.5.6

org.springframework.aop-3.0.5.RELEASE

org.springframework.asm-3.0.5.RELEASE

org.springframework.beans-3.0.5.RELEASE

org.springframework.context.support-3.0.5.RELEASE

org.springframework.context-3.0.5.RELEASE

org.springframework.core-3.0.5.RELEASE

org.springframework.expression-3.0.5.RELEASE

org.springframework.jdbc-3.0.5.RELEASE

org.springframework.orm-3.0.5.RELEASE

org.springframework.transaction-3.0.5.RELEASE

org.springframework.web-3.0.5.RELEASE

-----------------CXF的核心Jar包-------------------------

asm-3.3.1.jar

commons-logging-1.1.1.jar

cxf-2.6.2.jar

neethi-3.0.2.jar

wsdl4j-1.6.2.jar

xmlschema-core-2.0.3.jar

 2.2準備配置檔案:applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
 <!-- myDate  用于測試 -->
 <bean class="java.util.Date" id="myDate"></bean>
 <!--導包  -->
 <import resource="applicationContext-service.xml"/>
</beans>
           

 2.3進行測試-----采用Junit3的測試----SpringTest

public class SpringTest {
 /**
  * 目的:1. 測試Date 可以擷取  2.判斷是否是單例的
  * 步驟:  junit3測試 不能使用注解
  *   1.通過main進行測試  public static void main(String[] args)
  *   2.擷取application
  */
 public static void main(String[] args) {
  //第一步  擷取上下午配置檔案
  ApplicationContext  aContext=new ClassPathXmlApplicationContext("applicationContext.xml");
  //第二步:通過上下午擷取beans
  Date bean = aContext.getBean(Date.class);
  //第三步;做測試
  System.out.println("bean="+bean);
  //第四步:測試 是不是 單例,思路通過 線程睡眠 測試是不是單例的
  try {
   Thread.sleep(1000);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  System.out.println("bean="+bean);
 }
}
           

第三步:讓spring在web中一啟動就初始化---->配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="WebApp_ID" version="3.0">
  <!-- web項目一啟動就初始化Spring 思路:通過配置Spring容器監聽器來做-->
  <listener id="contextLoaderListener" >
  <!--contextLoaderListener  會預設到web-inf  下面去找applicationContext.xml
  配置檔案,故而需要制定路徑:解決方案
  通過context-param标簽指定  -->
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置路徑 -->
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!--專門提供服務的servlet  -->
  <!--  內建CXFServlet  :完成後通過此位址在浏覽器通路:  http://localhost:8080/ws-->
  <servlet>
   <servlet-name>CXFServlet</servlet-name>
   <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>CXFServlet</servlet-name>
   <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>
</web-app>
           

第四步:建立cxf-servlet.xml檔案(位于:WEB-INF目錄下面)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jaxws="http://cxf.apache.org/jaxws"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://cxf.apache.org/jaxws
  http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 第二種方式:預設位址(常用) 
 cxf 的釋出條件(3個參數):1.位址   2.接口   3.實作類
 可增加攔截器
-->
  <jaxws:server id="hellowServer" address="/hi" >
     <jaxws:serviceBean>
     <!--引用已經存在的service  -->
      <ref bean="hellowServiceImpl"/>
  </jaxws:serviceBean>
  <!-- 添加攔截器:日志攔截器 -->
  <jaxws:inInterceptors>
   <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
  </jaxws:inInterceptors>
  <jaxws:outInterceptors>
   <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
  </jaxws:outInterceptors>
  </jaxws:server>
</beans>
           

--------------處于規範的要求-----------

單獨建立  applicationContext-service.xml檔案與applicationContext.xml同目錄

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="hellowServiceImpl" class="cn.gaoshan.springCXF.service.impl.HellowServiceImpl"/>
</beans>
           

第五步:在浏覽器中輸入:http://localhost:8080/ws  進行測試

三.功能實作:spring中內建CXF----->服務的釋出

     第一步:建立一個web項目      第二步:導入CXF的資源包(略)-----與上面的一緻           第三步:添加Spring配置檔案------applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<import resource="applicationContext-cxf.xml"/>
</beans>
           

    第四步:添加cxf配置檔案      

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
	<!--cxf  調用 用戶端:需要兩個參數
		address="http://192.168.1.101:8080/ws/hi"  ---服務端位址
		serviceClass="cn.gaoshan.springCXF.test.IHellowService"----服務接口
		最後調用一個create  ,生成一個代理,放到spring容器中
	  -->
	<jaxws:client id="hellowService" address="http://192.168.1.101:8080/ws/hi"
		serviceClass="cn.gaoshan.springCXF.test.IHellowService">
		
	</jaxws:client>
</beans>
           

   第五步:找到wsdl檔案位址,生成用戶端代碼        

在需要生成代碼的根目錄,執行指令:wsdl2java -p cn.gaoshan.springCXF.test  http://localhost:8080/ws/hi?wsdl

備注:易出現錯誤提示:

1.wsdl2java既不是内部指令也不是外部指令

    解決方案:重寫配置cxf的環境變量

        建立CXF_HOME      C:\ProgramFiles(my)\apache-cxf-2.7.2

        在CLASSPATH          %CSF_HOME%\lib

        在PATH中添加           %CSF_HOME%\bin

2.錯誤提示:WSDLToJava Error: http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl [15,19]: undefined element declaration 's:schema'......

   解決方案:檢查路徑:wsdl2java -p cn.gaoshan.springCXF.test  (此處要間隔兩個空格)http://localhost:8080/ws/hi?wsdl是否有錯,

http://localhost:8080/ws/hi?wsdl必須是目前能通路的位址

第六步:進行測試---->SpringTest(兩個版本)

xml版本

public class SpringTest {
	//調用用戶端服務之----------xml版本
	public static void main(String[] args) {
		//第一步  擷取上下午配置檔案
		ApplicationContext  aContext=new ClassPathXmlApplicationContext("applicationContext.xml");
		//第二步:通過上下午擷取本地接口的服務代理
		IHellowService hellowService = aContext.getBean(IHellowService.class);
		//第三步:調用服務
		System.out.println(hellowService.sayHellow("金順鵬"));
	}
}
           
注解版本
public class SpringTest {
	//調用用戶端服務之----------非xml版本
	public static void main(String[] args) {
		/**
		 * 流程:1.先建立JaxWsProxyFactoryBean  用于接收服務
		 * 		2.設定 服務的釋出位址
		 * 		3.設定 服務的釋出接口
		 * 		4.通過create  傳回服務接口的執行個體
		 */
		JaxWsProxyFactoryBean Bean = new JaxWsProxyFactoryBean();
		Bean.setAddress("http://192.168.1.101:8080/ws/hi");
		Bean.setServiceClass(IHellowService.class);
		IHellowService hellowService = (IHellowService) Bean.create();
		System.out.println(hellowService.sayHellow("王振濤"));
	}
}
           
以上!