博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CXF之四 cxf集成Spring
阅读量:7158 次
发布时间:2019-06-29

本文共 4892 字,大约阅读时间需要 16 分钟。

CXF原生支持,可以和Spring无缝集成。通过Spring Web实现CXFServlet。下面将Spring和CXF集成在一起,CXF发布的WebService可以调用Spring的Bean。 

创建Maven Web项目,在pom.xml中添加CXF和Spring的引用,由于该Web项目中不涉及,没有添加Spring JDBC、Spring ORM等数据库相关模块。

4.0.0
study
cxfspringdemo1
0.0.1-SNAPSHOT
war
cxfspringdemo1
http://maven.apache.org
3.1.1
4.1.7.RELEASE
org.springframework
spring-aop
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.apache.cxf
cxf-rt-frontend-jaxws
${cxf.version}
org.apache.cxf
cxf-rt-transports-http
${cxf.version}

在项目中添加Dao接口及实现类,WebService调用Dao层数据,项目结构如图所示。 

HelloDao接口,提供welcome方法。

package com.study.cxfspringdemo1.dao;public interface HelloDao {    public String welcome(String name);}

HelloDao接口实现类HelloDaoImpl.java

package com.study.cxfspringdemo1.dao.impl;import org.springframework.stereotype.Repository;import com.study.cxfspringdemo1.dao.HelloDao;@Repository("helloDao")public class HelloDaoImpl implements HelloDao {    public String welcome(String name) {        return "欢迎使用CXF:" + name;    }}

HelloWS接口,由于集成了Spring,该接口既是WebService,也是Spring Service。

package com.study.cxfspringdemo1.ws;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebService;@WebService(        name = "HelloWS",        targetNamespace = "http://www.hellocxfspring.com/services/hello"    )public interface HelloWS {    @WebMethod    public String welcome(@WebParam(name = "name") String name);}

HelloWS接口实现类HelloWSImpl,实现类调用HelloDaoImpl的方法。

package com.study.cxfspringdemo1.ws.impl;import javax.annotation.Resource;import javax.jws.WebService;import org.springframework.stereotype.Service;import com.study.cxfspringdemo1.dao.HelloDao;import com.study.cxfspringdemo1.ws.HelloWS;@WebService(        endpointInterface = "com.study.cxfspringdemo1.ws.HelloWS",        portName = "HelloWSPort",        serviceName = "HelloWSService",        targetNamespace = "http://www.hellocxfspring.com/services/hello"    )@Service("helloWS")public class HelloWSImpl implements HelloWS {    @Resource    private HelloDao helloDao;    public String welcome(String name) {        return helloDao.welcome(name);    }}

Spring上下文配置beans.xml

在WEB-INF下创建cxf-servlet.xml,内容如下

在web.xml中添加Spring上下文Listener以及CXFServlet

cxfserver
contextConfigLocation
classpath:beans.xml
org.springframework.web.context.ContextLoaderListener
cxfservlet
org.apache.cxf.transport.servlet.CXFServlet
1
cxfservlet
/services/*
index.html
index.jsp

工程结构:

客户端

wsimport -encoding utf-8 -d D:\dev\wsdl\cxfspringdemo1\wsimport\compile -s D:\dev\wsdl\cxfspringdemo1\wsimport\src -p com.study.cxfspringdemo1client http://localhost:8080/cxfspringdemo1-0.0.1-SNAPSHOT/services/hello?wsdl

将源码拷贝到maven工程中

 

 

转载地址:http://xhhgl.baihongyu.com/

你可能感兴趣的文章
如何拿到国内IT巨头的Offer
查看>>
CentOS 7.3 配置postfix并发送邮件
查看>>
border和background
查看>>
TCP/IP之(三)四次挥手
查看>>
设计模式之工厂模式(三)
查看>>
【木叶精品系统】木叶 GhostXP SP3 纯净版/装机版_2013.06
查看>>
spring标签的使用
查看>>
Record log while in programing
查看>>
使用python开发app后台,xml和json的区别
查看>>
Bio服务端两个实例
查看>>
PHP Warning: PHP Startup: unable to load dynamic library
查看>>
刀片(sda)Ubuntu preseed配置文件示例
查看>>
HAProxy同时对80和443做负载均衡
查看>>
关闭SELINUX
查看>>
9种用户体验设计的状态是必须知道的(三)
查看>>
关于双网卡绑定与端口聚合
查看>>
Mandriva基础知识之一:改主机名称
查看>>
OpenStack 学习笔记(四):OpenStack glance服务搭建
查看>>
Python模拟新浪微博登录
查看>>
zabbix 代理(agent)端详细安装配置一
查看>>