本文主要是介绍二、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,jndiPropertiesinitPropertySources();// 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();
}
这篇关于二、Spring源码学习之prepareRefresh方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!