ApplicationContext上下文

ApplicationContext

ApplicationContext是什么?

  • ApplicationContext是一个大哥,手下有很多小弟,自己基本上不实现功能,干活的都是小弟。
  • 具体从下面的代码可以看出ApplicationContext继承了Environment、MessageSource、Resource…也就拥有了他们的功能
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
		MessageSource, ApplicationEventPublisher, ResourcePatternResolver
  • 那么如何来获取ApplicationContext呢?

方式一 ApplicationContextInitializer

  • 通过实现ApplicationContextInitializer接口来引入ApplicationContext
  • 直接上代码
public class SpringBeanUtil1 implements ApplicationContextInitializer {
     

    private static ApplicationContext applicationContext;

    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
     
        if (null == applicationContext) {
     
            applicationContext = configurableApplicationContext;
        }
    }

    /**
     * @param clazz
     * @param 
     * @return 获取bean
     */
    public static <T> T getBean(Class<T> clazz) {
     
        return applicationContext.getBean(clazz);
    }
}

// 在Application类中启动方式如下
@SpringBootApplication
public class SpringUtilApplication {
     

    public static void main(String[] args) {
     
        // 方式1,通过ApplicationContextInitializer
        // 也可以通过spring.factories来加载 org.springframework.context.ApplicationContextInitializer=xxx 初始化的
        SpringApplication springApplication = new SpringApplication(SpringUtilApplication.class);
        springApplication.addInitializers(new SpringBeanUtil1());
        springApplication.run(args);
        ServiceDemo bean = SpringBeanUtil1.getBean(ServiceDemo.class);
        System.out.println(bean.toString());
    }
}

方式二 ApplicationListener

  • 通过上下文初始化的时候会处理监听接口
  • 直接上代码
@Component
public class SpringBeanUtil2 implements ApplicationListener<ApplicationContextEvent> {
     

    private static ApplicationContext applicationContext;

    /**
     * @param clazz
     * @param 
     * @return 获取bean
     */
    public static <T> T getBean(Class<T> clazz) {
     
        return applicationContext.getBean(clazz);
    }


    /**
     * 监听刷新事件
     * @param event
     */
    @Override
    public void onApplicationEvent(ApplicationContextEvent event) {
     
        if (null == applicationContext) {
     
            applicationContext = event.getApplicationContext();
        }
    }
}
// 启动类
@SpringBootApplication
public class SpringUtilApplication {
     
    public static void main(String[] args) {
     
      // 方式2,通过ApplicationListener
      SpringApplication.run(SpringUtilApplication.class);
        ServiceDemo bean = SpringBeanUtil2.getBean(ServiceDemo.class);
        System.out.println(bean.toString()); 
    }
}

方式三 ApplicationContextAware

  • 通过Spring的通知接口来注入上下文配置
  • 直接上代码
@Component
public class SpringBeanUtil3 implements ApplicationContextAware {
     

    private static ApplicationContext context;

    /**
     * @param clazz
     * @param 
     * @return 获取bean
     */
    public static <T> T getBean(Class<T> clazz) {
     
        return context.getBean(clazz);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
     
        if (null == context) {
     
            context = applicationContext;
        }
    }
}

// 启动类配置
@SpringBootApplication
public class SpringUtilApplication {
     

    public static void main(String[] args) {
     
      // 方式3,通过ApplicationContextAware来获取
      SpringApplication.run(SpringUtilApplication.class);
        ServiceDemo bean = SpringBeanUtil3.getBean(ServiceDemo.class);
        System.out.println(bean.toString());
    }
}

方式四 SpringApplication

  • SpringBoot启动Application的时候可以获取到ApplicationContext,这个时候可以把ApplicationContext注入到SpringBeanUtil中去
  • 直接上代码
@Component
public class SpringBeanUtil4 {
     

    private static ApplicationContext context;

    /**
     * @param clazz
     * @param 
     * @return 获取bean
     */
    public static <T> T getBean(Class<T> clazz) {
     
        return context.getBean(clazz);
    }


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

// main方法
@SpringBootApplication
public class SpringUtilApplication {
     

    public static void main(String[] args) {
     
        // 直接通过main方法设置值
        ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringUtilApplication.class);
        SpringBeanUtil4.setApplicationContext(applicationContext);
        ServiceDemo bean = SpringBeanUtil4.getBean(ServiceDemo.class);
        System.out.println(bean.toString());

    }
}

总结

  • 以上就是获取ApplicationContext的四种方式,可以通过上面的任意一种方式来实现自己的SpringBeanUtil类,方便获取Bean实例,同时也可以在自己的类中通过@Autowired的方式直接引入ApplicationContext

大家也可以关注spring-cloud系列
1-openfeign
2-hystrix
你们的点赞和关注是我创作的最大动力,有什么不足和错误的地方欢迎留言!可以微信搜索关注【小二说码

你可能感兴趣的:(打怪升级系列,spring,spring,boot,java)