• 一:Spring源码解析之prepareRefresh()


    AbstractApplicationContext -> refresh()		
    
    • 1

    1. 功能概述:

    1. 设置容器启动时间
    2. 设置容器关闭标志位
    3. 设置激活标志位
    4. 记录轨迹日志
    5. 初始化属性资源(空,留给子类实现)
    6. 创建并获取环境对象,校验
    7. 准备监听器和事件的集合对象,默认为空

    2. 源码

    	/**
    	 * Prepare this context for refreshing, setting its startup date and
    	 * active flag as well as performing any initialization of property sources.
    	 */
    	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<>();
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    2.1 细节解析
    2.1.1 getEnvironment()

    AbstactApplicationContext.java

    @Override
    	public ConfigurableEnvironment getEnvironment() {
    		if (this.environment == null) {
    			this.environment = createEnvironment();
    		}
    		return this.environment;
    	}
    
    //============================
    protected ConfigurableEnvironment createEnvironment() {
    		return new StandardEnvironment();
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    类StandardEnvironment继承自AbstractEnvironment,创建对象,执行父类的构造方法

    public class StandardEnvironment extends AbstractEnvironment
    
    //=======================
    public AbstractEnvironment() {   
    customizePropertySources(this.propertySources);
    }
    
    //留给子类去扩展
    protected void customizePropertySources(M
    utablePropertySources propertySources) {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    StandardEnvironment.java

    // 父类定义的方法,子类扩展的接口
    	@Override
    	protected void customizePropertySources(MutablePropertySources propertySources) {
    		propertySources.addLast(
    				new PropertiesPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));
    		propertySources.addLast(
    				new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Debug截图:
    prepareRefresh

  • 相关阅读:
    研发项目管理系统对比:找到最适合的高效工具
    模拟电子技术(四)放大电路的频率响应
    .locked勒索病毒的最新威胁:如何恢复您的数据?
    [附源码]JAVA毕业设计科研项目审批管理系统(系统+LW)
    第3部分 静态路由
    首家上市的量子计算软件公司!Zapata AI拟完成SPAC交易
    Python学习基础笔记七——元组
    MyISAM 与 InnoDB 的区别是什么?
    RT-DETR 应用 BiFPN 结构 | 加权双向特征金字塔网络
    R数据分析:用R建立预测模型
  • 原文地址:https://blog.csdn.net/sbl19940819/article/details/127924276