在Spring boot 中获取spring上下文的工具类

/**
 * 从Spring上下文取出Bean工具类
 *
 * @author ZhuPengWei
 * @date 2018/6/27 18:04
 */
public class SpringApplicationContextUtil {

    private static ApplicationContext applicationContext = null;

    public static void setApplicationContext(ApplicationContext applicationContext) {
        if (SpringApplicationContextUtil.applicationContext == null) {
            SpringApplicationContextUtil.applicationContext = applicationContext;
        }

    }

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

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

    }

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

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

}

启动类上需要设置


public static void main(String[] args) {
        ApplicationContext run = SpringApplication.run(MusicCpApplication.class, args);
        SpringApplicationContextUtil.setApplicationContext(run);
    }

你可能感兴趣的:(spring-boot)