Spring监听器之ApplicationListener原理及源码深度解析
一、原理及源码解析
事件:ContextRefreshedEvent、IOCTest_Ext$1[source=我发布的事件]、ContextClosedEvent;
* 1)、ContextRefreshedEvent事件:
* 1)、容器创建对象:refresh();
* 2)、finishRefresh();容器刷新完成会发布ContextRefreshedEvent事件
* 2)、自己发布事件;
* 3)、容器关闭会发布ContextClosedEvent;
*
* 【事件发布流程】源码执行流程:
* 3)、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);
* 拿到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);
*
* SmartInitializingSingleton 原理:->afterSingletonsInstantiated();
* 1)、ioc容器创建对象并refresh();
* 2)、finishBeanFactoryInitialization(beanFactory);初始化剩下的单实例bean;
* 1)、先创建所有的单实例bean;getBean();
* 2)、获取所有创建好的单实例bean,判断是否是SmartInitializingSingleton类型的;
* 如果是就调用afterSingletonsInstantiated();
*
二、实例
ApplicationListener:监听容器中发布的事件。事件驱动模型开发;
* public interface ApplicationListener<E extends ApplicationEvent>
* 监听 ApplicationEvent 及其下面的子事件;
*
* 步骤:
* 1)、写一个监听器(ApplicationListener实现类)来监听某个事件(ApplicationEvent及其子类)
* @EventListener;
* 原理:使用EventListenerMethodProcessor处理器来解析方法上的@EventListener;
*
* 2)、把监听器加入到容器;
* 3)、只要容器中有相关事件的发布,我们就能监听到这个事件;
* ContextRefreshedEvent:容器刷新完成(所有bean都完全创建)会发布这个事件;
* ContextClosedEvent:关闭容器会发布这个事件;
* 4)、发布一个事件:
* applicationContext.publishEvent();
1、自定义监听器
定义MyApplicationListener 类并实现ApplicationListener<ApplicationEvent>接口
用@Component装配到Spring容器中
package com.atguigu.ext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
public class MyApplicationListener implements ApplicationListener<ApplicationEvent> {
//当容器中发布此事件以后,方法触发
@Override
public void onApplicationEvent(ApplicationEvent event) {
// TODO Auto-generated method stub
System.out.println("收到事件:"+event);
}
}或者使用@EventListener注解来定义一个监听器
注:@EventListener(classes={ApplicationEvent.class})中classes监听的类可以是多个,以逗号分开
package com.atguigu.ext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@EventListener(classes={ApplicationEvent.class})
public void listen(ApplicationEvent event){
System.out.println("UserService。。监听到的事件:"+event);
}
}2、定义配置类,加载IOC容器
@ComponentScan("com.atguigu.ext")
@Configuration
public class ExtConfig {
@Bean
public Blue blue(){
return new Blue();
}
}3、测试
自定义发布事件使用了匿名内部类
package com.atguigu.test;
import org.junit.Test;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.atguigu.ext.ExtConfig;
public class IOCTest_Ext {
@Test
public void test01(){
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ExtConfig.class);
//发布事件;
applicationContext.publishEvent(new ApplicationEvent(new String("我发布的事件")) {
});
applicationContext.close();
}
}结果:


说明:spring自己有自定义监听器,如容器有刷新和关闭事件发布就会被监听到;自己定义了监听器并装配到spring容器中,一旦有相关事件发布也可以被监听到。
到此这篇关于Spring监听器之ApplicationListener原理及源码深度解析的文章就介绍到这了,更多相关Spring ApplicationListener原理内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
- Spring事件监听器ApplicationListener的使用与原理分析
- Spring中ApplicationListener的使用解析
- SpringBoot中的ApplicationListener事件监听器使用详解
- SpringBoot中ApplicationEvent和ApplicationListener用法小结
- Spring ApplicationListener的使用详解
- SpringMVC事件监听ApplicationListener实例解析
- Spring ApplicationListener监听器用法详解
- springBoot的事件机制GenericApplicationListener用法解析
相关文章
IDEA提示:Boolean method ‘xxx‘ is always&nb
这篇文章主要介绍了IDEA提示:Boolean method ‘xxx‘ is always inverted问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-08-08
如何使用nexus在局域网内搭建maven私服及idea的使用
这篇文章主要介绍了如何使用nexus在局域网内搭建maven私服及idea的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-11-11
SpringBoot内置服务器(Tomcat/Jetty/Undertow)切换的操作指南
本文介绍了如何在SpringBoot中切换内置服务器(Tomcat、Jetty和Undertow),分析了它们的核心差异和适用场景,通过修改pom.xml和application.yml文件,可以实现服务器的切换和自定义配置,最后给出了选型建议和注意事项,需要的朋友可以参考下2026-03-03
MyBatis注解方式之@Update/@Delete使用详解
这篇文章主要介绍了MyBatis注解方式之@Update/@Delete使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-11-11


最新评论