spring 事件监听器原理分析

本文深入解析Spring框架中事件监听机制的实现过程,包括监听器的注册、事件的发布及派发流程,通过实例代码演示如何自定义事件监听器并集成到Spring容器中。

先搭建源码分析环境,这里引用的是org.springframework4.3.12.RELEASE版本分析的,debug之前github上下载相关源码-地址 除了在上述代码中打上断点之外,增加EventListenerMethodProcessor的afterSingletonsInstantiated方法打上断点,AbstractApplicationContext的initApplicationEventMulticaster方法和registerListeners上打上断点。

@ComponentScan("com.atguigu.ext")
@Configuration
public class ExtConfig {
   
   
	
	@Bean
	public Blue blue(){
   
   
		return new Blue();
	}

}
@Component
public class MyApplicationListener implements ApplicationListener<ApplicationEvent> {
   
   

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

}
@Service
public class UserService {
   
   
	
	@EventListener(classes={
   
   ApplicationEvent.class})
	public void 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值