Springboot中随时获取ApplicationContext,得到想要的bean

时间:2024-11-02 07:45:55

在springBoot启动类的main方法中写成

/**
     * 启动嵌入式的Tomcat并初始化Spring环境
     */
    public static void main(String[] args) throws Exception {
        ApplicationContext app = (, args);
        (app);
        //(, args);
    }

创建工具类

package ;

import ;
import ;
import ;
import ;

import ;
import ;

public class SpringUtil  {

    private static ApplicationContext applicationContext = null;
// 非@import显式注入,@Component是必须的,且该类必须与main同包或子包
    // 若非同包或子包,则需手动import 注入,有没有@Component都一样
    // 可复制到Test同包测试

    public static void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if( == null){
              = applicationContext;
        }
        //查询所有预警任务,开启的预警任务自动开启
        RiskWarningTaskService riskWarningTaskServiceStatic=();
        List<EarlyWarningTask> earlyWarningTask = ();
        for (EarlyWarningTask warningTask : earlyWarningTask) {
            //如果任务是打开的,可就开启任务
            if("1".equals(())){
                ("1",().toString(),());
                ("0",().toString(),());
            }
        }
    }

    //获取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //通过name获取 Bean.
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);

    }

    //通过class获取Bean.
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    //通过name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }

}