/**
* Set the config locations for this application context.//未应用上下文设置资源路径
* <p>If not set, the implementation may use a default as appropriate.//如果未设置,则实现可以根据需要使用默认值。
*/
public void setConfigLocations(String... locations) {
if (locations != null) {//非空
Assert.noNullElements(locations, "Config locations must not be null");//断言保证locations的每个元素都不为null
this.configLocations = new String[locations.length];
for (int i = 0; i < locations.length; i++) {
this.configLocations[i] = resolvePath(locations[i]).trim();//去空格,很好奇resolvePath做了什么事情?
}
}
else {
this.configLocations = null;
}
}
复制代码
进入resolvePath()方法看看:
/**
* 解析给定的资源路径,必要时用相应的环境属性值替换占位符,应用于资源路径配置。
* Resolve the given path, replacing placeholders with corresponding
* environment property values if necessary. Applied to config locations.
* @param path the original file path
* @return the resolved file path
* @see org.springframework.core.env.Environment#resolveRequiredPlaceholders(String)
*/
protected String resolvePath(String path) {
return getEnvironment().resolveRequiredPlaceholders(path);
}
复制代码
进入getEnvironment()看看:
/**
* {@inheritDoc}
* <p>If {@code null}, a new environment will be initialized via
* {@link #createEnvironment()}.
*/
@Override
public ConfigurableEnvironment getEnvironment() {
if (this.environment == null) {
this.environment = createEnvironment();
}
return this.environment;
}
复制代码
/**
* Prepare this context for refreshing, setting its startup date and
* active flag as well as performing any initialization of property sources.
*/
protected void prepareRefresh() {
this.startupDate = System.currentTimeMillis();//设置容器启动时间
this.closed.set(false);//容器关闭标志,是否关闭?
this.active.set(true);//容器激活标志,是否激活?
if (logger.isInfoEnabled()) {//运行到这里,控制台就会打印当前容器的信息
logger.info("Refreshing " + this);
}
// 空方法,由子类覆盖实现,初始化容器上下文中的property文件
initPropertySources();
//验证标记为必需的所有属性均可解析,请参阅ConfigurablePropertyResolver#setRequiredProperties
getEnvironment().validateRequiredProperties();
//允许收集早期的ApplicationEvents,一旦多播器可用,即可发布...
this.earlyApplicationEvents = new LinkedHashSet<ApplicationEvent>();
}
复制代码
控制台输出:
三月 22, 2018 4:21:13 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@96532d6: startup date [Thu Mar 22 16:21:09 CST 2018]; root of context hierarchy
复制代码
第九步:
进入obtainFreshBeanFactory()方法:
/**
* 告诉子类刷新内部bean工厂(子类是指AbstractApplicationContext的子类,我们使用的是ClassPathXmlApplicationContext)
* Tell the subclass to refresh the internal bean factory.
*/
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
refreshBeanFactory();//刷新Bean工厂,如果已经存在Bean工厂,那就关闭并销毁,再创建一个新的bean工厂
ConfigurableListableBeanFactory beanFactory = getBeanFactory();//获取新创建的Bean工厂
if (logger.isDebugEnabled()) {
logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);//控制台打印
}
return beanFactory;
}
复制代码
进入refreshBeanFactory()方法:
/**
* 该实现执行该上下文的基础Bean工厂的实际刷新,关闭以前的Bean工厂(如果有的话)以及为该上下文的生命周期的下一阶段初始化新鲜的Bean工厂。
* This implementation performs an actual refresh of this context's underlying
* bean factory, shutting down the previous bean factory (if any) and
* initializing a fresh bean factory for the next phase of the context's lifecycle.
*/
@Override
protected final void refreshBeanFactory() throws BeansException {
if (hasBeanFactory()) {//如果已有bean工厂
destroyBeans();//销毁
closeBeanFactory();//关闭
}
try {
DefaultListableBeanFactory beanFactory = createBeanFactory();//创建一个新的bean工厂
beanFactory.setSerializationId(getId());//为序列化目的指定一个id,如果需要,可以将此BeanFactory从此id反序列化回BeanFactory对象。
//定制容器,设置启动参数(bean可覆盖、循环引用),开启注解自动装配
customizeBeanFactory(beanFactory);
将所有BeanDefinition载入beanFactory中,此处依旧是模板方法,具体由子类实现
loadBeanDefinitions(beanFactory);
//beanFactory同步赋值
synchronized (this.beanFactoryMonitor) {
this.beanFactory = beanFactory;
}
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
复制代码