二、Spring源码学习之prepareRefresh方法

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

		// Initialize any placeholder property sources in the context environment.
		//加载环境变量 后面可能会有占位符(${}) 需要替换实际的值
		//主要是加载 StandardServletEnvironment servletContextInitParams,servletConfigInitParams,jndiProperties
		initPropertySources();

		// Validate that all properties marked as required are resolvable:
		// see ConfigurablePropertyResolver#setRequiredProperties
		//校验环境变量必不可少的属性,如果缺少,则报错
		getEnvironment().validateRequiredProperties();

		// Store pre-refresh ApplicationListeners...
		//这里加这个判断的原因是因为 当是SpringMvc加载启动创建容器时,SpringMvc中会有一些提前创建好的监听器
		//如果是这样的话,会将SpringMvc及Spring中的监听器都放到applicationListeners中
		//再当前只是刷新Spring容器会走这个if判断
		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<>();
	}

initPropertySources()方法

protected void initPropertySources() {
	// For subclasses: do nothing by default.
	//提供给子类实现 这里是AbstractRefreshableWebApplicationContenxt
}

AbstractRefreshableWebApplicationContext#initPropertySources()方法

protected void initPropertySources() {
		//获取可配置的环境对象
		ConfigurableEnvironment env = getEnvironment();
		if (env instanceof ConfigurableWebEnvironment) {
			//初始化systemProperties:JVM中的参数,systemEnvironment:系统环境参数,servletContextInitParams,servletConfigInitParams这四个参数
			((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig);
		}
}

StandardServletEnvironment#initPropertySources()方法

public void initPropertySources(@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) {
		WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig);
}

WebApplicationContextUtils#initServletPropertySources()

public static void initServletPropertySources(MutablePropertySources sources,
			@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) {

		Assert.notNull(sources, "'propertySources' must not be null");
		//servletContextInitParams 指的是Servlet上下文中的参数
		String name = StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME;
		if (servletContext != null && sources.get(name) instanceof StubPropertySource) {
			sources.replace(name, new ServletContextPropertySource(name, servletContext));
		}
		//servletConfigInitParams 指的是Servlet的配置项中的参数
		name = StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME;
		if (servletConfig != null && sources.get(name) instanceof StubPropertySource) {
			sources.replace(name, new ServletConfigPropertySource(name, servletConfig));
		}
	}

getEnvironment()方法

public ConfigurableEnvironment getEnvironment() {
	if (this.environment == null) {
		this.environment = createEnvironment();
	}
	return this.environment;
}
//这里是创建标准环境对象
protected ConfigurableEnvironment createEnvironment() {
	return new StandardEnvironment();
}
  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值