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。配置如下
[html]  view plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
antlr-2.7.7.jar aopalliance-1.0.jar asm-3.3.1.jar commons-beanutils-1.8.3.jar commons-codec-1.4.jar commons-collections-3.2.1.jar commons-dbutils-1.5.jar commons-httpclient.jar commons-lang-2.6.jar commons-lang3-3.0.1.jar commons-logging-1.1.1.jar cxf-2.6.3.jar cxf-manifest.jar cxf-services-sts-core-2.6.3.jar cxf-services-wsn-api-2.6.3.jar cxf-services-wsn-core-2.6.3.jar cxf-xjc-boolean-2.6.0.jar cxf-xjc-bug671-2.6.0.jar cxf-xjc-dv-2.6.0.jar cxf-xjc-runtime-2.6.0.jar cxf-xjc-ts-2.6.0.jar ehcache-core-2.5.1.jar FastInfoset-1.2.12.jar geronimo-activation_1.1_spec-1.1.jar geronimo-annotation_1.0_spec-1.1.1.jar geronimo-javamail_1.4_spec-1.7.1.jar geronimo-jaxws_2.2_spec-1.1.jar geronimo-jms_1.1_spec-1.1.1.jar geronimo-servlet_2.5_spec-1.1.2.jar geronimo-stax-api_1.0_spec-1.0.1.jar geronimo-ws-metadata_2.0_spec-1.1.3.jar h2-1.3.169.jar isorelax-20030108.jar jaxb-api-2.2.6.jar jaxb-impl-2.2.5.jar jaxb-xjc-2.2.5.jar jettison-1.3.2.jar jetty-continuation-7.5.4.v20111024.jar jetty-http-7.5.4.v20111024.jar jetty-io-7.5.4.v20111024.jar jetty-security-7.5.4.v20111024.jar jetty-server-7.5.4.v20111024.jar jetty-util-7.5.4.v20111024.jar joda-time-1.6.2.jar js-1.7R2.jar json-lib-2.4-jdk15.jar jsr311-api-1.1.1.jar mimepull-1.7.jar msv-core-2011.1.jar neethi-3.0.2.jar oauth-20100527.jar oauth-provider-20100527.jar opensaml-2.5.1-1.jar openws-1.4.2-1.jar relaxngDatatype-20020414.jar saaj-api-1.3.4.jar saaj-impl-1.3.19.jar serializer-2.7.1.jar slf4j-api-1.6.2.jar slf4j-jdk14-1.6.2.jar spring-aop-3.0.7.RELEASE.jar spring-asm-3.0.7.RELEASE.jar spring-beans-3.0.7.RELEASE.jar spring-context-3.0.7.RELEASE.jar spring-core-3.0.7.RELEASE.jar spring-expression-3.0.7.RELEASE.jar spring-jms-3.0.7.RELEASE.jar spring-tx-3.0.7.RELEASE.jar spring-web-3.0.7.RELEASE.jar stax2-api-3.1.1.jar velocity-1.7.jar woodstox-core-asl-4.1.4.jar wsdl4j-1.6.2.jar wss4j-1.6.7.jar xalan-2.7.1.jar xml-resolver-1.2.jar xmlbeans-2.5.0.jar xmlschema-core-2.0.3.jar xmlsec-1.5.2.jar xmltooling-1.3.2-1.jar xsdlib-2010.1.ja

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值