获取单例
单例的获取是从缓存中获取,这里我们说缓存中没有时需要新建,Spring使用getSingleton的重载方法(又是模板方法模式)实现bean的加载过程。
我们看一下DefaultSingletonBeanRegistry#getSingleton方法。
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
// 检查beanName不能为空
Assert.notNull(beanName, "'beanName' must not be null");
// 全局加锁
synchronized (this.singletonObjects) {
// 读取缓存数据
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
if (this.singletonsCurrentlyInDestruction) {
throw new BeanCreationNotAllowedException(beanName,
"Singleton bean creation not allowed while the 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<Exception>();
}
try {
// 初始化
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 != NULL_OBJECT ? singletonObject : null);
}
}
这个代码中其实是使用了回调方法(beforeSingletonCreation、afterSingletonCreation),使得程序可以在单例创建的前后做一些准备和处理操作,而真正的获取单例bean方法其实并不是在此方法中实现的,其实现逻辑是在ObjectFactory类型的实例singletonFactory中实现的。而这些准备及处理操作包括以下内容。
- 检查缓存是否已经加载过
- 若没有加载,则记录beanName的正在加载状态
- 加载单例前记录加载状态
这个beforeSingletonCreation方法并不是一个空实现,里面没有任何逻辑,但其实不是,这个函数中做了一个很重要的操作:记录加载状态,也就是通过this.singletonsCurrentlyInCreation.add(beanName)将正要创建的bean记录在缓存中,这样就可以对循环依赖做检测:
- 4.通过调用参数传入的ObjectFactory的个体Object方法实例化bean。
- 5.加载单例后的处理方法调用
同步骤3的加载状态相似,当bean加载结束后需要移除缓存中对该bean的正在加载状态的记录。
- 6.将结果记录至缓存并删除加载bean过程中所记录的各种辅助状态
- 7.返回处理结果。
虽然我们现在已经从外部了解了加载bean的逻辑架构,但现在我们还并没有开始对bean加载功能的探索,之前提到过,bean的加载逻辑其实是在传入的ObjectFactory类型的参数singletonFactory中定义的,我们反推参数的获取,得到如下代码:
sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
@Override
public Object getObject() throws BeansException {
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;
}
}
});
以上代码在:AbstractBeanFactory#doGetBean方法中
ObjectFactory的核心部分其实只是调用了createBean的方法,所以我们还要到createBean方法中去追寻真理。