链接地址:http://zhengshuo.iteye.com/blog/2154047

废话不说,直接上代码 
新增cxf的plugin 
CXFPlugin

  1. package com.jfinal.plugin.spring;
  2. import org.apache.cxf.Bus;
  3. import org.apache.cxf.bus.spring.SpringBusFactory;
  4. import org.apache.cxf.transport.servlet.ServletTransportFactory;
  5. import org.springframework.context.ApplicationContext;
  6. import com.jfinal.plugin.IPlugin;
  7. public class CXFPlugin implements IPlugin{
  8. private ApplicationContext ctx;
  9. SpringBusFactory busFactory;
  10. ServletTransportFactory transportFac;
  11. public boolean start() {
  12. if (ctx == null){
  13. ctx = IocInterceptor.ctx;
  14. }
  15. busFactory = new SpringBusFactory(ctx);
  16. Bus bus = busFactory.createBus();
  17. transportFac = new ServletTransportFactory(bus);
  18. CXFContext.cxf = new CXFContext(ctx,busFactory,transportFac);
  19. return true;
  20. }
  21. public boolean stop() {
  22. return true;
  23. }
  24. }

因为要用到spring的context,所以包名就放在spring同目录了 
新增CXFcontent

  1. package com.jfinal.plugin.spring;
  2. import org.apache.cxf.bus.spring.SpringBusFactory;
  3. import org.apache.cxf.transport.servlet.ServletTransportFactory;
  4. import org.springframework.context.ApplicationContext;
  5. public class CXFContext{
  6. private ApplicationContext ctx;
  7. private SpringBusFactory busFactory;
  8. private ServletTransportFactory transportFac;
  9. static CXFContext cxf;
  10. public static CXFContext getCXFContent(){
  11. return cxf;
  12. }
  13. CXFContext(ApplicationContext ctx,SpringBusFactory busFactory,ServletTransportFactory transportFac){
  14. this.ctx = ctx;
  15. this.busFactory = busFactory;
  16. this.transportFac = transportFac;
  17. }
  18. public ApplicationContext getCtx() {
  19. return ctx;
  20. }
  21. public void setCtx(ApplicationContext ctx) {
  22. this.ctx = ctx;
  23. }
  24. public SpringBusFactory getBusFactory() {
  25. return busFactory;
  26. }
  27. public void setBusFactory(SpringBusFactory busFactory) {
  28. this.busFactory = busFactory;
  29. }
  30. public ServletTransportFactory getTransportFac() {
  31. return transportFac;
  32. }
  33. public void setTransportFac(ServletTransportFactory transportFac) {
  34. this.transportFac = transportFac;
  35. }
  36. public CXFContext getCxf() {
  37. return cxf;
  38. }
  39. public void setCxf(CXFContext cxf) {
  40. this.cxf = cxf;
  41. }
  42. }

