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入门到精通教程
查看: 1081|回复: 0

[JavaEE] Spring和Apache CXF创建WebService解决方案

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

    [LV.4]偶尔看看III

    发表于 2013-12-22 15:04:02 | 显示全部楼层 |阅读模式
    很多时候,我们需要创建WebService供外部系统调用。这个时候一种好的WebService解决方案非常重要。下面介绍Spring结合CXF创建WebService的步骤以及如何创建客户端测试。
    第一步,需要引用相关包,这里已经将所有需要引用的包结构打包了,如下:
    所需导入的包下载:http://www.javaxxz.com/file.php?id=15524516
    第二步,在web.xml中配置Spring和CXFServlet如下:
    //从WEB-INF路径下加载Spring配置文件。
    <context-param>
    < param-name>contextConfigLocation</param-name>
    < param-value>/WEB-INF/springcfg/applicationContext.xml</param-value>
    < /context-param>
    //负责启动Spring容器的监听器,它将引用上方的contextConfigLocation参数获得Spring配置文件地址。
    <listener>
    < listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    < /listener>
    < listener>
    < listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    < /listener>
    //配置CXF
    < servlet>
    < servlet-name>CXFServlet</servlet-name>
    < servlet-class>
    org.apache.cxf.transport.servlet.CXFServlet
    < /servlet-class>
    < load-on-startup>1</load-on-startup>
    < /servlet>
    < servlet-mapping>
    < servlet-name>CXFServlet</servlet-name>
    < url-pattern>/*</url-pattern>
    < /servlet-mapping>
    第三步:创建dao,service,action三层架构中类,并配置依赖关系
    比如:CustomerDao,CustomerService,CustomerAction以及它们的实现类。
    其中CustomerAction接口类用于发布,所以在类前声明:@WebService。CXF便知道这个类为Service服务类。
    在Spring配置文件中,配置三层架构的依赖关系。
    第四步:在Spring配置文件中配置CXF WebService服务。
    配置文件spring-webservice.xml如下:
    <?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:context=”http://www.springframework.org/schema/context
    xmlns:aop=”http://www.springframework.org/schema/aop
    xmlns:tx=”http://www.springframework.org/schema/tx
    xmlns:jaxws=”http://cxf.apache.org/jaxws
    xmlns:cxf=”http://cxf.apache.org/core
    xsi:schemaLocation=”http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/s ... ing-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://cxf.apache.org/jaxws
    http://cxf.apache.org/schemas/jaxws.xsd
    http://cxf.apache.org/core
    http://cxf.apache.org/schemas/core.xsd”>
    //引入CXF默认配置
    <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” />
    //IP限制访问拦截器
    <bean id=”accessInterceptor”  class=”****.AccessInterceptor”/>
    //IP限制访问拦截器配置
    <cxf:bus>
    < cxf:inInterceptors>
    < ref bean=”accessInterceptor” />
    < /cxf:inInterceptors>
    < /cxf:bus>
    //配置Customer Service类
    <bean id=”customerAction”  class=”****.CustomerActionImpl”   scope=”prototype”>
    < property name=”customerService” ref=”customerService”/>
    < /bean>
    //发布Customer服务配置
    <jaxws:endpoint id=”customer” implementor=”#customerAction” address=”/customer” publish=”true” />
    < /beans>
    至此解决方案完成!直接启动tomcat便可访问WebService接口。
    回复

    使用道具 举报

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

    本版积分规则

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

    GMT+8, 2024-3-29 13:37 , Processed in 0.346060 second(s), 47 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

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