本文主要是介绍Spring中ApplicationListener实现监听原理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- ApplicationListener使用方式
- ApplicationListener实现原理
- 1.引入并实例化时机
- 2.作用时机
- 3.发布事件,生效
- 总结
ApplicationListener使用方式
package com.cyl.listener;import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;@Component
public class CylOrderFistListener implements ApplicationListener {@Overridepublic void onApplicationEvent(ApplicationEvent event) {System.out.println(event);}
}
ApplicationListener实现原理
ApplicationListener主要是通过ApplicationListenerDetector类实现的,是一个bean的后置处理器,主要识别出所有实现了ApplicationListener的对象,然后注册到spring容器的监听器属性中
后续将通过new AnnotationConfigApplicationContext为入口调试代码去讲解ApplicationListenerDetector的作用机制。
package com.cyl;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Test {public static void main(String[] args) {// 创建一个Spring容器AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();context.register(AppConfig.class);context.refresh();}
}
1.引入并实例化时机
当执行容器的refresh方法时,会执行registerBeanPostProcessors方法,该方法会将将扫描到的BeanPostProcessors实例化并排序,并添加到BeanFactory的beanPostProcessors属性中去
最终会调用到org.springframework.context.support.PostProcessorRegistrationDelegate#registerBeanPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, org.springframework.context.support.AbstractApplicationContext),最后一步会将ApplicationListenerDetector实例化,添加bean工厂的后置处理器列表内。
2.作用时机
在实例化bean的后置处理器后,容器接下来会实例化所有非懒加载的bean对象,即执行org.springframework.context.support.AbstractApplicationContext#finishBeanFactoryInitialization,在实例化所有懒加载所有对象时,ApplicationListenerDetector作为bean的后置处理器会处理所有的非懒加载的bean对象,判断该对象是否实现了ApplicationListener,若是则注册到容器的监听器列表中
ApplicationListenerDetector实现了MergedBeanDefinitionPostProcessor接口,重写了
postProcessMergedBeanDefinition和postProcessAfterInitialization,两个方法分别在bean的实例化后和初始化后执行
postProcessMergedBeanDefinition该方法是在bean生命周期的实例化后执行,该方法的逻辑判断目前的bean是否实现了ApplicationListener接口,若是则记录到ApplicationListenerDetector对象的singletonNames属性中
@Overridepublic void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {if (ApplicationListener.class.isAssignableFrom(beanType)) {this.singletonNames.put(beanName, beanDefinition.isSingleton());}}
postProcessAfterInitialization该方法是在bean生命周期的初始化后执行,主要是bean初始化后判断属性singletonNames是否有当前beanName,若有则注册到容器的监听器列表内
@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) {if (bean instanceof ApplicationListener) {// potentially not detected as a listener by getBeanNamesForType retrievalBoolean flag = this.singletonNames.get(beanName);if (Boolean.TRUE.equals(flag)) {// singleton bean (top-level or inner): register on the flythis.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 eventslogger.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;}
3.发布事件,生效
容器初始化最后阶段,即执行org.springframework.context.support.AbstractApplicationContext#finishRefresh
会发布事件并通知所有容器内的监听器最终效果图为:
总结
ApplicationListener实现监听的原理就是使用了ApplicationListenerDetector类,这是一个bean后置处理器,在容器启动中,ApplicationListenerDetector先被实例化,然后实例化其他非懒加载的bean对象时,ApplicationListenerDetector会作用于这些非懒加载的bean对象,判断对象是否实现了ApplicationListener接口,若实现了则注册到容器的监听器列表中。待容器内发布相关事件时,获取到所有监听该事件的监听器,执行对应的监听方法
这篇关于Spring中ApplicationListener实现监听原理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!