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

Spring5源码之二——refresh方法之prepareRefresh()

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

Spring5源码之二——refresh方法之prepareRefresh()

        书接前文:Spring5源码之一(Spring调试起点)

        本文主要解析AbstractApplicationContext类中refresh()方法中prepareRefresh()子方法的具体实现。废话不多说,直接上源码。

            
			prepareRefresh();

(1)prepareRefresh()调用自身(AbstractApplicationContext类)的prepareRefresh()方法,具体实现如下:

	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());
			}
		}

		// 预留接口,便于子类功能扩展
		initPropertySources();

		// 1、检查是否存在环境变量如果存在直接用,不存在重更新获取
		// 2、创建并获取环境对象、验证需要的属性文件是否符合
		getEnvironment().validateRequiredProperties();

		// 判断刷新前的应用程序监听器集合是否为空,如果为空,则将监听器添加到此集合中
		if (this.earlyApplicationListeners == null) {
			this.earlyApplicationListeners = new linkedHashSet<>(this.applicationListeners);
		}
		else {
			this.applicationListeners.clear();
			this.applicationListeners.addAll(this.earlyApplicationListeners);
		}
		// 创建刷新前的监听事件集合
		this.earlyApplicationEvents = new linkedHashSet<>();
	}

(2)在(1)中initPropertySources()方法,默认无实现,主要用于业务拓展,比如:可以在Spring启动时内置一些环境变量。

protected void initPropertySources() {
		// For subclasses: do nothing by default.
	}

(3)在(1)中getEnvironment()方法,实现如下。

	@Override
	public ConfigurableEnvironment getEnvironment() {
		if (this.environment == null) {
			this.environment = createEnvironment();//创建ConfigurableEnvironment 新环境
		}
		return this.environment;
	}


    protected ConfigurableEnvironment createEnvironment() {
		return new StandardEnvironment();
	}

(4)在(1)中validateRequiredProperties()方法,调用AbstractEnvironment类中validateRequiredProperties()的方法。

	@Override
	public void validateRequiredProperties() throws MissingRequiredPropertiesException {
		this.propertyResolver.validateRequiredProperties();
	}

        最终,调用到AbstractPropertyResolver类的validateRequiredProperties()方法

    //验证setRequiredProperties指定的每个属性是否存在并解析为非null值
    @Override
	public void validateRequiredProperties() {
		MissingRequiredPropertiesException ex = new MissingRequiredPropertiesException();
		for (String key : this.requiredProperties) {
			if (this.getProperty(key) == null) {
				ex.addMissingRequiredProperty(key);
			}
		}
		if (!ex.getMissingRequiredProperties().isEmpty()) {
			throw ex;
		}
	}

 (5)最后判断前置监听是否为null,若为null则新增,部位null则清空之前的监听,添加现在的监听。并初始化前置事件集合。

// 判断刷新前的应用程序监听器集合是否为空,如果为空,则将监听器添加到此集合中
		if (this.earlyApplicationListeners == null) {
			this.earlyApplicationListeners = new linkedHashSet<>(this.applicationListeners);
		}
		else {
			this.applicationListeners.clear();
			this.applicationListeners.addAll(this.earlyApplicationListeners);
		}
		// 创建刷新前的监听事件集合
		this.earlyApplicationEvents = new linkedHashSet<>();

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/666398.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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