田源
2024-03-01 02b3d584d201ca7cb8a024fd151fe6eddbf43def
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.vci.ubcs.code.webService.config;
 
import com.alibaba.cloud.commons.lang.StringUtils;
import com.vci.ubcs.code.webService.annotation.VciWebservice;
import com.vci.ubcs.starter.web.util.ApplicationContextProvider;
import com.vci.ubcs.starter.web.util.VciBaseUtil;
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);
    }
}