您的位置:首页 > 编程语言 > Java开发

Jfinal 集成spring 、cxf 做webService服务

2016-07-09 14:03 483 查看
废话不说,直接上代码

新增cxf的plugin

CXFPlugin

Java代码  


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

Java代码  


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

Java代码  


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配置:

Java代码  


<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配置:

Java代码  


public void configHandler(Handlers me) {  
        me.add(new UrlSkipHandler(".*/service.*",false));  
    } 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: