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

相关推荐

  1. Spring学习prepareRefresh方法

    2024-03-21 22:26:03       47 阅读
  2. 九、Spring学习initApplicationEventMulticaster方法

    2024-03-21 22:26:03       41 阅读
  3. 十一、Spring学习registerListeners方法

    2024-03-21 22:26:03       31 阅读
  4. 十、Spring学习onRefresh方法

    2024-03-21 22:26:03       35 阅读
  5. Spring() refresh () 方法

    2024-03-21 22:26:03       27 阅读
  6. Spring推断构造方法解析

    2024-03-21 22:26:03       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-21 22:26:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-21 22:26:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-21 22:26:03       82 阅读
  4. Python语言-面向对象

    2024-03-21 22:26:03       91 阅读

热门阅读

  1. 【LeetCode-46.全排列】

    2024-03-21 22:26:03       41 阅读
  2. RecyclerView万能适配器之BaseQuickAdapter方法详解

    2024-03-21 22:26:03       41 阅读
  3. Python PEP 8 代码风格指南

    2024-03-21 22:26:03       39 阅读
  4. Python电子邮件自动化实战案例

    2024-03-21 22:26:03       34 阅读
  5. 每日一题 第二十期 洛谷 烤鸡

    2024-03-21 22:26:03       45 阅读
  6. 如何让自己的前端知识更全面

    2024-03-21 22:26:03       38 阅读