spring扩展点四:SmartInitializingSingleton 补充

本文探讨了Spring事件监听机制中SmartInitializingSingleton的作用,通过一个demo演示其在@EventListener注解解析过程中的应用。核心在于processBean方法,该方法在处理@EventListener和接口声明的事件监听器时被调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前面有笔记,记录过SmartInitializingSingleton这个扩展点的使用,可以参考这篇博客 spring扩展点四:SmartInitializingSingleton的应用

这篇博客是我自己写的一个demo,要验证这个扩展机制,但是最近在看spring事件监听机制的时候,看到了对这个扩展点的应用
在这里插入图片描述
EventListenerMethodProcessor ,就是这个bean,我们先不说这个bean是什么注入到spring容器中的,会在后面事件监听器源码的博客中,单独记录,这篇博客主要想说明的是:在spring事件监听机制中,如果我通过@EventListener来声明一个事件监听器,那他就是通过SmartInitializingSingleton这个扩展机制来完成解析的

org.springframework.context.event.EventListenerMethodProcessor#afterSingletonsInstantiated
/**
 * 这个方法也是spring扩展点之一
 */
@Override
public void afterSingletonsInstantiated() {
   
   
	/**
	 * 1.获取到所有的eventListenerFactory,理论上就是DefaultEventListenerFactory
	 */
	List<EventListenerFactory> factories = getEventListenerFactories();
	ConfigurableApplicationContext context = getApplicationContext();
	/**
	 * 2.获取到spring容器中,所有的beanName,依次遍历
	 */
	String[] beanNames = context.getBeanNamesForType(Object.class);
	for (String beanName : beanNames) {
   
   
		if (!ScopedProxyUtils.isScopedTarget(beanName)) {
   
   
			Class<?> type = null;
			try {
   
   
				type = AutoProxyUtils.determineTargetClass(context.getBeanFactory(), beanName);
			}
			catch (Throwable ex) {
   
   
				// An unresolvable bean type, probably from a lazy bean - let's ignore it.
				if (logger.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值