jfinal集成spring cxf做webservice服务
链接地址:http://zhengshuo.iteye.com/blog/2154047
废话不说,直接上代码
新增cxf的plugin
CXFPlugin
- package com.jfinal.plugin.spring;
- import org.apache.cxf.Bus;
- import org.apache.cxf.bus.spring.SpringBusFactory;
- import org.apache.cxf.transport.servlet.ServletTransportFactory;
- import org.springframework.context.ApplicationContext;
- import com.jfinal.plugin.IPlugin;
- public class CXFPlugin implements IPlugin{
- private ApplicationContext ctx;
- SpringBusFactory busFactory;
- ServletTransportFactory transportFac;
- public boolean start() {
- if (ctx == null){
- ctx = IocInterceptor.ctx;
- }
- busFactory = new SpringBusFactory(ctx);
- Bus bus = busFactory.createBus();
- transportFac = new ServletTransportFactory(bus);
- CXFContext.cxf = new CXFContext(ctx,busFactory,transportFac);
- return true;
- }
- public boolean stop() {
- return true;
- }
- }
因为要用到spring的context,所以包名就放在spring同目录了
新增CXFcontent
- package com.jfinal.plugin.spring;
- import org.apache.cxf.bus.spring.SpringBusFactory;
- import org.apache.cxf.transport.servlet.ServletTransportFactory;
- import org.springframework.context.ApplicationContext;
- public class CXFContext{
- private ApplicationContext ctx;
- private SpringBusFactory busFactory;
- private ServletTransportFactory transportFac;
- static CXFContext cxf;
- public static CXFContext getCXFContent(){
- return cxf;
- }
- CXFContext(ApplicationContext ctx,SpringBusFactory busFactory,ServletTransportFactory transportFac){
- this.ctx = ctx;
- this.busFactory = busFactory;
- this.transportFac = transportFac;
- }
- public ApplicationContext getCtx() {
- return ctx;
- }
- public void setCtx(ApplicationContext ctx) {
- this.ctx = ctx;
- }
- public SpringBusFactory getBusFactory() {
- return busFactory;
- }
- public void setBusFactory(SpringBusFactory busFactory) {
- this.busFactory = busFactory;
- }
- public ServletTransportFactory getTransportFac() {
- return transportFac;
- }
- public void setTransportFac(ServletTransportFactory transportFac) {
- this.transportFac = transportFac;
- }
- public CXFContext getCxf() {
- return cxf;
- }
- public void setCxf(CXFContext cxf) {
- this.cxf = cxf;
- }
- }
新增servlet一个,用于替换CXFServlet
- package cn.edu.jxut.common.config;
- import java.io.IOException;
- import java.io.InputStream;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import org.apache.cxf.bus.spring.BusApplicationContext;
- import org.apache.cxf.bus.spring.SpringBusFactory;
- import org.apache.cxf.common.classloader.ClassLoaderUtils;
- import org.apache.cxf.resource.ResourceManager;
- import org.apache.cxf.resource.URIResolver;
- import org.apache.cxf.transport.servlet.AbstractCXFServlet;
- import org.apache.cxf.transport.servlet.ServletContextResourceResolver;
- import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.GenericApplicationContext;
- import org.springframework.core.io.InputStreamResource;
- import com.jfinal.plugin.spring.CXFContext;
- public class WsController extends AbstractCXFServlet{
- private GenericApplicationContext childCtx;
- private boolean inRefresh;
- public void loadBus(ServletConfig servletConfig) throws ServletException {
- String springCls = "org.springframework.context.ApplicationContext";
- try {
- ClassLoaderUtils.loadClass(springCls, getClass());
- loadSpringBus(servletConfig);
- } catch (ClassNotFoundException e) {
- throw new ServletException("Can't load bus with Spring context class", e);
- }
- }
- private void loadSpringBus(ServletConfig servletConfig)
- throws ServletException
- {
- ServletContext svCtx = getServletContext();
- ApplicationContext ctx = CXFContext.getCXFContent().getCtx();
- if (ctx == null) {
- Object ctxObject = svCtx.getAttribute("org.springframework.web.context.WebApplicationContext.ROOT");
- if ((ctxObject instanceof ApplicationContext)) {
- ctx = (ApplicationContext)ctxObject;
- } else if (ctxObject != null)
- {
- Exception ex = (Exception)ctxObject;
- throw new ServletException(ex);
- }
- }
- updateContext(servletConfig, ctx);
- }
- private void updateContext(ServletConfig servletConfig, ApplicationContext ctx)
- {
- if (ctx == null) {
- this.bus = new SpringBusFactory().createBus();
- ctx = (ApplicationContext)this.bus.getExtension(BusApplicationContext.class);
- } else {
- this.bus = CXFContext.getCXFContent().getBusFactory().createBus();
- }
- ResourceManager resourceManager = (ResourceManager)this.bus.getExtension(ResourceManager.class);
- resourceManager.addResourceResolver(new ServletContextResourceResolver(servletConfig.getServletContext()));
- replaceDestinationFactory();
- this.controller = createServletController(servletConfig);
- loadAdditionalConfig(ctx, servletConfig);
- }
- private void loadAdditionalConfig(ApplicationContext ctx, ServletConfig servletConfig)
- {
- String location = servletConfig.getInitParameter("config-location");
- if (location == null) {
- location = "/META-INF/cxf/cxf-servlet.xml";
- }
- InputStream is = null;
- try {
- is = servletConfig.getServletContext().getResourceAsStream(location);
- if ((is == null) || (is.available() == -1)) {
- URIResolver resolver = new URIResolver(location);
- if (resolver.isResolved()) {
- is = resolver.getInputStream();
- }
- }
- }
- catch (IOException e)
- {
- }
- if (is != null) {
- this.childCtx = new GenericApplicationContext(ctx);
- XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.childCtx);
- reader.setValidationMode(3);
- reader.loadBeanDefinitions(new InputStreamResource(is, location));
- this.childCtx.refresh();
- }
- }
- }
web.xml配置:
- <servlet>
- <servlet-name>CXFServlet</servlet-name>
- <servlet-class>
- [color=red]cn.edu.jxut.common.config.WsController [/color] </servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>CXFServlet</servlet-name>
- <url-pattern>/service/*</url-pattern>
- </servlet-mapping>
jfinal配置:
- public void configHandler(Handlers me) {
- me.add(new UrlSkipHandler(".*/service.*",false));
- }
此服务已发布可用,在webservice实现类中直接使用jfinal的db获取数据或保存数据都非常方便。
jfinal集成spring cxf做webservice服务的更多相关文章
- 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 ...
- 搭建web项目结合spring+cxf的webservice服务
服务端: 服务端和客户端都需要引入包 antlr-2.7.7.jar aopalliance-1.0.jar asm-3.3.jar commons-collections-3.2.1.jar com ...
- Spring Boot 使用 CXF 调用 WebService 服务
上一张我们讲到 Spring Boot 开发 WebService 服务,本章研究基于 CXF 调用 WebService.另外本来想写一篇 xfire 作为 client 端来调用 webservi ...
- 使用 CXF 做 webservice 简单例子(转载)
使用 CXF 做 webservice 简单例子 Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这 ...
- CXF发布webService服务以及客户端调用
这篇随笔内容是CXF发布webService服务以及客户端调用的方法 CXF是什么? 开发工作之前需要下载CXF和安装 下载地址:http://cxf.apache.org 安装过程: <1&g ...
- 【转】构建基于CXF的WebService服务
构建基于CXF的WebService服务 Apache CXF = Celtix+ XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.C ...
- idea+maven+spring+cxf创建webservice应用(二)生成客户端程序
idea+maven+spring+cxf创建webservice应用(二)生成客户端程序,以上一篇为基础"idea+maven+spring+cxf创建webservice应用" ...
- Spring Boot 开发 WebService 服务
WebService 虽然现在大部分互联网企业不太提倡使用,但在以第三方接口为主导的市场,对方来什么接口你还得用什么接口,不可能把接口重写了.例如大部分传统的大型企业都在用 WebService,并且 ...
- Spring Boot+CXF搭建WebService服务参考资料
pom.xml文件引入包: <!--WerbService CXF依赖--> <dependency> <groupId>org.apache.cxf</gr ...
随机推荐
- c++实现精确计时
//获取比較准确是程序执行时间 #include<iostream> #include<windows.h> using namespace std; int main(voi ...
- 使用LINQ的几个小技巧
这里总结了这些技巧.介绍如何使用LINQ来: 初始化数组 在一个循环中遍历多个数组 生成随机序列 生成字符串 转换序列或集合 把值转换为长度为1的序列 遍历序列的所有子集 如果你在LINQ方面有心得也 ...
- Node.js入门-Node.js 介绍
Node.js 是什么 Node.js 不是一种独立的语言,与 PHP,Python 等"既是语言优势平台"不同,它也不是一个 JavaScrip 框架,不同于 CakePHP,D ...
- Xcode Could not load NIB 的一个问题解决
# Could not load NIB 的一个问题 html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pr ...
- ios 面试题 经典(比较全) 根据重点总结
史上最全的iOS面试题及答案 1.写一个NSString类的实现 + (id)initWithCString:(c*****t char *)nullTerminatedCString encodin ...
- PHP 时间和日期 总结
PHP 时间戳 UNIX 时间戳(timestamp)是 PHP 中关于时间日期一个很重要的概念,它表示从 1970年1月1日 00:00:00 到当前时间的秒数之和. 可以使用time()函数来获取 ...
- QQ对话代码
<li> <strong class="QQ">客服QQ</strong> <span> <a href="mqqw ...
- matlab之kmeans聚类用法
kmeans函数用法如下: [IDX,C,sumd,D] = kmeans(X,2,'Distance','city','Replicates',5,'Options',opts); 参数含义如下:I ...
- 2015-12-27 socket的用法
sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) sk.bind(address) s.bind(address) 将套接字绑定到地址.a ...
- 再次复习数据结构:c语言链表的简单操作
最近呢,又要面临多次的数据结构与算法方面的试题了,而我呢,大概也重新温习c语言的基本要点快一个月了,主要是针对指针这货的角度在研究c语言,感觉又学到了不少. 现在c指针感觉知道点了,也就匆忙开展数据结 ...