354798ggg
2023-10-17 0552181155581ecd56242d8e4d3f90d420873a3f
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
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<String> 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);
        }
    }
 
}