SpringIOC源码解析之--如何实现多播器的异步多播的(ApplicationEventMulticaster)

支持异步多播

本文主要关注的代码为org.springframework.context.support.AbstractApplicationContext#refresh方法中的initApplicationEventMulticaster(), registerListeners()这两行代码,

1. 初始化一个事件多播器

//org.springframework.context.support.AbstractApplicationContext#refresh			
// Initialize event multicaster for this context.
initApplicationEventMulticaster();

2. 注册一个监听器--将一个监听事件注册到上面的多播器里面去

// Check for listener beans and register them.
registerListeners();

如下图所示:

SpringIOC源码解析之--如何实现多播器的异步多播的(ApplicationEventMulticaster)_第1张图片

主要解决的问题:如何让spring支持事件多播器的异步多播。

如何实现:自己定义一个多播器,并给bean命名为applicationEventMulticaster:

@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 复制成我们自定义的类实例。

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给我们预定的类的名字。
/**
 * 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就不为空了 ,就会走到第一个异步多播的路径。

@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,ioc,spring,ioc,多播器,事件监听)