spring启动过程的追踪(六)——initApplicationEventMulticaster初始化事件广播器

initApplicationEventMulticaster初始化事件广播器

1、初始化应用事件广播器。这是观察者模式得典型应用。我们知道观察者模式由主题Subject和Observer组成。广播器相当于主题Subject,其包含多个监听器。当主题发生变化时会通知所有得监听器。

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.isDebugEnabled()) {
				logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
			}
		}
		else {
		//创建事件广播器
			this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
			//将事件广播器放入beanFactory
			beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
			if (logger.isDebugEnabled()) {
				logger.debug("Unable to locate ApplicationEventMulticaster with name '" +
						APPLICATION_EVENT_MULTICASTER_BEAN_NAME +
						"': using default [" + this.applicationEventMulticaster + "]");
			}
		}
	}

2、事件广播器生效时机

在finishRefresh完成spring容器刷新的地方,我们可以看到有应用事件广播器

protected void finishRefresh() {
		// Initialize lifecycle processor for this context.
		initLifecycleProcessor();

		// Propagate refresh to lifecycle processor first.
		getLifecycleProcessor().onRefresh();

		// 发布事件
		publishEvent(new ContextRefreshedEvent(this));

		// Participate in LiveBeansView MBean, if active.
		LiveBeansView.registerApplicationContext(this);
	}

进入publishEvent方法

@Override
	public void publishEvent(ApplicationEvent event) {
		Assert.notNull(event, "Event must not be null");
		if (logger.isTraceEnabled()) {
			logger.trace("Publishing event in " + getDisplayName() + ": " + event);
		}
		getApplicationEventMulticaster().multicastEvent(event);
		if (this.parent != null) {
			this.parent.publishEvent(event);
		}
	}

getApplicationEventMulticaster().multicastEvent(event);获取广播器,进行发布事件,此时的广播器就是之前初始化创建的SimpleApplicationEventMulticaster,看下事件发布的代码

	public void multicastEvent(final ApplicationEvent event) {
  001		for (final ApplicationListener listener : getApplicationListeners(event)) {
			Executor executor = getTaskExecutor();
			if (executor != null) {
				executor.execute(new Runnable() {
					@Override
					public void run() {
						listener.onApplicationEvent(event);
					}
				});
			}
			else {
   002		listener.onApplicationEvent(event);
			}
		}
	}

001、获取所有符合该事件的监听器
在这里插入图片描述
重点关注for循环里面的supportsEvent方法

protected boolean supportsEvent(ApplicationListener<?> listener,
			Class<? extends ApplicationEvent> eventType, Class<?> sourceType) {

		SmartApplicationListener smartListener = (listener instanceof SmartApplicationListener ?
				(SmartApplicationListener) listener : new GenericApplicationListenerAdapter(listener));
		return (smartListener.supportsEventType(eventType) && smartListener.supportsSourceType(sourceType));
	}

@Override
	public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
		Class<?> declaredEventType = resolveDeclaredEventType(this.delegate.getClass());
		if (declaredEventType == null || declaredEventType.equals(ApplicationEvent.class)) {
			Class<?> targetClass = AopUtils.getTargetClass(this.delegate);
			if (targetClass != this.delegate.getClass()) {
				declaredEventType = resolveDeclaredEventType(targetClass);
			}
		}
		return (declaredEventType == null || declaredEventType.isAssignableFrom(eventType));
	}

进入resolveDeclaredEventType方法
这里用到了泛型解析工具类GenericTypeResolver,可以获取到指定父接口的泛型具体类型,这个在工具类文章中有简单的使用介绍(泛型解析工具类的使用

static Class<?> resolveDeclaredEventType(Class<?> listenerType) {
		return GenericTypeResolver.resolveTypeArgument(listenerType, ApplicationListener.class);
	}

002、调用监听器的onApplicationEvent方法,将事件对象传给每一个监听器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值