springboot集成cxf,配置文件如下:
@Configuration
public class CxfConfig {
// 添加webservice访问的前缀路径,如http://127.0.0.1:8080/cxf/ConsAndMeasureAPI?wsdl中的/cxf/路径
@Bean
public ServletRegistrationBean dispatcherServletName() {
return new ServletRegistrationBean(new CXFServlet(), "/cxf/*");
}
// 消息总线
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public Endpoint endpoint() {
// 绑定webservice接口
EndpointImpl endpoint=new EndpointImpl(springBus(), new WebMthod());
// 发布的名称
endpoint.publish("/ConsAndMeasureAPI");
return endpoint;
}
}
查看SpringBus源码
// 实现了ApplicationContextAware, 获取ApplicationContext
public class SpringBus extends ExtensionManagerBus
implements ApplicationContextAware {
AbstractApplicationContext ctx;
boolean closeContext;
public SpringBus() {
}
public void setBusConfig(BusDefinitionParser.BusConfig bc) {
bc.setBus(this);
}
/** {@inheritDoc}*/
// 接口ApplicationContextAware的方法,spring容器初始化SpringBus回调,注入ApplicationContext
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ctx = (AbstractApplicationContext)applicationContext;
@SuppressWarnings("rawtypes")
ApplicationListener listener = new ApplicationListener() {
public void onApplicationEvent(ApplicationEvent event) {
/**
* 添加事件的监听器,当有spring发生事件时候触发,事件类型
* 接收到 Spring事件 => ServletWebServerInitial
* 接收到 Spring事件 => ContextRefreshedEvent
* 接收到 Spring事件 => ApplicationStartedEvent
* 接收到 Spring事件 => AvailabilityChangeEvent
* 接收到 Spring事件 => ApplicationReadyEvent
* 接收到 Spring事件 => AvailabilityChangeEvent
* .......
*/
SpringBus.this.onApplicationEvent(event);
}
};
// 注册监听事件
ctx.addApplicationListener(listener);
ApplicationContext ac = applicationContext.getParent();
while (ac != null) {
if (ac instanceof AbstractApplicationContext) {
((AbstractApplicationContext)ac).addApplicationListener(listener);
}
ac = ac.getParent();
}
// set the classLoader extension with the application context classLoader
setExtension(applicationContext.getClassLoader(), ClassLoader.class);
setExtension(new ConfigurerImpl(applicationContext), Configurer.class);
ResourceManager m = getExtension(ResourceManager.class);
m.addResourceResolver(new BusApplicationContextResourceResolver(applicationContext));
// 保存上下文
setExtension(applicationContext, ApplicationContext.class);
ConfiguredBeanLocator loc = getExtension(ConfiguredBeanLocator.class);
if (!(loc instanceof SpringBeanLocator)) {
setExtension(new SpringBeanLocator(applicationContext, this), ConfiguredBeanLocator.class);
}
if (getState() != BusState.RUNNING) {
initialize();
}
}
// 监听springboot服务的启动,运行,关闭状态,同步webservice接口的启动,运行,关闭状态
public void onApplicationEvent(ApplicationEvent event) {
if (ctx == null) {
return;
}
boolean doIt = false;
ApplicationContext ac = ctx;
while (ac != null && !doIt) {
if (event.getSource() == ac) {
doIt = true;
break;
}
ac = ac.getParent();
}
if (doIt) {
if (event instanceof ContextRefreshedEvent) {
if (getState() != BusState.RUNNING) {
initialize();
}
} else if (event instanceof ContextClosedEvent && getState() == BusState.RUNNING) {
// The bus could be create by using SpringBusFactory.createBus("/cxf.xml");
// Just to make sure the shutdown is called rightly
shutdown();
}
}
}
public void destroyBeans() {
if (closeContext) {
ctx.close();
}
super.destroyBeans();
}
public String getId() {
if (id == null) {
try {
Class<?> clsbc = Class.forName("org.osgi.framework.BundleContext");
Class<?> clsb = Class.forName("org.osgi.framework.Bundle");
Object o = getExtension(clsbc);
Object o2 = clsbc.getMethod("getBundle").invoke(o);
String s = (String)clsb.getMethod("getSymbolicName").invoke(o2);
id = s + "-" + DEFAULT_BUS_ID + Integer.toString(this.hashCode());
} catch (Throwable t) {
id = super.getId();
}
}
return id;
}
public void setCloseContext(boolean b) {
closeContext = b;
}
}
查看另外一个配置实例
public EndpointImpl(Bus b, Object i, String bindingUri, String wsdl, WebServiceFeature f[]) {
bus = b;
implementor = i;
this.bindingUri = bindingUri;
wsdlLocation = wsdl == null ? null : new String(wsdl);
// 用于发布webservice的服务
serverFactory = new JaxWsServerFactoryBean();
if (f != null) {
((JaxWsServiceFactoryBean)serverFactory.getServiceFactory()).setWsFeatures(Arrays.asList(f));
}
}
JaxWsServiceFactoryBean通过spring的上下文发布wsdl路径,通过监听器来处理请求。
public void start() {
if (!stopped) {
return;
}
LOG.fine("Server is starting.");
bindingFactory.addListener(destination, endpoint);
// register the active server to run
if (null != serverRegistry) {
LOG.fine("register the server to serverRegistry ");
serverRegistry.register(this);
}
if (slcMgr == null) {
slcMgr = bus.getExtension(ServerLifeCycleManager.class);
if (slcMgr != null && mep != null) {
slcMgr.registerListener(mep);
}
}
if (slcMgr != null) {
slcMgr.startServer(this);
}
stopped = false;
}
瑕疵太多,下次时间多了,慢慢整理一下。