Spring源码情操陶冶-AbstractApplicationContext#registerListeners

承接前文Spring源码情操陶冶-AbstractApplicationContext#onRefresh
约定web.xml配置的contextClass为默认值XmlWebApplicationContext

直接源码AbstractRefreshableWebApplicationContext#registerListeners

	/**
	 * Add beans that implement ApplicationListener as listeners.
	 * Doesn't affect other listeners, which can be added without being beans.
	 */
	protected void registerListeners() {
		// Register statically specified listeners first.
		for (ApplicationListener<?> listener : getApplicationListeners()) {
			getApplicationEventMulticaster().addApplicationListener(listener);
		}

		// Do not initialize FactoryBeans here: We need to leave all regular beans
		// uninitialized to let post-processors apply to them!
		//从DefaultListableFactory中获取ApplicationListener beans
		String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
		for (String listenerBeanName : listenerBeanNames) {
			//放入前文创建的SimpleApplicationEventMultlcaster中
			getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
		}

		// Publish early application events now that we finally have a multicaster...
		Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
		//设置earlyApplicationEvents集合为空,让finishRefresh()可执行剩下的ApplicationEvent
		this.earlyApplicationEvents = null;
		if (earlyEventsToProcess != null) {
			for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
				getApplicationEventMulticaster().multicastEvent(earlyEvent);
			}
		}
	}

注册ApplicationListener beans到广播集合中,并执行earlyApplicationEvents中的事件,一般为空

下节预告

Spring源码情操陶冶-AbstractApplicationContext#finishBeanFactoryInitialization

posted @ 2017-05-11 21:54  南柯问天  阅读(529)  评论(0编辑  收藏  举报