package com.vci.ubcs.webservice.config;
|
|
import com.alibaba.cloud.commons.lang.StringUtils;
|
import com.vci.ubcs.starter.web.util.ApplicationContextProvider;
|
import com.vci.ubcs.starter.web.util.VciBaseUtil;
|
import com.vci.ubcs.webservice.annotation.VciWebservice;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.cxf.Bus;
|
import org.apache.cxf.jaxws.EndpointImpl;
|
import org.springframework.aop.support.AopUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.util.CollectionUtils;
|
|
import java.util.Map;
|
|
/**
|
* 发布服务类
|
* @author weidy
|
* @date 2020/3/27
|
*/
|
@Configuration
|
@Slf4j
|
public class VciCxfPublishConfig {
|
|
/**
|
* 注入cxf的bus
|
*/
|
@Autowired(required = false)
|
private Bus bus;
|
|
|
|
|
/**
|
* 发布站点服务
|
* @return 站点服务
|
*/
|
@Bean
|
public void autoPushCxf(){
|
|
log.info("开始进行自动发布webService接口");
|
Map<String, Object> beansWithAnnotation = ApplicationContextProvider.getApplicationContext().getBeansWithAnnotation(VciWebservice.class);
|
if(!CollectionUtils.isEmpty(beansWithAnnotation)){
|
//找这些bean的
|
beansWithAnnotation.forEach((beanName,bean)->{
|
Class<?> targetClass = AopUtils.getTargetClass(bean);
|
VciWebservice annotation = targetClass.getDeclaredAnnotation(VciWebservice.class);
|
if(annotation == null){
|
targetClass.getAnnotation(VciWebservice.class);
|
}
|
if(annotation!=null){
|
//我们去找路径和名称
|
String name = annotation.value();
|
if(StringUtils.isBlank(name)){
|
name = VciBaseUtil.toLowForFirst(targetClass.getSimpleName()).replace(".class","");
|
}
|
String path = annotation.path();
|
if(StringUtils.isBlank(path)){
|
path = "/" + name;
|
}
|
//注册
|
EndpointImpl endpoint = new EndpointImpl(bus,bean);
|
endpoint.publish(path);
|
log.info(String.format("发布接口地址:[%s]", path));
|
registerBean(endpoint,name + "_EndPoint");
|
}
|
});
|
}
|
log.info("weBservice接口自动发布结束");
|
}
|
|
/**
|
* 动态注入bean
|
* @param singletonObject 注入对象
|
* @param beanName bean名称
|
*/
|
public void registerBean(Object singletonObject,String beanName) {
|
|
//获取BeanFactory
|
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) ApplicationContextProvider.getApplicationContext().getAutowireCapableBeanFactory();
|
|
//动态注册bean.
|
defaultListableBeanFactory.registerSingleton(beanName, singletonObject);
|
}
|
}
|