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

Spring源码(四)Reflush的prepareRefresh()方法

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

Spring源码(四)Reflush的prepareRefresh()方法

Reflush的prepareRefresh方法
  • 前言
  • prepareFresh()方法
  • 总结

前言


前文将super方法与setConfigLocations方法都解释完毕,这里正式进入到了refresh方法了。

进入到refresh方法中,首先会有个锁,这个锁在super中已经定义好了,这是为了对refresh与destry操作保证其是原子性的。
本文介绍下prepareRefresh()方法。

prepareFresh()方法

此方法较为简单,所以直接进入这个方法看下到底有什么。

protected void prepareRefresh() {
		// Switch to active.
		this.startupDate = System.currentTimeMillis();
		this.closed.set(false);
		this.active.set(true);

		if (logger.isDebugEnabled()) {
			if (logger.isTraceEnabled()) {
				logger.trace("Refreshing " + this);
			}
			else {
				logger.debug("Refreshing " + getDisplayName());
			}
		}

		// Initialize any placeholder property sources in the context environment.
		initPropertySources();

		// Validate that all properties marked as required are resolvable:
		// see ConfigurablePropertyResolver#setRequiredProperties
		getEnvironment().validateRequiredProperties();

		// Store pre-refresh ApplicationListeners...
		if (this.earlyApplicationListeners == null) {
			this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
		}
		else {
			// Reset local application listeners to pre-refresh state.
			this.applicationListeners.clear();
			this.applicationListeners.addAll(this.earlyApplicationListeners);
		}

		// Allow for the collection of early ApplicationEvents,
		// to be published once the multicaster is available...
		this.earlyApplicationEvents = new LinkedHashSet<>();
	}

用一个图可以完全显示这个方法到底做了什么,如下图所示。

prepareReflush方法主要设置了容器开启时间、激活状态、关闭状态。然后到了initPropertySources()方法,这个方法进去发现是空的,这里是一个扩展点,我们测试下:

这里重新创建了一个MyClassPathXmlApplicationContext类,需要继承ClassPathXmlApplicationContext类,configLocations也需要设置,然后重写initPropertySources方法。这里一般设置一些属性,通常用
getEnvironment().setRequiredProperties(“”);在springMVC中,这里是MVC的重要扩展点。
getEnvironment().validateRequiredProperties();方法是看是否创建了enviroment,这里在setconfigLocations已经完成了环境创建操作,然后是validateRequiredProperties();这个方法主要做的就是验证需要的属性文件是否都已经放入环境中。
继续往下,初始化了监听器集合以及的监听事件集合。

总结

prepareReflush方法主要做了以下工作:

  前戏,做容器刷新前的准备工作
		 1、设置容器的启动时间
		 2、设置活跃状态为true
		 3、设置关闭状态为false
		 4、对initPropertySources扩展点
		 5、获取Environment对象,并验证属性文件是否都已经放入环境中
		 6、准备监听器和事件的集合对象,默认为空的集合
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1040583.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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