package com.vci.web.util; import com.vci.starter.web.annotation.bus.VciLoginBefore; import com.vci.starter.web.annotation.bus.VciLoginPlugin; 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 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); } } } } }); } } }