概述
这次是关于
refresh
里的两个方法prepareRefresh()
与obtainFreshBeanFactory()
;spring
版本为6.0.12
- prepareRefresh(准备刷新)
- 1.设置容器的启动时间
- 2.设置活跃状态为true
- 3.设置关闭状态为false
- 4.获取
Environmen
t对象,并加载当前系统的属性值到Environment
对象中 - 5.准备监听器和时间的集合对象,默认为空的集合
- obtainFreshBeanFactory
- 1.如果存在旧
BeanFactory
,则销毁 - 2.创建新的 BeanFactory (
DefaultListableBeanFactory
) - 3.解析xml/加载bean定义,注册bean定义到beanFactory(不初始化)
- 4.返回新的 BeanFactory (DefaultListableBeanFactory)
- 1.如果存在旧
相关链接
阅读之前,可以先浏览一下
源代码定位
根据下面的全类名,在
idea
中快速定位并打上断点
# 源代码快速定位
org.springframework.context.support.AbstractApplicationContext#refresh
# 两个重要方法
// Prepare this context for refreshing.
// 一.准备刷新
// 1.设置容器的启动时间
// 2.设置活跃状态为true
// 3.设置关闭状态为false
// 4.获取Environment对象,并加载当前系统的属性值到Environment对象中
// 5.准备监听器和时间的集合对象,默认为空的集合
prepareRefresh();
# ----------
// 二. 初始化 新的BeanFactory
// 1.如果存在旧 BeanFactory,则销毁
// 2.创建新的 BeanFactory (DefaultListableBeanFactory)
// 3.解析xml/加载bean定义,注册bean定义到beanFactory(不初始化)
// 4.返回新的 BeanFactory (DefaultListableBeanFactory)
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
两个方法
prepareRefresh 与 obtainFreshBeanFactory
prepareRefresh()
prepareRefresh 刷新之前的一些准备
// 方法快速定位
org.springframework.context.support.AbstractApplicationContext#prepareRefresh
上面的图片,涉及了上一篇
spring
源码四初始化好的一些对象,重要的源码已标注
obtainFreshBeanFactory()
见名知意,获取一个
beanFactory
// 源码快速定位
org.springframework.context.support.AbstractApplicationContext#obtainFreshBeanFactory
loadBeanDefinition(beanFactory)详解链接
结束
这两个方法相对简单,loadBeanDefinition(beanFactory)这个是单独一篇文章进行了说明