新增servlet一个,用于替换CXFServlet

  1. package cn.edu.jxut.common.config;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import javax.servlet.ServletConfig;
  5. import javax.servlet.ServletContext;
  6. import javax.servlet.ServletException;
  7. import org.apache.cxf.bus.spring.BusApplicationContext;
  8. import org.apache.cxf.bus.spring.SpringBusFactory;
  9. import org.apache.cxf.common.classloader.ClassLoaderUtils;
  10. import org.apache.cxf.resource.ResourceManager;
  11. import org.apache.cxf.resource.URIResolver;
  12. import org.apache.cxf.transport.servlet.AbstractCXFServlet;
  13. import org.apache.cxf.transport.servlet.ServletContextResourceResolver;
  14. import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
  15. import org.springframework.context.ApplicationContext;
  16. import org.springframework.context.support.GenericApplicationContext;
  17. import org.springframework.core.io.InputStreamResource;
  18. import com.jfinal.plugin.spring.CXFContext;
  19. public class WsController extends AbstractCXFServlet{
  20. private GenericApplicationContext childCtx;
  21. private boolean inRefresh;
  22. public void loadBus(ServletConfig servletConfig) throws ServletException {
  23. String springCls = "org.springframework.context.ApplicationContext";
  24. try {
  25. ClassLoaderUtils.loadClass(springCls, getClass());
  26. loadSpringBus(servletConfig);
  27. } catch (ClassNotFoundException e) {
  28. throw new ServletException("Can't load bus with Spring context class", e);
  29. }
  30. }
  31. private void loadSpringBus(ServletConfig servletConfig)
  32. throws ServletException
  33. {
  34. ServletContext svCtx = getServletContext();
  35. ApplicationContext ctx = CXFContext.getCXFContent().getCtx();
  36. if (ctx == null) {
  37. Object ctxObject = svCtx.getAttribute("org.springframework.web.context.WebApplicationContext.ROOT");
  38. if ((ctxObject instanceof ApplicationContext)) {
  39. ctx = (ApplicationContext)ctxObject;
  40. } else if (ctxObject != null)
  41. {
  42. Exception ex = (Exception)ctxObject;
  43. throw new ServletException(ex);
  44. }
  45. }
  46. updateContext(servletConfig, ctx);
  47. }
  48. private void updateContext(ServletConfig servletConfig, ApplicationContext ctx)
  49. {
  50. if (ctx == null) {
  51. this.bus = new SpringBusFactory().createBus();
  52. ctx = (ApplicationContext)this.bus.getExtension(BusApplicationContext.class);
  53. } else {
  54. this.bus = CXFContext.getCXFContent().getBusFactory().createBus();
  55. }
  56. ResourceManager resourceManager = (ResourceManager)this.bus.getExtension(ResourceManager.class);
  57. resourceManager.addResourceResolver(new ServletContextResourceResolver(servletConfig.getServletContext()));
  58. replaceDestinationFactory();
  59. this.controller = createServletController(servletConfig);
  60. loadAdditionalConfig(ctx, servletConfig);
  61. }
  62. private void loadAdditionalConfig(ApplicationContext ctx, ServletConfig servletConfig)
  63. {
  64. String location = servletConfig.getInitParameter("config-location");
  65. if (location == null) {
  66. location = "/META-INF/cxf/cxf-servlet.xml";
  67. }
  68. InputStream is = null;
  69. try {
  70. is = servletConfig.getServletContext().getResourceAsStream(location);
  71. if ((is == null) || (is.available() == -1)) {
  72. URIResolver resolver = new URIResolver(location);
  73. if (resolver.isResolved()) {
  74. is = resolver.getInputStream();
  75. }
  76. }
  77. }
  78. catch (IOException e)
  79. {
  80. }
  81. if (is != null) {
  82. this.childCtx = new GenericApplicationContext(ctx);
  83. XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.childCtx);
  84. reader.setValidationMode(3);
  85. reader.loadBeanDefinitions(new InputStreamResource(is, location));
  86. this.childCtx.refresh();
  87. }
  88. }
  89. }

web.xml配置:

  1. <servlet>
  2. <servlet-name>CXFServlet</servlet-name>
  3. <servlet-class>
  4. [color=red]cn.edu.jxut.common.config.WsController  [/color]        </servlet-class>
  5. <load-on-startup>1</load-on-startup>
  6. </servlet>
  7. <servlet-mapping>
  8. <servlet-name>CXFServlet</servlet-name>
  9. <url-pattern>/service/*</url-pattern>
  10. </servlet-mapping>

jfinal配置:

  1. public void configHandler(Handlers me) {
  2. me.add(new UrlSkipHandler(".*/service.*",false));
  3. }

此服务已发布可用,在webservice实现类中直接使用jfinal的db获取数据或保存数据都非常方便。

