cxf的一些使用说明
/**
* 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的一些使用说明的更多相关文章
- Atitit.项目修改补丁打包工具 使用说明
Atitit.项目修改补丁打包工具 使用说明 1.1. 打包工具已经在群里面.打包工具.bat1 1.2. 使用方法:放在项目主目录下,执行即可1 1.3. 打包工具的原理以及要打包的项目列表1 1. ...
- java调用CXF WebService接口的两种方式
通过http://localhost:7002/card/services/HelloWorld?wsdl访问到xml如下,说明接口写对了. 2.静态调用 // 创建WebService客户端代理工厂 ...
- webService学习之路(三):springMVC集成CXF后调用已知的wsdl接口
webService学习之路一:讲解了通过传统方式怎么发布及调用webservice webService学习之路二:讲解了SpringMVC和CXF的集成及快速发布webservice 本篇文章将讲 ...
- webService学习之路(二):springMVC集成CXF快速发布webService
继上一篇webService入门之后,http://www.cnblogs.com/xiaochangwei/p/4969448.html ,现在我将我周六在家研究的结果公布出来 本次集成是基于之前已 ...
- awk使用说明
原文地址:http://www.cnblogs.com/verrion/p/awk_usage.html Awk使用说明 运维必须掌握的三剑客工具:grep(文件内容过滤器),sed(数据流处理器), ...
- CXF:根据werservice代码生成WSDL(转)
原文:http://hongyegu.iteye.com/blog/619147,谢谢! import org.apache.cxf.tools.java2ws.JavaToWS; import ne ...
- webservice入门实例,CXF方式
1.下载CXF,及先关jar包. CXF 下载地址:http://cxf.apache.org/download.html,选择"File"列中的zip格式下载.解压后可以看到一些 ...
- 脱离spring集成cxf(基于nutz框架)
什么是webService WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 理论资料: http://blog.csdn.net/wooshn/article/details/8 ...
- WebService -- Java 实现之 CXF ( 使用Spring添加拦截器)
最重要的就是在ApplicationContext.xml下面添加配置 <!-- service provider --> <jaxws:endpoint implementor=& ...
随机推荐
- Oracle单表去重复(一)
去重有两层含义,一:是记录完全一样:二:是符合一定条件的认为是重复. 根据表的数量,去重可划分为:单表去重和多表关联去重. 对于去重,一般最容易想到的是用distinct,而distinct只能对 ...
- CollabNet Subversion Edge 安装笔记(1):基本安装设定篇
转载于:http://blog.miniasp.com/post/2011/12/30/CollabNet-Subversion-Edge-Installation-Notes-Part-1-Basi ...
- return 0;和exit(0);的区别
首先说一下fork和vfork的差别: fork 是 创建一个子进程,并把父进程的内存数据copy到子进程中. vfork是 创建一个子进程,并和父进程的内存数据share一起用. 这两个的差别是,一 ...
- 腾讯高性能RPC开发框架Tars实现服务治理(微服务)
Github:https://github.com/Tencent/Tars 1. 介绍 Tars是基于名字服务使用Tars协议的高性能RPC开发框架,同时配套一体化的服务治理平台,帮助个人或者企业快 ...
- 北京师范大学第十六届程序设计竞赛决赛-重现赛-B题
一.题目链接 https://www.nowcoder.com/acm/contest/117/B 二.题意 给定一组序列$a_1,a_2,\cdots,a_n$,表示初始序列$b_1,b_2,\cd ...
- pyCharm最新2018激活码(转)
原文地址:https://blog.csdn.net/u014044812/article/details/78727496 社区版和专业版区别: 因公司的需求,需要做一个爬取最近上映的电影.列车号. ...
- java构造函数是否可继承,以及子类构造函数可否不使用super调用超类构造函数
问题一:java的构造函数能否被继承? 笔者初学java看的一本书说:“java的子类自然的继承其超类的“非private成员”. 通常java的构造函数被设置为public的(若你不写构造函数,ja ...
- Getting Started(入门)
欢迎阅读专门针对android开发者的培训课程,在这一系列的课程中,描述了如何通过我们的示例代码来完成特定的任务和功能,这些代码可以灵活地应用到你的应用程序中. 课程被分成了几部分, 第一部分,入门, ...
- 并发服务器和HTTP协议
单进程服务器 1. 完成一个简单的TCP服务器 from socket import * serSocket = socket(AF_INET, SOCK_STREAM) # 重复使用绑定的信息 se ...
- lookup 控件
lookup->ADOQuery->Connection = DataModule1->ADOConnection1; lookup->ADOQuery->SQL-> ...