zoukankan      html  css  js  c++  java
  • 42、扩展原理-ApplicationListener原理

    42、扩展原理-ApplicationListener原理

    • 有三个事件
    • ContextRefreshedEvent、Test_Ext$1[source=我发布了一个事件]、ContextClosedEvent
    1. ContextRefreshedEvent事件:
      1. 容器创建对象:refresh();
      2. finishRefresh();容器刷新完成会发布ContextRefreshedEvent事件
    2. 自己发布事件;
    3. 容器关闭会发布ContextClosedEvent;

    【事件发布流程】:

    publishEvent(new ContextRefreshedEvent(this));

    1)、获取事件的多播器(派发器):getApplicationEventMulticaster()
    2)、multicastEvent派发事件:
    3)、获取到所有的ApplicationListener;
    for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
    
    1. 如果有Executor,可以支持使用Executor进行异步派发;

      Executor executor = getTaskExecutor();
      
    2. 否则,同步的方式直接执行listener方法;invokeListener(listener, event);

      1. 拿到listener回调onApplicationEvent方法;

    【事件多播器(派发器)】:

    1)、容器创建对象:refresh();
    2)、initApplicationEventMulticaster();初始化ApplicationEventMulticaster;
    1. 先去容器中找有没有id=“applicationEventMulticaster”的组件;

    2. 如果没有

      this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
      并且加入到容器中,我们就可以在其他组件要派发事件,自动注入这个applicationEventMulticaster;
      

    【容器中有哪些监听器】:

    1)、容器创建对象:refresh();
    2)、registerListeners();
    // 从容器中拿到所有的监听器,把他们注册到applicationEventMulticaster中;
    String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
    // 将listener注册到ApplicationEventMulticaster中
    getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
    
  • 相关阅读:
    Java面试题
    大二上学期周总结
    大二上每日总结
    大二上每日总结
    大二上每日总结
    大二上每日总结
    大二上每日总结
    大二上学期周总结
    大二上每日总结
    大二上每日总结
  • 原文地址:https://www.cnblogs.com/Grand-Jon/p/10067945.html
Copyright © 2011-2022 走看看