SpringBoot 获取 ApplicationContext

1. 概念

ApplicationContext是什么?

简单来说就是Spring中的容器,可以用来获取容器中的各种bean组件,注册监听事件,加载资源文件等功能

2. 获取ApplicationContext的方式

2.1. 创建工具类

通过此工具类,可以方便的获取bean组件, 获取配置信息等

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;


public class ApplicationContextUtil {

    private static ApplicationContext applicationContext = null;

    private ApplicationContextUtil() {
    }

    public static void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (ApplicationContextUtil.applicationContext == null) {
            ApplicationContextUtil.applicationContext = applicationContext;
        }
    }

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


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

    }

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

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

}

2.2 在main方法设置

@SpringBootApplication
public class SpiderApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(SpiderApplication.class, args);
        ApplicationContextUtil.setApplicationContext(run);
    }

}

3. 获取 beanFactory 的方式

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

public class BeanFactoryUtil implements BeanFactoryPostProcessor {

    private static BeanFactory beanFactory;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        init(configurableListableBeanFactory);
    }

    private static void init(BeanFactory factory) {
        beanFactory = factory;
    }

    public static BeanFactory getBeanFactory() {
        return beanFactory;
    }

    /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return getBeanFactory().getBean(name);

    }

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

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

可搜索 springboot 扩展点 了解更多

你可能感兴趣的:(springboot,spring,boot,java,spring)