refresh()方法标志着spring容器启动过程开始,
它主要做两件事:
创建工厂、实例化bean对象。
ClassPathXmlApplicationContext
ClassPathXmlApplicationContext#ClassPathXmlApplicationContext(java.lang.String)
ClassPathXmlApplicationContext#ClassPathXmlApplicationContext(java.lang.String[], boolean, org.springframework.context.ApplicationContext)
AbstractApplicationContext#refresh
AbstractApplicationContext#obtainFreshBeanFactory
// 获取BeanFactory
// 加载BeanDefinition并注册到BeanDefinitionRegistry去
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
/**
* 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 {
// 判断是否有BeanFactory
if (hasBeanFactory()) {
// 销毁Beans
destroyBeans();
// 关闭BeanFactory
closeBeanFactory();
}
try {
// 实例化DefaultListableBeanFactory
DefaultListableBeanFactory beanFactory = createBeanFactory();
// 设置序列化ID
beanFactory.setSerializationId(getId());
// 定义BeanFactory的一些属性(是否允许覆盖、是否允许循环依赖)
customizeBeanFactory(beanFactory);
// ## 加载BeanDefinitions
loadBeanDefinitions(beanFactory);
this.beanFactory = beanFactory;
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
如果你不知道它执行的是哪个类中的实现,debug!
穷且益坚,不坠青云之志,
老当益壮,宁移白首之心。
与诸位共勉!