spring refresh()方法详解1之prepareRefresh()

本文详细探讨了Spring的refresh()方法中的prepareRefresh()步骤,包括initPropertySources的用法和示例,以及getEnvironment和validateRequiredProperties的解释。通过自定义ClassPathXmlApplicationContext并重写initPropertySources方法,展示了如何在容器启动前进行自定义操作。同时,介绍了getEnvironment方法的作用和validateRequiredProperties在属性验证中的角色。
摘要由CSDN通过智能技术生成

1.方法代码如下:

	/**
	 * 准备工作
	 */
	protected void prepareRefresh() {
		//记录初始化开始时间
		this.startupDate = System.currentTimeMillis();
		this.closed.set(false);	//context是否关闭的标志,设置为false
		this.active.set(true);	//context是否激活的标志,设置为true

		if (logger.isDebugEnabled()) {	//记录日志
			if (logger.isTraceEnabled()) {
				logger.trace("Refreshing " + this);
			}
			else {
				logger.debug("Refreshing " + getDisplayName());
			}
		}

		//留给子类实现的空方法
		initPropertySources();

		 /** AbstractPropertyResolver类的requiredProperties是个集合,
		  * 在下面的validateRequiredProperties方法中,都要拿requiredProperties中的元素作为key去检查是否存在对应的环境变量,
		  * 如果不存在就抛出异常
		  */
		getEnvironment().validateRequiredProperties();

		// 初始化earlyApplicationListeners容器 --LinkedHashSet
		if (this.earlyApplicationListeners == null) {
			this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
		}
		
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值