加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 23:31:15 所属栏目:安全 来源:网络整理
导读:?通过前面两节的讲解,相信你对CXF框架开始有一些认识了。在当今项目开发中,Spring框架基上都用到过,那么它怎么与CXF结合呢,这就是我们这一间要讲的内容。好了,闲话少说。? ??? 首先,在前面基础上再导入几个spring要用到的几个.jar包: ???????????????
?通过前面两节的讲解,相信你对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 ????????param-name>contextConfigLocation</>??
  9. ????????param-value ????????????classpath:/applicationContext-server.xml??
  10. ???? ??????
  11. ?????????<!--?Spring?配置?-->??
  12. ?????????listener ??????????????????listener-class>org.springframework.web.context.ContextLoaderListener ????????? ??????????????????>org.springframework.web.util.IntrospectorCleanupListener<!--?WebServices设置?-->??
  13. servletservlet-name>CXFServicesservlet-class>org.apache.cxf.transport.servlet.CXFServletload-on-startup>0servlet-mappingurl-pattern>/services/* ??????
  14. ??welcome-file-listwelcome-file>index.jspweb-app>??
第二步:新建一个接口类和接口实现类
[java]? copy
    package?com.ms.services;??
  1. ??
  2. import?java.util.List;??
  3. import?javax.jws.WebService;??
  4. import?com.ms.model.UserInfo;??
  5. @WebService??
  6. public?interface?IHelloServices?{??
  7. ????public?String?sayHello(String?name);??
  8. ????public?String?sayHelloToAll(List<UserInfo>?users);??
  9. }??
copy
    package?com.ms.services.impl;??
  1. import?com.ms.services.IHelloServices;??
  2. ??
  3. @WebService(endpointInterface="com.ms.services.IHelloServices")??
  4. class?HelloServicesImpl?implements?IHelloServices?{??
  5. public?String?sayHello(String?name)?{??
  6. ????????return?"Hello?"+name+"?.";??
  7. ????}??
  8. public?String?sayHelloToAll(List<UserInfo>?users)?{??
  9. ????????String?hello?=?"hello?";??
  10. for(UserInfo?user:users){??
  11. ????????????hello?+=?user.getUserName()+"?,";??
  12. ????????}??
  13. ????????hello?+=?"?,everybody.";??
  14. return?hello;??
  15. }??
第三步:新建一个spring Bean的xml文件,配置CXF webservices的服务
copy
    beans??xmlns="http://www.springframework.org/schema/beans"??
  1. ????????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  2. ????????xmlns:jaxws="http://cxf.apache.org/jaxws"??
  3. ????????xsi:schemaLocation="????
  4. ????????????http://www.springframework.org/schema/beans?????
  5. ????????????http://www.springframework.org/schema/beans/spring-beans-3.0.xsd????
  6. ????????????http://cxf.apache.org/jaxws????
  7. ????????????http://cxf.apache.org/schemas/jaxws.xsd" ????<!--?Import?apache?CXF?bean?definition?固定-->??
  8. import?resource="classpath:META-INF/cxf/cxf.xml"?/>??
  9. import?resource="classpath:META-INF/cxf/cxf-extension-soap.xml"?/>??
  10. import?resource="classpath:META-INF/cxf/cxf-servlet.xml"?<!--?services接口配置?-->??
  11. bean?id="helloServicesBean"?class="com.ms.services.impl.HelloServicesImpl"?<!--?CXF?配置WebServices的服务名及访问地址?-->??
  12. jaxws:server?id="helloServices"?address="/HelloServices"???
  13. ????????????serviceClass="com.ms.services.IHelloServices" ????????????jaxws:serviceBean ????????????????ref?bean="helloServicesBean"jaxws:serverbeans第四步:将工程部署到Tomcat中运行,在IE中输入" http://localhost:8090/CxfServer_Spring/services ",测试服务是否发布成功
    第五步:新建一个客户端web project,导入要用到的cxf和spring的.jar包
    第六步:将服务端的接口类及JavaBean对象类copy到客户端工程中,且路径要与服务端路径一致
    第七步:新建一个spring Bean的xml配置文件,配置CXF webservices的客户端
    copy
      <!--?Import?apache?CXF?bean?definition?-->??
    1. <!--?CXF?webservices?客户端配置?-->??
    2. jaxws:client?id="helloClient"?serviceClass="com.ms.services.IHelloServices"???
    3. ????????????address="http://localhost:8090/CxfServer_Spring/services/HelloServices"jaxws:client第八步:新建一个测试类进行测试,代码如下
      copy
        package?com.test;??
      1. import?java.util.ArrayList;??
      2. import?java.util.List;??
      3. import?org.apache.cxf.jaxws.JaxWsProxyFactoryBean;??
      4. import?org.springframework.context.ApplicationContext;??
      5. import?org.springframework.context.support.ClassPathXmlApplicationContext;??
      6. import?com.ms.model.UserInfo;??
      7. import?com.ms.services.IHelloServices;??
      8. class?Client?{??
      9. static?void?main(String[]?args)?{??
      10. ????????invokeBySpring();??
      11. ????}??
      12. ????/**?
      13. ?????*?通过Spring测试webservices?
      14. ?????*/??
      15. void?invokeBySpring(){??
      16. ????????ApplicationContext?context?=?new?ClassPathXmlApplicationContext("applicationContext-client.xml");??
      17. ????????IHelloServices?helloServices?=?context.getBean("helloClient",IHelloServices.class);??
      18. ??????????
      19. ????????List<UserInfo>?users?=?new?ArrayList<UserInfo>();??
      20. ????????users.add(new?UserInfo("vicky",23));??
      21. ????????users.add(new?UserInfo("caty",0); background-color:inherit">23));??
      22. new?UserInfo("ivy",153); background-color:inherit; font-weight:bold">new?UserInfo("kelly",248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????????String?helloAll?=?helloServices.sayHelloToAll(users);??
      23. ??????????
      24. ????????System.out.println(helloAll);??
      25. void?invoke(){??
      26. ????????//创建WebService客户端代理工厂?????
      27. ????????JaxWsProxyFactoryBean?factory?=?new?JaxWsProxyFactoryBean();?????
      28. //注册WebService接口?????
      29. ????????factory.setServiceClass(IHelloServices.class);?????
      30. //设置WebService地址?????
      31. ????????factory.setAddress("http://localhost:8090/CxfServer_Spring/services/HelloServices");??????????
      32. ????????IHelloServices?helloServices?=?(IHelloServices)factory.create();?????
      33. ????????System.out.println("invoke?helloServices?webservice...");??
      34. ????????String?hello?=?helloServices.sayHello("vicky");??
      35. ????????System.out.println(hello);??
      36. }??

      (编辑:李大同)

      【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读