ApplicationListener实现原理

文章详细介绍了在Spring框架中,如何自定义一个实现ApplicationListener接口的类来监听特定事件。通过@Component注解确保bean的实例化,然后在AbstractApplicationContext#refresh方法的bean初始化过程中,应用BeanPostProcessor后置处理器。关键在于ApplicationListenerDetector检测并注册监听器,确保当发布特定事件时,自定义的监听器能够被调用执行onApplicationEvent方法。此外,文章还提及了非单例bean作为监听器的限制以及SPI和@Bean的注入方式。

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

自定义一个实现ApplicationListener接口的类:

@Component
public class MyApplicationListener implements ApplicationListener<MyApplicationEvent> {
    @Override
    public void onApplicationEvent(MyApplicationEvent event) {
        System.out.println("我是测试监听器的类方法:"+event.getSource());
    }
}

这里使用了@Component注解,此注解下节分析;

然后再自定义一个触发事件:

public class MyApplicationEvent extends ApplicationEvent {
    public MyApplicationEvent(Object source) {
        super(source);
    }
}

要达到的效果就是,当请求发布的事件是MyApplicationEvent事件的时候,就需要调用自定义的监听器;

执行结果:

 可以看到执行的结果就是想要的结果。那么代码是如何实现这一点的呢?

这里只看关键代码,首先要找到AbstractApplicationContext#refresh的方法,其中最后第二步实例化方法是finishBeanFactoryInitialization(beanFactory)方法,它是通过getBean(beanName)来实例化剩余的单利的;

在此过程中,会调用如下方法:

	@Override
	public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
			throws BeansException {

		Object result = existingBean;
		for (BeanPostProcessor processor : getBeanPostProcessors()) {
			Object current = processor.postProcessAfterInitialization(result, beanName);
			if (current == null) {
				return result;
			}
			result = current;
		}
		return result;
	}

看方法名称可知,其实初始化后应用bean的后置处理器的方法;

即调用了ApplicationListenerDetectorBeanPostProcessor的后置处理器,它实现了MergedBeanDefinitionPostProcessor接口,

注(在此之前已经执行):

所以在这之前,它会在doCreateBean的实例化之后调用其后置处理器的postProcessMergedBeanDefinition()方法,此处是将自定义的监听器添加到singletonNames中。

然后接着上面继续,它后置方法postProcessAfterInitialization()

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) {
		if (bean instanceof ApplicationListener) {
			// potentially not detected as a listener by getBeanNamesForType retrieval
			Boolean flag = this.singletonNames.get(beanName);
			if (Boolean.TRUE.equals(flag)) {
				// singleton bean (top-level or inner): register on the fly
				this.applicationContext.addApplicationListener((ApplicationListener<?>) bean);
			}
			else if (Boolean.FALSE.equals(flag)) {
				if (logger.isWarnEnabled() && !this.applicationContext.containsBean(beanName)) {
					// inner bean with other scope - can't reliably process events
					logger.warn("Inner bean '" + beanName + "' implements ApplicationListener interface " +
							"but is not reachable for event multicasting by its containing ApplicationContext " +
							"because it does not have singleton scope. Only top-level listener beans are allowed " +
							"to be of non-singleton scope.");
				}
				this.singletonNames.remove(beanName);
			}
		}
		return bean;
	}

此时,先判断了一下对象是否是ApplicationListener类型,如果是,则添加到this.defaultRetriever.applicationListeners变量中,取值的时候,则会根据具体的类型进行判断

		for (ApplicationListener<?> listener : listeners) {
			if (supportsEvent(listener, eventType, sourceType)) {
				if (retriever != null) {
					filteredListeners.add(listener);
				}
				allListeners.add(listener);
			}
		}

此时,就会将根据事件类型,添加相应的监听器,然后调用对应的onApplicationEvent方法。

当然,此处也可以使用spi的形式注入或者@Bean的形式都可以;

监听器可以实现线程上下文的的热更新等功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值