/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cxf.transport.servlet; import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Enumeration; import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException; import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.common.util.ReflectionUtil;
import org.apache.cxf.helpers.CastUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext; public class CXFServlet extends CXFNonSpringServlet
implements ApplicationListener<ContextRefreshedEvent> {
private static final long serialVersionUID = -5922443981969455305L;
private boolean busCreated;
private XmlWebApplicationContext createdContext; public CXFServlet() {
} @Override
protected void loadBus(ServletConfig sc) {
ApplicationContext wac = WebApplicationContextUtils.
getWebApplicationContext(sc.getServletContext()); if (wac instanceof AbstractApplicationContext) {
addListener((AbstractApplicationContext)wac);
} String configLocation = sc.getInitParameter("config-location");
if (configLocation == null) {
try {
InputStream is = sc.getServletContext().getResourceAsStream("/WEB-INF/cxf-servlet.xml");
if (is != null && is.available() > ) {
is.close();
configLocation = "/WEB-INF/cxf-servlet.xml";
}
} catch (Exception ex) {
//ignore
}
}
if (configLocation != null) {
wac = createSpringContext(wac, sc.getServletContext(), configLocation);
}
if (wac != null) {
setBus((Bus)wac.getBean("cxf", Bus.class));
} else {
busCreated = true;
setBus(BusFactory.newInstance().createBus());
}
} protected void addListener(AbstractApplicationContext wac) {
try {
//spring 2 vs spring 3 return type is different
Method m = wac.getClass().getMethod("getApplicationListeners");
Collection<Object> c = CastUtils.cast((Collection<?>)ReflectionUtil
.setAccessible(m).invoke(wac));
c.add(this);
} catch (Throwable t) {
//ignore.
}
} /**
* Try to create a spring application context from the config location.
* Will first try to resolve the location using the servlet context.
* If that does not work then the location is given as is to spring
*
* @param ctx
* @param sc
* @param configLocation
* @return
*/
private ApplicationContext createSpringContext(ApplicationContext ctx,
final ServletContext sc,
String location) {
XmlWebApplicationContext ctx2 = new XmlWebApplicationContext();
createdContext = ctx2;
ctx2.setServletConfig(new ServletConfig() {
public String getServletName() {
return "CXF";
}
public ServletContext getServletContext() {
return sc;
}
public String getInitParameter(String name) {
return sc.getInitParameter(name);
}
public Enumeration<String> getInitParameterNames() {
return sc.getInitParameterNames();
}
});
Resource r = ctx2.getResource(location);
try {
InputStream in = r.getInputStream();
in.close();
} catch (IOException e) {
//ignore
r = ctx2.getResource("classpath:" + location);
try {
r.getInputStream().close();
} catch (IOException e2) {
//ignore
r = null;
}
}
try {
if (r != null) {
location = r.getURL().toExternalForm();
}
} catch (IOException e) {
//ignore
}
if (ctx != null) {
ctx2.setParent(ctx);
String names[] = ctx.getBeanNamesForType(Bus.class);
if (names == null || names.length == ) {
ctx2.setConfigLocations(new String[] {"classpath:/META-INF/cxf/cxf.xml",
location});
} else {
ctx2.setConfigLocations(new String[] {location});
}
} else {
ctx2.setConfigLocations(new String[] {"classpath:/META-INF/cxf/cxf.xml",
location});
createdContext = ctx2;
}
ctx2.refresh();
return ctx2;
}
public void destroyBus() {
if (busCreated) {
//if we created the Bus, we need to destroy it. Otherwise, spring will handleit.
getBus().shutdown(true);
setBus(null);
}
if (createdContext != null) {
createdContext.close();
}
} public void onApplicationEvent(ContextRefreshedEvent event) {
destroy();
setBus(null);
try {
init(getServletConfig());
} catch (ServletException e) {
throw new RuntimeException("Unable to reinitialize the CXFServlet", e);
}
} }

如果存在Web-INF/cxf-servlet.xml,或者在web.xml中配置的cxf Servlet节按中提供了config-location,那么cxf会自己创建一个spring容器,如果在web.xml中同时也使用spring的监听器创建容器

 <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:conf/cxf-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

那么cxf容器会作为子容器,就是系统中有2个spring容器。
cxf自己创建容器时会加载classpath:/META-INF/cxf/cxf.xml这个就中的bug,如果系统只建立一个spring容器(Web-INF/下以及cxfServlet中不提供配置),那么需要自己在spring配置xml文件中通过资源导入classpath:/META-INF/cxf/cxf.xml 加载,或者将该配置放在web.xml的context-param 中

