Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 1183|回复: 0

Spring和Apache CXF创建WebService解决方案

  [复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2013-1-14 16:46:31 | 显示全部楼层 |阅读模式
    很多时候,我们需要创建WebService供外部系统调用。这个时候一种好的WebService解决方案非常重要。下面介绍Spring结合CXF创建WebService的步骤以及如何创建客户端测试。
    第一步,需要引用相关包,这里已经将所有需要引用的包结构打包了,如下:
    所需导入的包下载:
    文件名:Spring和Apache CXF创建WebService解决方案[包结构].rar
    下载地址:http://www.javaxxz.com/file.php?id=15524516
    第二步,在web.xml中配置Spring和CXFServlet如下:
    1. //从WEB-INF路径下加载Spring配置文件。
    2. <context-param>
    3.     <param-name>contextConfigLocation</param-name>
    4.     <param-value>/WEB-INF/springcfg/applicationContext.xml</param-value>
    5. </context-param>

    6. //负责启动Spring容器的监听器,它将引用上方的contextConfigLocation参数获得Spring配置文件地址。
    7. <listener>
    8.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    9.   </listener>
    10.   <listener>
    11.     <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    12. </listener>

    13.   //配置CXF
    14.   <servlet>
    15.     <servlet-name>CXFServlet</servlet-name>
    16.     <servlet-class>      
    17.             org.apache.cxf.transport.servlet.CXFServlet      
    18.         </servlet-class>
    19.     <load-on-startup>1</load-on-startup>
    20.   </servlet>
    21.   <servlet-mapping>
    22.     <servlet-name>CXFServlet</servlet-name>
    23.     <url-pattern>/*</url-pattern>
    24.   </servlet-mapping>
    复制代码
    第三步:创建dao,service,action三层架构中类,并配置依赖关系
    比如:CustomerDao,CustomerService,CustomerAction以及它们的实现类。
    其中CustomerAction接口类用于发布,所以在类前声明:@WebService。CXF便知道这个类为Service服务类。
    在Spring配置文件中,配置三层架构的依赖关系。

    第四步:在Spring配置文件中配置CXF WebService服务。
    配置文件spring-webservice.xml如下:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans
    3. xmlns="http://www.springframework.org/schema/beans"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:context="http://www.springframework.org/schema/context"
    6. xmlns:aop="http://www.springframework.org/schema/aop"
    7. xmlns:tx="http://www.springframework.org/schema/tx"
    8. xmlns:jaxws="http://cxf.apache.org/jaxws"
    9. xmlns:cxf="http://cxf.apache.org/core"
    10. xsi:schemaLocation="http://www.springframework.org/schema/beans
    11.                     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    12.                     http://www.springframework.org/schema/context
    13.                     http://www.springframework.org/schema/context/spring-context-3.0.xsd
    14.                     http://www.springframework.org/schema/aop
    15.                     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    16.                     http://www.springframework.org/schema/tx
    17.                     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    18.                     http://cxf.apache.org/jaxws
    19.                     http://cxf.apache.org/schemas/jaxws.xsd
    20.                     http://cxf.apache.org/core
    21.                     http://cxf.apache.org/schemas/core.xsd">
    22.                     
    23.     //引入CXF默认配置               
    24.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
    25.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
    26.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    27.    //IP限制访问拦截器
    28.     <bean id="accessInterceptor"  class="****.AccessInterceptor"/>
    29.     //IP限制访问拦截器配置
    30.     <cxf:bus>  
    31.         <cxf:inInterceptors>  
    32.             <ref bean="accessInterceptor" />  
    33.         </cxf:inInterceptors>  
    34.     </cxf:bus>  
    35.     //配置Customer Service类
    36.     <bean id="customerAction"  class="****.CustomerActionImpl"   scope="prototype">
    37.         <property name="customerService" ref="customerService"/>
    38.     </bean>

    39.    //发布Customer服务配置
    40.     <jaxws:endpoint id="customer" implementor="#customerAction" address="/customer" publish="true" />
    41. </beans>
    复制代码
    至此解决方案完成!直接启动tomcat便可访问WebService接口。

    回复

    使用道具 举报

    该用户从未签到

    发表于 2013-1-14 16:48:13 | 显示全部楼层
    谢谢楼主总结的很好。
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-6-17 04:55 , Processed in 0.355902 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表