WebService CXF學習(進階篇3):CXF整合Spring框架

 通過前面兩節的講解,相信你對CXF框架開始有一些認識了。在當今項目開發中,Spring框架基上都用到過,那麼它怎麼與CXF結合呢,這就是我們這一間要講的內容。好了,閒話少說。
    首先,在前面基礎上再導入幾個spring要用到的幾個.jar包:
               1、spring-asm.jar
               2、spring-beans.jar
               3、spring-context.jar
               4、spring-core.jar
               5、spring-expression.jar
               6、spring-aop.jar
               7、spring-web.jar
第一步:新建一個服務端web project,導入要用到的cxf和spring的.jar包,修改web.xml。配置如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<!-- Spring 容器加載的配置文件 設置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:/applicationContext-server.xml
		</param-value>
	</context-param>
	
         <!-- Spring 配置 -->
         <listener>
                  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
         </listener>
         <listener>
                  <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
         </listener>
	
	<!-- WebServices設置 -->
	<servlet>
		<servlet-name>CXFServices</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServices</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>
	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
第二步:新建一個接口類和接口實現類
package com.ms.services;

import java.util.List;
import javax.jws.WebService;
import com.ms.model.UserInfo;

@WebService
public interface IHelloServices {
	public String sayHello(String name);
	public String sayHelloToAll(List<UserInfo> users);
}
package com.ms.services.impl;

import java.util.List;
import javax.jws.WebService;
import com.ms.model.UserInfo;
import com.ms.services.IHelloServices;

@WebService(endpointInterface="com.ms.services.IHelloServices")
public class HelloServicesImpl implements IHelloServices {

	public String sayHello(String name) {
		return "Hello "+name+" .";
	}
	public String sayHelloToAll(List<UserInfo> users) {
		String hello = "hello ";
		for(UserInfo user:users){
			hello += user.getUserName()+" ,";
		}
		hello += " ,everybody.";
		return hello;
	}
}
第三步:新建一個spring Bean的xml文件,配置CXF webservices的服務
<?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-3.0.xsd  
        	http://cxf.apache.org/jaxws  
        	http://cxf.apache.org/schemas/jaxws.xsd">
	<!-- Import apache CXF bean definition 固定-->
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	
	<!-- services接口配置 -->
	<bean id="helloServicesBean" class="com.ms.services.impl.HelloServicesImpl" />
	<!-- CXF 配置WebServices的服務名及訪問地址 -->
	<jaxws:server id="helloServices" address="/HelloServices" 
			serviceClass="com.ms.services.IHelloServices">
			<jaxws:serviceBean>
				<ref bean="helloServicesBean"/>
			</jaxws:serviceBean>
	</jaxws:server>
</beans>
第四步:將工程部署到Tomcat中運行,在IE中輸入"http://localhost:8090/CxfServer_Spring/services",測試服務是否發佈成功
第五步:新建一個客戶端web project,導入要用到的cxf和spring的.jar包
第六步:將服務端的接口類及JavaBean對象類copy到客戶端工程中,且路徑要與服務端路徑一致
第七步:新建一個spring Bean的xml配置文件,配置CXF webservices的客戶端
<?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-3.0.xsd  
        	http://cxf.apache.org/jaxws  
        	http://cxf.apache.org/schemas/jaxws.xsd">
	<!-- Import apache CXF bean definition -->
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	
	<!-- CXF webservices 客戶端配置 -->
	<jaxws:client id="helloClient" serviceClass="com.ms.services.IHelloServices" 
			address="http://localhost:8090/CxfServer_Spring/services/HelloServices">
	</jaxws:client>
</beans>
第八步:新建一個測試類進行測試,代碼如下
package com.test;

import java.util.ArrayList;
import java.util.List;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ms.model.UserInfo;
import com.ms.services.IHelloServices;

public class Client {
	public static void main(String[] args) {
		invokeBySpring();
	}
	
	/**
	 * 通過Spring測試webservices
	 */
	public static void invokeBySpring(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");
		IHelloServices helloServices = context.getBean("helloClient",IHelloServices.class);
		
		List<UserInfo> users = new ArrayList<UserInfo>();
		users.add(new UserInfo("vicky",23));
		users.add(new UserInfo("caty",23));
		users.add(new UserInfo("ivy",23));
		users.add(new UserInfo("kelly",23));
		String helloAll = helloServices.sayHelloToAll(users);
		
		System.out.println(helloAll);
	}
	
	public static void invoke(){
		//創建WebService客戶端代理工廠   
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();   
		//註冊WebService接口   
		factory.setServiceClass(IHelloServices.class);   
		//設置WebService地址   
		factory.setAddress("http://localhost:8090/CxfServer_Spring/services/HelloServices");        
		IHelloServices helloServices = (IHelloServices)factory.create();   
		System.out.println("invoke helloServices webservice...");
		String hello = helloServices.sayHello("vicky");
		
		System.out.println(hello);
	}
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章