Spring容器创建流程(7)事件派发机制

Spring事件驱动,监听机制。由三步分组成,事件,派发器,监听器。

事件:
spring事件的顶级父类是EventObject,通常情况下继承ApplicationEvent 自定义自己的事件。
在这里插入图片描述

事件派发器:
事件派发器,负责事件的派发,顶级父类为ApplicationEventPublisher,真正的事件派发机制在子类AbstractApplicationContext中实现,事件派发交由一个 ApplicationEventMulticaster 完成。

在这里插入图片描述

事件监听器:
监听事件发布器发布的事件,实现ApplicationListener,放入容器完成时间监听。

在这里插入图片描述

容器refresh总览:

		synchronized (this.startupShutdownMonitor) {
			// 设置环境,校验参数。
			prepareRefresh();
			
			// 创建BeanFactory(DefaultListableBeanFactor),加载bean定义信息。
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// 对BeanFactory设置	
			prepareBeanFactory(beanFactory);

			try {
				// 留给子类去实现,完成对beanFactory的自定义处理
				postProcessBeanFactory(beanFactory);

				// 调用beanFactory的后置处理器
				invokeBeanFactoryPostProcessors(beanFactory);

				// 注册bean后置处理器
				registerBeanPostProcessors(beanFactory);

				// 初始化messageSource,国际化支持
				initMessageSource();

				// 初始化事件派发器
				initApplicationEventMulticaster();

				// 留给子类实现,自定义处理
				onRefresh();

				// 注册事件监听器
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				finishRefresh();
			}

			catch (BeansException ex) {
				if (logger.isWarnEnabled()) {
					logger.warn("Exception encountered during context initialization - " +
							"cancelling refresh attempt: " + ex);
				}

				// Destroy already created singletons to avoid dangling resources.
				destroyBeans();

				// Reset 'active' flag.
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			}

			finally {
				// Reset common introspection caches in Spring's core, since we
				// might not ever need metadata for singleton beans anymore...
				resetCommonCaches();
			}
		}



initApplicationEventMulticaster

初始化事件事件派发器很简单,如果容器中有名为 applicationEventMulticaster 的派发器那么就用自定义的,否则直接创建一个 SimpleApplicationEventMulticaster

	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 + "]");
			}
		}
	}

onRefresh

方法留给子类实现

registerListeners

注册事件监听器

	protected void registerListeners() {
		// 注册已经事先指定容器中的Listener
		for (ApplicationListener<?> listener : getApplicationListeners()) {
			getApplicationEventMulticaster().addApplicationListener(listener);
		}

		// 注册其他的Listener
		String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
		for (String listenerBeanName : listenerBeanNames) {
			getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
		}

		// 发布一些事件
		Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
		this.earlyApplicationEvents = null;
		if (earlyEventsToProcess != null) {
			for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
				getApplicationEventMulticaster().multicastEvent(earlyEvent);
			}
		}
	}

事件如何发布

	@Override
	public void multicastEvent(ApplicationEvent event) {
		// 事件类型被封装为一个 ResolvableType 
		multicastEvent(event, resolveDefaultEventType(event));
	}

	@Override
	public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
		ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
		// getApplicationListeners(event, type) 获取注册的监听器
		for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {	
		// 如果派发器配置的executor,进行异步派发事件
			Executor executor = getTaskExecutor();
			if (executor != null) {
				executor.execute(() -> invokeListener(listener, event));
			}
			else {
				// 遍历监听器发布事件
				invokeListener(listener, event);
			}
		}
	}

如何获取监听事件

	protected Collection<ApplicationListener<?>> getApplicationListeners(
			ApplicationEvent event, ResolvableType eventType) {
		
		Object source = event.getSource();
		Class<?> sourceType = (source != null ? source.getClass() : null);
		// 事件类型和事件中的source封装为key用于做缓存 
		// retrieverCache = Map<ListenerCacheKey, ListenerRetriever>
		ListenerCacheKey cacheKey = new ListenerCacheKey(eventType, sourceType);

		// 查看缓存中,是否有发布过这个事件
		ListenerRetriever retriever = this.retrieverCache.get(cacheKey);
		if (retriever != null) {
			return retriever.getApplicationListeners();
		}
		
		if (this.beanClassLoader == null ||
				(ClassUtils.isCacheSafe(event.getClass(), this.beanClassLoader) &&
						(sourceType == null || ClassUtils.isCacheSafe(sourceType, this.beanClassLoader)))) {
			synchronized (this.retrievalMutex) {
				// 再次检测
				retriever = this.retrieverCache.get(cacheKey);
				if (retriever != null) {
					return retriever.getApplicationListeners();
				}
				retriever = new ListenerRetriever(true);
				// 直接拿注册的listener,listener的name进行beanFactory.getBean获取实例
				Collection<ApplicationListener<?>> listeners =
						retrieveApplicationListeners(eventType, sourceType, retriever);
				// 放入缓存
				this.retrieverCache.put(cacheKey, retriever);
				return listeners;
			}
		}
		else {
			// No ListenerRetriever caching -> no synchronization necessary
			return retrieveApplicationListeners(eventType, sourceType, null);
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值