ludc
2024-10-15 aecacfb404d19749260189ab1d4ee90efc92ae24
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
package com.vci.web.util;
 
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.util.ApplicationContextProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
 
import java.lang.reflect.Method;
import java.util.Map;
 
/**
 * 业务的注解工具类
 * @author weidy
 * @date 2022-04-12
 */
public class BusAnnotationUtil {
 
    /**
     * 日志
     */
    private static Logger logger = LoggerFactory.getLogger(BusAnnotationUtil.class);
 
    /**
     * 通过注解去调用对应的方法
     * @param classAnnotation 类的注解
     * @param methodAnnotation 方法的注解
     * @param args 参数
     */
    public static void callForAnnotation(Class classAnnotation,Class methodAnnotation,Object ... args){
        //在登录之前,看看是否有插件
        Map<String, Object> beanMap = ApplicationContextProvider.getApplicationContext().getBeansWithAnnotation(classAnnotation);
        if (!CollectionUtils.isEmpty(beanMap)) {
            beanMap.forEach((k, v) -> {
                Method[] methods = v.getClass().getSuperclass().getDeclaredMethods();
                if (methods != null && methods.length > 0) {
                    for (Method method : methods) {
                        if (method.isAnnotationPresent(methodAnnotation)) {
                            try {
                                method.invoke(v, args);
                            } catch (Throwable e) {
                                if (logger.isErrorEnabled()) {
                                    logger.error("调用插件出错", e);
                                }
                                throw new VciBaseException("调用插件出错,{0},{1}", new String[]{v.getClass().getName(), method.getName()}, e);
                            }
                        }
                    }
                }
            });
        }
    }
}