spring boot ApplicationListener监听事件原理

    xiaoxiao2022-07-14  153

    package com.zdc.sp.listener;

    import java.util.EventListener;

    import org.springframework.context.ApplicationEvent; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;

    /**  * ApplicationListener 监听器中发布的事件,事件驱动模型开发  *           *         public interface ApplicationListener<E extends ApplicationEvent>  *             监听ApplicationEvent 及其下面的子事件  *   *     步骤:  *       1)、写一个监听器来监听某一个事件(ApplicationEvent及其子类)  *       2)、把监听器加入到容器中;  *       3)、只要容器中相关事件的发布,我们就能监听到这个事件  *                   ContextRefershedEvent:容器刷新完成(所有bean都完成创建)会发布这个事件;  *                   ContextClosedEvent:关闭容器发布这个事件  *       4)、发布一个是事件:  *               applicationContext.publishEvent();  *               例子:applicationContext.publishEvent(new ApplicationEvent(new String("这是一个发布事件!")) {});  *  原理:  *      1、ContextRefreshedEvent事件:  *          1)、容器创建对象:fresh();  *          2)、finishRefresh();容器刷新完成;  *          3)、publishEvent(new ContextRefreshdEvent(this));  *              事件发布流程:  *                 1)、获取事件的多播器(派发器):getApplicationEventMulticaster()  *                 2)、multicastEvent派发事件:  *                 3)、获取到所有的ApplicationListener;  *                         for (final ApplicationListener<?> listener : getApplicationListeners(event,type))  *                             1)、若有Executor,则可以支持使用Executor进行异步派发;  *                             2)否则,同步的方式直接执行listener方法;invokeListener(listener,event);  *                                 拿到listener回调onApplicationEvent方法;  *                   *     2、(事件的多播器(派发器)):  *                 1)、容器创建对象;refresh();  *                 2)、initApplicationEventMulticaster();初始化ApplicationEventMulticaster;  *                     1、先去容器中找到有没有id = "applicationEventMulticaster"  *                     2、如果没有this.applicationEventMulicaster = new SimpleApplicationEventMulicaster(beanFactory)  *                         并且加入到容器中,我们就可以在其他组件要派发事件,自动注入这个applicationEventMulicaster;  *     3、容器中有哪些监听器  *                 1)容器的创建:refresh();  *                 2)、registerListener();  *                     从容器中拿到所有的监听器,把他们注册到applicationEventMulicaster中;  *                     String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class,true,false)  *                     //将listener注册到ApplicationEventMulicaster中  *                 getApplicationEventMulicaster().addApplicationListenerBean(listenerBeanName)  *                  *                       *   *   * @author lenovo  *  */ @Configuration @ComponentScan("com.zdc.sp.listener") public class Myconfig {

    }  

    代码示例:

    package com.zdc.sp.listener;

    import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component;

    @Component public class MyApplicationListener implements ApplicationListener<ApplicationEvent>{

        //当容器中发布此事件后,方法触发     @Override     public void onApplicationEvent(ApplicationEvent event) {         System.out.println("收到事件:"+event);              }

    } 测试类

    package com.zdc.sp.listener;

    import org.springframework.context.ApplicationEvent; import org.springframework.context.annotation.AnnotationConfigApplicationContext;

    public class Test {

        public static void main(String[] args) {         AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Myconfig.class);         //applicationContext.close();                  applicationContext.publishEvent(new ApplicationEvent(new String("这是一个发布事件!")) {});     } }  

    最新回复(0)