ApplicationListener和ContextRefreshedEvent

实际开发中,ApplicationListener和ContextRefreshedEvent一般都是一个类去实现ApplicationListener 接口来使用的;

1):底层原理是什么? org.springframework.context.support.AbstractApplicationContext

在IOC的容器的启动过程,当所有的bean都已经处理完成之后,spring ioc容器会有一个发布事件的动作。从 AbstractApplicationContext 的源码中就可以看出 当ioc容器加载处理完相应的bean之后,也给我们提供了一个机会(先有InitializingBean,后有ApplicationListener),可以去做一些自己想做的事。其实这也就是spring ioc容器给提供的一个扩展的地方。我们可以这样使用这个扩展机制。

protected void finishRefresh() {
          
   
        this.initLifecycleProcessor();
        this.getLifecycleProcessor().onRefresh();
        this.publishEvent((ApplicationEvent)(new ContextRefreshedEvent(this)));
        LiveBeansView.registerApplicationContext(this);
    }
public class ContextRefreshedEvent extends ApplicationContextEvent {
          
   
    public ContextRefreshedEvent(ApplicationContext source) {
          
   
        super(source);
    }
}

2)实际开发中怎么使用?

一个最简单的方式就是,让我们的bean实现ApplicationListener接口,这样当发布事件时,[spring]的ioc容器就会以容器的实例对象作为事件源类,并从中找到事件的监听者,此时ApplicationListener接口实例中的onApplicationEvent(E event)方法就会被调用

public class MyInitListener implements ApplicationListener<ContextRefreshedEvent> {
          
   
	@Autowired
    private MyService myService;
	 @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
          
   
        try {
          
    
            Map<String, String> config = myService.getGoodsInfo();         
            Consts.GOODS_CONFIG = config;
        } catch (Exception e) {
          
   
            logger.error("初始化出现异常:{}", e);
        }
    }

}

Consts配置如下;

public static Map<String, String> GOODS_CONFIG = new HashMap<String, String>();

3)网上,系统会存在两个容器,一个是root application context ,另一个就是我们自己的 projectName-servlet context(作为root application context的子容器) 这种情况下,就会造成onApplicationEvent方法被执行两次。为了避免上面提到的问题,我们可以只在root application context初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理, 可以自行测试一下结果;

if (event.getApplicationContext().getParent() == null) {
          
      //root application context 没有parent
//其它逻辑
            
            //业务逻辑
        }
经验分享 程序员 微信小程序 职场和发展