spring中获取容器的方式

1.实现ApplicationContextAware接口

重写setApplicationContext 方法  添加@Component 注解

@Component
public class VipSolverChooser implements ApplicationContextAware {
    private Map<String, AbstractVipSolver> chooseMap = new HashMap<>();
    private ApplicationContext context;

    public AbstractVipSolver choose(String type) {
        return chooseMap.get(type);
    }

    @PostConstruct
    public void register() {
        Map<String, AbstractVipSolver> solverMap = context.getBeansOfType(AbstractVipSolver.class);
        for (AbstractVipSolver solver : solverMap.values()) {
            for (String support : solver.supports()) {
                chooseMap.put(support, solver);
            }
        }
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}

  

2.从Application 启动类之间讲spring容器放入对象内

package com.yssoft.fs.supervise.utils;

import com.yssoft.fs.repository.entity.comm.EarlyWarningTask;
import com.yssoft.fs.supervise.service.riskWarning.RiskWarningTaskService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;

import javax.annotation.Resource;
import java.util.List;

public class SpringUtil  {

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

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

    //获取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);
    }

}

  

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

  

建议使用第一种非常简单

上一篇:SpringBoot中ApplicationContextAware获取上下文


下一篇:Spring IoC getBean 方法详解