package com.product; import com.google.common.collect.Lists; import com.product.common.utils.spring.SpringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.ReflectionUtils; import java.lang.reflect.Method; import java.util.List; /** * @Author cheng * @Description 系统启动初始化加载 * @Date 2021/11/8 15:14 * @Version 1.0 */ public class InitialLoad { private static Logger logger = LoggerFactory.getLogger(InitialLoad.class); /** * 初始化加载bean.method 只接受无参方法 * 且必须到缓存表中配置相应缓存数据 */ private static List initMethods = Lists.newArrayList( "sysJobService.init", "systemUserService.initUserSignature" ); public static void runInitialization() { for (int i = 0; i < initMethods.size(); i++) { String beanMethod = initMethods.get(i); if (beanMethod.indexOf('.') > -1) { String[] p = beanMethod.split("\\."); execute(p[0], p[1]); //删除已执行过的方法防止重复初始化 initMethods.remove(i); i--; } } } private static void execute(String beanName, String methodName) { try { Object bean = SpringUtils.getBean(beanName); Method method = null; if (bean != null) { method = ReflectionUtils.findMethod(bean.getClass(), methodName); } if (bean == null || method == null) { throw new ReflectiveOperationException(" execute bean or method not exist ! "); } ReflectionUtils.invokeMethod(method, bean); } catch (Exception e) { logger.error("execute init method " + beanName + "." + methodName + " error !", e); } } }