cxf的一些使用说明的更多相关文章

  1. Atitit.项目修改补丁打包工具 使用说明

    Atitit.项目修改补丁打包工具 使用说明 1.1. 打包工具已经在群里面.打包工具.bat1 1.2. 使用方法:放在项目主目录下,执行即可1 1.3. 打包工具的原理以及要打包的项目列表1 1. ...

  2. java调用CXF WebService接口的两种方式

    通过http://localhost:7002/card/services/HelloWorld?wsdl访问到xml如下,说明接口写对了. 2.静态调用 // 创建WebService客户端代理工厂 ...

  3. webService学习之路(三):springMVC集成CXF后调用已知的wsdl接口

    webService学习之路一:讲解了通过传统方式怎么发布及调用webservice webService学习之路二:讲解了SpringMVC和CXF的集成及快速发布webservice 本篇文章将讲 ...

  4. webService学习之路(二):springMVC集成CXF快速发布webService

    继上一篇webService入门之后,http://www.cnblogs.com/xiaochangwei/p/4969448.html ,现在我将我周六在家研究的结果公布出来 本次集成是基于之前已 ...

  5. awk使用说明

    原文地址:http://www.cnblogs.com/verrion/p/awk_usage.html Awk使用说明 运维必须掌握的三剑客工具:grep(文件内容过滤器),sed(数据流处理器), ...

  6. CXF:根据werservice代码生成WSDL(转)

    原文:http://hongyegu.iteye.com/blog/619147,谢谢! import org.apache.cxf.tools.java2ws.JavaToWS; import ne ...

  7. webservice入门实例,CXF方式

    1.下载CXF,及先关jar包. CXF 下载地址:http://cxf.apache.org/download.html,选择"File"列中的zip格式下载.解压后可以看到一些 ...

  8. 脱离spring集成cxf(基于nutz框架)

    什么是webService WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 理论资料: http://blog.csdn.net/wooshn/article/details/8 ...

  9. WebService -- Java 实现之 CXF ( 使用Spring添加拦截器)

    最重要的就是在ApplicationContext.xml下面添加配置 <!-- service provider --> <jaxws:endpoint implementor=& ...

随机推荐

  1. lerna基本试用

    安装 yarn global add lerna or npm install -g lerna 基本项目 初始化 git init platform cd platform lerna init 效 ...

  2. Spring Boot 入门之消息中间件篇(五)

    原文地址:Spring Boot 入门之消息中间件篇(五) 博客地址:http://www.extlight.com 一.前言 在消息中间件中有 2 个重要的概念:消息代理和目的地.当消息发送者发送消 ...

  3. PHP框架(如:laravel、yii2、thinkPHP5)中统一异常处理及统一日志打印

    背景: 现在写接口服务应用有一个很通用的需求,想通过日志.或者监控的形式监测的接口的运行情况,比如耗时.请求参数.响应结果.和前端联调接口时或者排查线上问题时日志必不可少,特别是现场日志. 应用运行时 ...

  4. Oracle中的一连接语句

    首先构建场景 相应表中数据如下: SELECT * FROM EMPLOYEE: SELECT * FROM DEPTINFO; 连接方式: 1. , SELECT E.EMPNAME, D.DEPN ...

  5. emacs之配置etags

    emacsConfig/etags-setting.el (require 'auto-complete-etags) (setq ac-sources (append '(ac-source-eta ...

  6. [html]CSS 小贴士

    CSS 中用四个伪类来定义链接的样式,分别是:a:link.a:visited.a:hover 和 a : active,例如: a:link{font-weight : bold ;text-dec ...

  7. ubuntu 16.04安装Chrome离线crx插件包

    /opt/google/chrome/google-chrome --enable-easy-off-store-extension-install 打开浏览器后,输入chrome://extensi ...

  8. leetcode 几何题 位运算 面试编程

    [BZOJ][CQOI2014]数三角形 Description给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个.下图为4x4的网格上的一个三角形. 注意三角形的三点不能共线. Input ...

  9. 【CentOS 6.5】安装gcc-4.8.2和Qt5.2

    因为CentOS6.5中gcc版本比较低.安装Qt5.2前先升级gcc. 首先下载gcc,附上百度盘地址:http://pan.baidu.com/s/1jGibvqY 解压安装包,并进入到解压文件夹 ...

  10. leetcode190

    public class Solution { public uint reverseBits(uint n) { var list = new List<uint>();//逆序的二进制 ...