我们在 七、Spring 源码解析之 bean 的加载流程二:从缓存中获取单例 bean 文章中讲解了从缓存中获取单例的过程,代码如下:

protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
//提取对应的 beanName
final String beanName = transformedBeanName(name);
Object bean;
//直接尝试从缓存中获取或者 singletonFactories 中的 ObjectFactory 中获取
Object sharedInstance = getSingleton(beanName);
if (sharedInstance != null && args == null) {
.....
} else {
....
// Create bean instance.
// 实例化依赖的 bean 后便可以实例化 mbd 本身了
// Singleton 模式的实例创建
if (mbd.isSingleton()) {
sharedInstance = getSingleton(beanName, () -> {
try {
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
});
.....
}
....
return (T) bean;
}

  那么,若缓存中不存在已经加载的单例 bean ,就需要从头开始 bean 的加载过程了,而 Spring 中使用 ​​DefaultSingletonBeanRegistry#getSingleton(String beanName, ObjectFactory<?> singletonFactory)​​ 的重载方法实现了 bean 的加载过程,代码如下:

public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(beanName, "Bean name must not be null");
//全局变量需要同步
synchronized (this.singletonObjects) {
//首先检查对应的 bean 是否已经加载过,因为 singleton 模式其实就是复用已创建的 bean,所以这一步是必须的
Object singletonObject = this.singletonObjects.get(beanName);
// 为空才可以进行的单例 bean 的初始化
if (singletonObject == null) {
if (this.singletonsCurrentlyInDestruction) {
throw new BeanCreationNotAllowedException(beanName,
"Singleton bean creation not allowed while singletons of this factory are in destruction " +
"(Do not request a bean from a BeanFactory in a destroy method implementation!)");
}
if (logger.isDebugEnabled()) {
logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
}
beforeSingletonCreation(beanName);
boolean newSingleton = false;
boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
if (recordSuppressedExceptions) {
this.suppressedExceptions = new LinkedHashSet<>();
}
try {
// 初始化 bean
singletonObject = singletonFactory.getObject();
newSingleton = true;
}
catch (IllegalStateException ex) {
// Has the singleton object implicitly appeared in the meantime ->
// if yes, proceed with it since the exception indicates that state.
singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
throw ex;
}
}
catch (BeanCreationException ex) {
if (recordSuppressedExceptions) {
for (Exception suppressedException : this.suppressedExceptions) {
ex.addRelatedCause(suppressedException);
}
}
throw ex;
}
finally {
if (recordSuppressedExceptions) {
this.suppressedExceptions = null;
}
afterSingletonCreation(beanName);
}
if (newSingleton) {
// 加入缓存
addSingleton(beanName, singletonObject);
}
}
return singletonObject;
}
}

上述代码其实是使用了回调方法,使得程序可以再单例创建的前后做一些准备及处理操作,而真正的获取单例 bean 的方法其实并不是在此方法中实现的,其实现逻辑是在 ​​ObjectFactory​​​ 类型的实例 ​​singletonFactory​​ 中实现的。而这些准备及处理操作包括如下内容:

  1. 检查缓存是否已经加载过
  2. 若没有加载,则记录 beanName 的正在加载状态
    这个功能主要靠​​​beforeSingletonCreation(beanName)​​​ 方法实现,通过​​this.singletonsCurrentlyInCreation.add(beanName)​​ 将当前正要创建的 bean 记录在缓存中,这样可以对循环依赖进行检测。
/**
* 加载单例前记录加载状态
* 通过 this.singletonsCurrentlyInCreation.add(beanName) 将当前正要创建的 bean 记录在缓存中,这样可以对循环依赖
* 进行检查
*/
protected void beforeSingletonCreation(String beanName) {
if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.add(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
}
  1. 通过调用参数传入的 ​​ObjectFactory​​ 类型的实例的 ​​getObject()​​ 方法实例化 bean。
  2. 加载单例后的处理方法
    同步骤2的记录加载状态相似,当 bean 加载结束后需要移除缓存中对该 bean 的正在加载状态的记录。
protected void afterSingletonCreation(String beanName) {
if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.remove(beanName)) {
throw new IllegalStateException("Singleton '" + beanName + "' isn't currently in creation");
}
}
  1. 将结果记录至缓存并删除加载 bean 过程中记录的各种辅助状态。
protected void addSingleton(String beanName, Object singletonObject) {
synchronized (this.singletonObjects) {
this.singletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
this.earlySingletonObjects.remove(beanName);
this.registeredSingletons.add(beanName);
}
}
  1. 返回处理结果

   在第3步中我们提到 通过调用参数传入的 ​​ObjectFactory​​ 类型的实例的 ​​getObject()​​ 方法实例化 bean,而这里的参数 ​​singletonFactory​​​ 是在 ​​AbstractBeanFactory#doGetBean()​​ 方法中传入的,代码如下:

protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType..)
...
sharedInstance = getSingleton(beanName, () -> {
try {
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
});
...
}

从这里看到,参数 ​​singletonFactory​​​ 就是 ​​createBean(beanName, mbd, args)​​​ 方法创建返回的,也是我们下一篇文章所有探索的 bean 的加载逻辑:十、Spring 源码解析之 bean 的加载流程五: 准备创建bean。