背景
在上一篇本人记录了 Spring boot中BeanDefination扫描流程.下面自然就是Bean是如何实例化和初始化的。本文目的在于记录近期阅读Spring boot源码所得。
说明
其实Bean的初始化依然属于整个Spring boot启动流程中refresh阶段(AbstractApplicationContext.refresh())的一部分,而且refresh阶段,进行了很多次Bean初始化,每个阶段都会有。在此,本文只记录常见的单例Bean的初始化流程,并且对于很多源码实现细节,本人没有仔细深究,所以会有很多不足之处。
流程
- 开始
在Spring boot启动中,经常会发现BeanFactory. getBean(String name)调用,这个方法就是一切梦的开始。
所有的getBean方法指向doGetBean(final String name, @Nullable final Class requiredType, @Nullable final Object[] args, boolean typeCheckOnly)。