栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring系列之IOC容器实例化过程七

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring系列之IOC容器实例化过程七

refresh方法中的initApplicationEventMulticaster、onRefresh、registerListeners

一个星期过去了,开心周末到来,又到了我们最喜欢的spring的解析环节,今天我们主要讲解三个方法,内容不会很多,请放心食用。

initApplicationEventMulticaster方法

作用: 为spring初始化事件多播器

贴上代码:

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

初始化 ApplicationEventMulticaster。如果上下文中没有定义,则使用 SimpleApplicationEventMulticaster。

containsLocalBean:

public interface HierarchicalBeanFactory extends BeanFactory {

	
	BeanFactory getParentBeanFactory();

	
	boolean containsLocalBean(String name);

}

返回本地 bean 工厂是否包含给定名称的 bean,忽略祖先上下文中定义的 bean。
这是containsBean的替代方法,忽略来自祖先 bean 工厂的给定名称的 bean

也就是这个方法只会从当前的工厂中去查找bean不会去父工厂中去查询。

onRefresh方法

初始化特定上下文子类中的其他特殊 bean

就是一个模版方法,用于给子类去扩展完成对其他特殊bean的初始化所使用,在Spring Boot中ServletWebServerApplicationContext#onRefresh()是有重写这个方法的:

@Override
	protected void onRefresh() {
		// 调用 GenericWebApplicationContext 初始化主题功能
		super.onRefresh();
		try {
			// 启动 Spring Boot 的嵌入式 Tomcat 服务器
			createWebServer();
		}
		catch (Throwable ex) {
			throw new ApplicationContextException("Unable to start web server", ex);
		}
	}
registerListeners方法

检查侦听器 bean 并注册它们

protected void registerListeners() {
		// Register statically specified listeners first.
		//首先注册静态指定的监听器(是特殊的事件监听器,而不是配置中的bean),先通过getApplicationListeners方法获取到ApplicationListener的set的集合,然后通过addApplicationListener(listener)注册监听器。
		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!
	// 获取beanfactory中ApplicationListener类型的bean,但是不会实例化这些bean,为了让后置处理器可以感知到它们
   // 再将这些bean注册到事件多播器中,真正完成事件的发布的时候是在bean的初始化过程中的后置处理器完成
		String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
		for (String listenerBeanName : listenerBeanNames) {
			getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
		}


		// Publish early application events now that we finally have a multicaster...
		//发布早期的事件
		Set earlyEventsToProcess = this.earlyApplicationEvents;
		this.earlyApplicationEvents = null;
		if (earlyEventsToProcess != null) {
			for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
				getApplicationEventMulticaster().multicastEvent(earlyEvent);
			}
		}
	}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/754813.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号