jfinal集成spring cxf做webservice服务的更多相关文章

  1. Eclipse+Maven+Spring+CXF 构建webservice 服务

    一. 软件准备 Eclipse 4.2.1 Maven 2.2.1 Spring 3.2.6 CXF 3.0.2 二. 步骤 首先,在Eclipse中用maven构建一个quickstart版本的ma ...

  2. 搭建web项目结合spring+cxf的webservice服务

    服务端: 服务端和客户端都需要引入包 antlr-2.7.7.jar aopalliance-1.0.jar asm-3.3.jar commons-collections-3.2.1.jar com ...

  3. Spring Boot 使用 CXF 调用 WebService 服务

    上一张我们讲到 Spring Boot 开发 WebService 服务,本章研究基于 CXF 调用 WebService.另外本来想写一篇 xfire 作为 client 端来调用 webservi ...

  4. 使用 CXF 做 webservice 简单例子(转载)

    使用 CXF 做 webservice 简单例子     Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这 ...

  5. CXF发布webService服务以及客户端调用

    这篇随笔内容是CXF发布webService服务以及客户端调用的方法 CXF是什么? 开发工作之前需要下载CXF和安装 下载地址:http://cxf.apache.org 安装过程: <1&g ...

  6. 【转】构建基于CXF的WebService服务

    构建基于CXF的WebService服务 Apache CXF = Celtix+ XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.C ...

  7. idea+maven+spring+cxf创建webservice应用(二)生成客户端程序

    idea+maven+spring+cxf创建webservice应用(二)生成客户端程序,以上一篇为基础"idea+maven+spring+cxf创建webservice应用" ...

  8. Spring Boot 开发 WebService 服务

    WebService 虽然现在大部分互联网企业不太提倡使用,但在以第三方接口为主导的市场,对方来什么接口你还得用什么接口,不可能把接口重写了.例如大部分传统的大型企业都在用 WebService,并且 ...

  9. Spring Boot+CXF搭建WebService服务参考资料

    pom.xml文件引入包: <!--WerbService CXF依赖--> <dependency> <groupId>org.apache.cxf</gr ...

随机推荐

  1. 74HC595的中文资料

    74HC595--具有三态输出锁存功能的8位串行输入.串行/并行输出移位寄存器 本文翻译自NXP的74HC595的datasheet 74HC595和74HCT595是带有存储寄存器和三态输出的8位串 ...

  2. SQL SERVER 2005 同步复制技术(转)

    SQL SERVER 2005 同步复制技术 以下实现复制步骤(以快照复制为例) 运行平台SQL SERVER 2005 一.准备工作: 1.建立一个 WINDOWS 用户,设置为管理员权限,并设置密 ...

  3. Leetcode解题记录

    尽量抽空刷LeetCode,持续更新 刷题记录在github上面,https://github.com/Zering/LeetCode 2016-09-05 300. Longest Increasi ...

  4. [Swust OJ 771]--奶牛农场(几何题,画图就好)

    题目链接:http://acm.swust.edu.cn/problem/771/    Description 将军有一个用栅栏围成的矩形农场和一只奶牛,在农场的一个角落放有一只矩形的箱子,有一天将 ...

  5. BZOJ 1620: [Usaco2008 Nov]Time Management 时间管理( 二分答案 )

    二分一下答案就好了... --------------------------------------------------------------------------------------- ...

  6. Spring AOP基于xml配置实例

    SpringAOP里的几个术语,什么切面,切点之类的,官方的说明太抽象.为了更好地理解记忆,这里几下我自己的通俗的理解. 切面:就是日记类,什么前置通知后置通知(这些都是所谓的Advice)的具体方法 ...

  7. Sublime Text 2使用技巧汇总

    一.下载链接: Windows-64bit: http://pan.baidu.com/s/1o6QdKYu 其它版本请移步官网: http://www.sublimetext.com/ 二.破解Li ...

  8. codeforces 620E. New Year Tree dfs序+线段树+bitset

    题目链接 给一棵树, 每个节点有颜色, 两种操作, 一种是将一个节点的子树全都染色成c, 一种是查询一个节点的子树有多少个不同的颜色, c<=60. 每个节点一个bitset维护就可以. #in ...

  9. C语言之三大查找算法

    查找算法 1.二分查找 二分查找就是折半查找,其基本思想是:首先选取表中间位置的记录,将其关键字与给定关键字key进行比较,若相等,则查找成功.若key值比该关键字值大,则要找的元素一定在右子表中,则 ...

  10. BZOJ 1708: [Usaco2007 Oct]Money奶牛的硬币

    1708: [Usaco2007 Oct]Money奶牛的硬币 Description 在创立了她们自己的政权之后,奶牛们决定推广新的货币系统.在强烈的叛逆心理的驱使下,她们准备使用奇怪的面值.在传统 ...