WebService CXF学习(进阶篇3):CXF整合Spring框架

来源:互联网 发布:新款mac锁屏快捷键 编辑:程序博客网 时间:2024/05/16 18:52
 通过前面两节的讲解,相信你对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。配置如下
[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"   
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.     <!-- Spring 容器加载的配置文件 设置 -->  
  8.     <context-param>  
  9.         <param-name>contextConfigLocation</param-name>  
  10.         <param-value>  
  11.             classpath:/applicationContext-server.xml  
  12.         </param-value>  
  13.     </context-param>  
  14.       
  15.          <!-- Spring 配置 -->  
  16.          <listener>  
  17.                   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  18.          </listener>  
  19.          <listener>  
  20.                   <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
  21.          </listener>  
  22.       
  23.     <!-- WebServices设置 -->  
  24.     <servlet>  
  25.         <servlet-name>CXFServices</servlet-name>  
  26.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  27.         <load-on-startup>0</load-on-startup>  
  28.     </servlet>  
  29.     <servlet-mapping>  
  30.         <servlet-name>CXFServices</servlet-name>  
  31.         <url-pattern>/services/*</url-pattern>  
  32.     </servlet-mapping>  
  33.       
  34.   <welcome-file-list>  
  35.     <welcome-file>index.jsp</welcome-file>  
  36.   </welcome-file-list>  
  37. </web-app>  
第二步:新建一个接口类和接口实现类
[java] view plaincopy
  1. package com.ms.services;  
  2.   
  3. import java.util.List;  
  4. import javax.jws.WebService;  
  5. import com.ms.model.UserInfo;  
  6.   
  7. @WebService  
  8. public interface IHelloServices {  
  9.     public String sayHello(String name);  
  10.     public String sayHelloToAll(List<UserInfo> users);  
  11. }  
[java] view plaincopy
  1. package com.ms.services.impl;  
  2.   
  3. import java.util.List;  
  4. import javax.jws.WebService;  
  5. import com.ms.model.UserInfo;  
  6. import com.ms.services.IHelloServices;  
  7.   
  8. @WebService(endpointInterface="com.ms.services.IHelloServices")  
  9. public class HelloServicesImpl implements IHelloServices {  
  10.   
  11.     public String sayHello(String name) {  
  12.         return "Hello "+name+" .";  
  13.     }  
  14.     public String sayHelloToAll(List<UserInfo> users) {  
  15.         String hello = "hello ";  
  16.         for(UserInfo user:users){  
  17.             hello += user.getUserName()+" ,";  
  18.         }  
  19.         hello += " ,everybody.";  
  20.         return hello;  
  21.     }  
  22. }  
第三步:新建一个spring Bean的xml文件,配置CXF webservices的服务
[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  xmlns="http://www.springframework.org/schema/beans"  
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.         xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.         xsi:schemaLocation="    
  6.             http://www.springframework.org/schema/beans     
  7.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  8.             http://cxf.apache.org/jaxws    
  9.             http://cxf.apache.org/schemas/jaxws.xsd">  
  10.     <!-- Import apache CXF bean definition 固定-->  
  11.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  12.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  13.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  14.       
  15.     <!-- services接口配置 -->  
  16.     <bean id="helloServicesBean" class="com.ms.services.impl.HelloServicesImpl" />  
  17.     <!-- CXF 配置WebServices的服务名及访问地址 -->  
  18.     <jaxws:server id="helloServices" address="/HelloServices"   
  19.             serviceClass="com.ms.services.IHelloServices">  
  20.             <jaxws:serviceBean>  
  21.                 <ref bean="helloServicesBean"/>  
  22.             </jaxws:serviceBean>  
  23.     </jaxws:server>  
  24. </beans>  
第四步:将工程部署到Tomcat中运行,在IE中输入"http://localhost:8090/CxfServer_Spring/services",测试服务是否发布成功
第五步:新建一个客户端web project,导入要用到的cxf和spring的.jar包
第六步:将服务端的接口类及JavaBean对象类copy到客户端工程中,且路径要与服务端路径一致
第七步:新建一个spring Bean的xml配置文件,配置CXF webservices的客户端
[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  xmlns="http://www.springframework.org/schema/beans"  
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.         xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.         xsi:schemaLocation="    
  6.             http://www.springframework.org/schema/beans     
  7.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  8.             http://cxf.apache.org/jaxws    
  9.             http://cxf.apache.org/schemas/jaxws.xsd">  
  10.     <!-- Import apache CXF bean definition -->  
  11.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  12.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  13.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  14.       
  15.     <!-- CXF webservices 客户端配置 -->  
  16.     <jaxws:client id="helloClient" serviceClass="com.ms.services.IHelloServices"   
  17.             address="http://localhost:8090/CxfServer_Spring/services/HelloServices">  
  18.     </jaxws:client>  
  19. </beans>  
第八步:新建一个测试类进行测试,代码如下
[java] view plaincopy
  1. package com.test;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  6. import org.springframework.context.ApplicationContext;  
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  8. import com.ms.model.UserInfo;  
  9. import com.ms.services.IHelloServices;  
  10.   
  11. public class Client {  
  12.     public static void main(String[] args) {  
  13.         invokeBySpring();  
  14.     }  
  15.       
  16.     /** 
  17.      * 通过Spring测试webservices 
  18.      */  
  19.     public static void invokeBySpring(){  
  20.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");  
  21.         IHelloServices helloServices = context.getBean("helloClient",IHelloServices.class);  
  22.           
  23.         List<UserInfo> users = new ArrayList<UserInfo>();  
  24.         users.add(new UserInfo("vicky",23));  
  25.         users.add(new UserInfo("caty",23));  
  26.         users.add(new UserInfo("ivy",23));  
  27.         users.add(new UserInfo("kelly",23));  
  28.         String helloAll = helloServices.sayHelloToAll(users);  
  29.           
  30.         System.out.println(helloAll);  
  31.     }  
  32.       
  33.     public static void invoke(){  
  34.         //创建WebService客户端代理工厂     
  35.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();     
  36.         //注册WebService接口     
  37.         factory.setServiceClass(IHelloServices.class);     
  38.         //设置WebService地址     
  39.         factory.setAddress("http://localhost:8090/CxfServer_Spring/services/HelloServices");          
  40.         IHelloServices helloServices = (IHelloServices)factory.create();     
  41.         System.out.println("invoke helloServices webservice...");  
  42.         String hello = helloServices.sayHello("vicky");  
  43.           
  44.         System.out.println(hello);  
  45.     }  
  46. }  
0 0
原创粉丝点击