链接地址:http://zhengshuo.iteye.com/blog/2154047
废话不说,直接上代码
新增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));
- }
此服务已发布可用,在webservice实现类中直接使用jfinal的db获取数据或保存数据都非常方便。
时间: 2024-12-25 17:00:08