SpringIOC源码解析之--如何实现多播器的异步多播的(ApplicationEventMulticaster)
2019-06-16 09:27
369 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/pengweismile/article/details/92363266
支持异步多播
本文主要关注的代码为org.springframework.context.support.AbstractApplicationContext#refresh方法中的initApplicationEventMulticaster(), registerListeners()这两行代码,
1. 初始化一个事件多播器
[code]//org.springframework.context.support.AbstractApplicationContext#refresh // Initialize event multicaster for this context. initApplicationEventMulticaster();
2. 注册一个监听器--将一个监听事件注册到上面的多播器里面去
[code]// Check for listener beans and register them. registerListeners();
如下图所示:
主要解决的问题:如何让spring支持事件多播器的异步多播。
如何实现:自己定义一个多播器,并给bean命名为applicationEventMulticaster:
[code]@Component(value = "applicationEventMulticaster") public class Myulticaster extends SimpleApplicationEventMulticaster{ public MyMulticaster () { setTaskExecutor(Executors.newSingleThreadExecutor()); } }
解释原理: 如下我们看到在以上的判断是否自定义了多播器的代码中,判断在ioc容器中是否包含如下名字的bean作为判断条件的,所以只要我们自定义一个bean命名为applicationEventMulticaster,并把异步支持的executor植入就行了。当我们自己定义了一个继承SimpleApplicationEventMulticaster命名为applicationEventMulticaster的bean时,就会给AbstractApplicationContext类1的this.applicationEventMulticaster 复制成我们自定义的类实例。
[code]protected void initApplicationEventMulticaster() { ConfigurableListableBeanFactory beanFactory = getBeanFactory(); //是否定义了自己的多播器 if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) { this.applicationEventMulticaster = beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class); if (logger.isTraceEnabled()) { logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]"); } } //没有定义自己的多播器就使用系统默认的多播器 else { this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory); beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster); if (logger.isTraceEnabled()) { logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " + "[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]"); } } }
如下图,applicationEventMulticaster就是 spring给我们预定的类的名字。
[code]/** * Name of the ApplicationEventMulticaster bean in the factory. * If none is supplied, a default SimpleApplicationEventMulticaster is used. * @see org.springframework.context.event.ApplicationEventMulticaster * @see org.springframework.context.event.SimpleApplicationEventMulticaster */ public static final String APPLICATION_EVENT_MULTICASTER_BEAN_NAME = "applicationEventMulticaster";
当在我们自定义的多播器中设置了executor时,下面的exeutor就不为空了 ,就会走到第一个异步多播的路径。
[code]@Override public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) { ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event)); for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) { Executor executor = getTaskExecutor(); if (executor != null) { executor.execute(() -> invokeListener(listener, event)); } else { invokeListener(listener, event); } } }
相关文章推荐
- Spring boot ApplicationEvent实现异步调用
- Spring源码解析之IoC容器系列的设计实现(IoC容器系列概况)
- Spring源码解析-IOC容器的实现-ApplicationContext
- spring 实现IOC过程源码解析<一>
- Spring IOC/BeanFactory/ApplicationContext的工作流程/实现原理/初始化/依赖注入源码详解
- 【Spring源码--IOC容器的实现】(三)BeanDefinition的载入和解析【I】
- 【Spring源码--IOC容器的实现】(三)BeanDefinition的载入和解析【II】
- Spring IOC 源码-ClassPathXmlApplicationContext-bean解析
- spring培训第一讲-ioc实现原理源码解析
- Spring源码情操陶冶-AbstractApplicationContext#initApplicationEventMulticaster
- Spring源码分析----IOC容器的实现(IoC容器的初始化过程(定位、载入解析、注册))
- Spring源码解析-IOC容器的实现
- Spring源码解析(一):Spring的IOC实现
- 深入解析Spring架构与设计原理 IOC实现原理
- (精)Spring IOC核心源码学习III:bean标签和自定义标签实现原理
- Spring如何实现IOC和AOP的,说出实现原理。
- spring源码学习之路---IOC实现原理(三)
- 【Spring源码解析】之IOC容器在Web容器中的启动
- Spring技术内幕——深入解析Spring架构与设计原理(一)IOC实现原理
- SpringIOC--初始化源码解析