概述
前文中介绍的finishBeanFactoryInitialization方法是IOC的核心,其中的getBean方法还没有讲解到,在这里继续展开解析。我们阅读源码时,要抓主次,先按主线下来,然后在主线里做支路的标记,等主线阅读完后,再回顾支路的标记慢慢进去展开,这样避免引起“"迷路现象",在源码中兜转一回不明白所以然。
在前文中,我们主要关注的是finishBeanFactoryInitialization里的preInstantiateSingletons方法,实例化所有剩余的非懒加载单例 bean,那如何实例化的呢?这得用到getBean这个方法。
getBean
AbstractBeanFactory类中的getBean方法,这里边有好几个getBean方法,我们主要看第一个用到的,源码如下:
@Override
public Object getBean(String name) throws BeansException {
return doGetBean(name, null, null, false);
}
doGetBean是主要做事的方法,那么多getBean方法只是给外部提供一个调用接口,查看doGetBean的源码:
/**
* Return an instance, which may be shared or independent, of the specified bean.
*/
@SuppressWarnings("unchecked")
protected T doGetBean(final String name, @Nullable final Class requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
//注释1. 获取 bean 名称,如果需要,去掉引用前缀(例如修饰符),还有将别名转换成最终 beanName
final String beanName = transformedBeanName(name);
Object bean;
// Eagerly check singleton cache for manually registered singletons.
// 注释2. 检查缓存中或者实例工厂是否有对应的实例或者从 singletonFactories 中的 ObjectFactory 中获取
Object sharedInstance = getSingleton(beanName);
// 注释3. 如果缓存中存在该beanName的实例
if (sharedInstance != null && args == null) {
if (logger.isTraceEnabled()) {
if (isSingletonCurrentlyInCreation(beanName)) {
logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
"' that is not fully initialized yet - a consequence of a circular reference");
}
else {
logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
}
}
// 注释3-1. 返回对应的实例,普通的bean返回
// 对 FactoryBean 的特殊处理,是返回指定方法返回的实例
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
else {
// 注释4. 如果缓存中不存在该beanName的实例
// Fail if we're already creating this bean instance:
// We're assumably within a circular reference.
// 注释4-1. 循环依赖校验,假设我们在一个循环引用中,我们之前已经创建了这个bean实例,则会抛出错误:循环依赖
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
// Check if bean definition exists in this factory.
// 注释4-2. 获取parentBeanFactory
BeanFactory parentBeanFactory = getParentBeanFactory();
// 注释4-2-1. 如果parentBeanFactory存在,并且beanName在当前BeanFactory不存在Bean定义,则尝试从parentBeanFactory中获取bean实例
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
// Not found -> check parent.将别名解析成真正的beanName
String nameToLookup = originalBeanName(name);
//通过parentBeanFactory获取bean实例
if (parentBeanFactory instanceof AbstractBeanFactory) {
return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
nameToLookup, requiredType, args, typeCheckOnly);
}
else if (args != null) {
// Delegation to parent with explicit args.
return (T) parentBeanFactory.getBean(nameToLookup, args);
}
else if (requiredType != null) {
// No args -> delegate to standard getBean method.
return parentBeanFactory.getBean(nameToLookup, requiredType);
}
else {
return (T) parentBeanFactory.getBean(nameToLookup);
}
}
// 注释4-2-2. 如果不是仅仅做类型检查则是创建 bean,这里要将beanName放到alreadyCreated缓存
if (!typeCheckOnly) {
markBeanAsCreated(beanName);
}
try {
// 注释5. 根据beanName重新获取MergedBeanDefinition
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
// 检查MergedBeanDefinition
checkMergedBeanDefinition(mbd, beanName, args);
// Guarantee initialization of beans that the current bean depends on.
// 注释6. 保证当前 bean 所依赖的bean的实例化,
String[] dependsOn = mbd.getDependsOn();
// 如果存在依赖则需要递归实例化依赖的 bean
if (dependsOn != null) {
for (String dep : dependsOn) {
// 检查循环依赖报错~
if (isDependent(beanName, dep)) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
}
// 将dep和beanName的依赖关系注册到缓存中
registerDependentBean(dep, beanName);
try {
// 获取dep对应的bean实例,如果dep还没有创建bean实例,则创建dep的bean实例
getBean(dep);
}
catch (NoSuchBeanDefinitionException ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
}
}
}
// Create bean instance. 创建 bean 实例
// 注释7. singleton 单例模式(最常使用)
if (mbd.isSingleton()) {
// 注释7-1. scope为singleton的bean创建
//ObjectFactory为函数式接口,lambda表达式重写了getObject方法
sharedInstance = getSingleton(beanName, () -> {
try {
//注释7-1-1. 创建Bean实例
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;
}
});
//注释7-1-2.返回beanName对应的实例对象
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
//注释7-2. scope为prototype的bean创建
else if (mbd.isPrototype()) {
// It's a prototype -> create a new instance.
Object prototypeInstance = null;
try {
//注释7-2-1. 创建实例前的操作
beforePrototypeCreation(beanName);
//注释7-2-2. 创建Bean实例
prototypeInstance = createBean(beanName, mbd, args);
}
finally {
//注释7-2-3.创建实例后的操作
afterPrototypeCreation(beanName);
}
//注释7-2-4.返回beanName对应的实例对象
bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
}
//注释7-3. 其他scope的bean创建
else {
//注释7-3-1. 获取scopeName,根据scopeName,从缓存拿到scope实例
String scopeName = mbd.getScope();
final Scope scope = this.scopes.get(scopeName);
if (scope == null) {
throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
}
try {
//注释7-3-2. 其他scope的bean创建,新建了一个ObjectFactory
//ObjectFactory为函数式接口,lambda表达式重写了getObject方法
Object scopedInstance = scope.get(beanName, () -> {
//创建实例前的操作
beforePrototypeCreation(beanName);
try {
//创建bean实例
return createBean(beanName, mbd, args);
}
finally {
//创建实例后的操作
afterPrototypeCreation(beanName);
}
});
//返回beanName对应的实例对象
bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
}
catch (IllegalStateException ex) {
throw new BeanCreationException(beanName,
"Scope '" + scopeName + "' is not active for the current thread; consider " +
"defining a scoped proxy for this bean if you intend to refer to it from a singleton",
ex);
}
}
}
catch (BeansException ex) {
cleanupAfterBeanCreationFailure(beanName);
throw ex;
}
}
// Check if required type matches the type of the actual bean instance.
// 检查需要的类型是否符合 bean 的实际类型
if (requiredType != null && !requiredType.isInstance(bean)) {
try {
//类型不对,则尝试转换bean类型
T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
if (convertedBean == null) {
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
return convertedBean;
}
catch (TypeMismatchException ex) {
if (logger.isTraceEnabled()) {
logger.trace("Failed to convert bean '" + name + "' to required type '" +
ClassUtils.getQualifiedName(requiredType) + "'", ex);
}
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
}
//返回创建出来的bean实例对象
return (T) bean;
}
跟着注释走一遍,理解完其执行过程后,进入源码解析。
注释2.检查缓存中或者实例工厂是否有对应的实例或者从 singletonFactories 中的 ObjectFactory 中获取。在前文中对getSingleton这个方法解析过一次,里边使用了三级缓存的概念,可以回顾一下。
注释3-1. 返回对应的实例(见源码解析1)
注释4-2 通过parentBeanFactory 获取bean实例
注释4-2-2 判断如果不是仅仅做类型检查则是创建 bean,这里要将beanName放到alreadyCreated缓存(见源码解析2)
注释5. 获取MergedBeanDefinition,参考前文中的getMergedLocalBeanDefinition方法解析
注释6. 保证当前 bean 所依赖的bean的实例化,isDependent方法来检查循环依赖,registerDependentBean方法为记录beanName所对应的bean和所依赖的ben(dep)之间的依赖关系,注册到缓存中。(见源码解析3)
注释7. 按scope不同进行创建 bean 实例
注释7-1. scope为singleton的bean创建,这里调用了getSingleton(String beanName, ObjectFactory> singletonFactory)方法(见源码解析4)
注释7-1-1. 创建Bean实例,createBean方法(这个方法很重要,在下一篇详细解析)
注释7-3-2 创建prototype的bean实例前的操作beforePrototypeCreation方法、创建实例后的操作afterPrototypeCreation方法,可以点进去跟踪简单了解一下这个执行过程:主要是在进行 bean 实例的创建前,将 beanName 添加到 prototypesCurrentlyInCreation 缓存;bean 实例创建后,将 beanName 从 prototypesCurrentlyInCreation 缓存中移除。这边 prototypesCurrentlyInCreation 存放的类型为 Object,在只有一个 beanName 的时候,直接存该 beanName,也就是 String 类型;当有多个 beanName 时,转成 Set 来存放。
【源码解析1】 获取 beanName 对应的实例对象:AbstractBeanFactory类中的getObjectForBeanInstance方法:
protected Object getObjectForBeanInstance(
Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) {
// Don't let calling code try to dereference the factory if the bean isn't a factory.
// 查看isFactoryDereference可知道,这里判断的是 beanName 前缀是否是& 符号
if (BeanFactoryUtils.isFactoryDereference(name)) {
//如果是NullBean类型,直接返回
if (beanInstance instanceof NullBean) {
return beanInstance;
}
// 如果不是空 bean,又不是工厂 bean,报错
if (!(beanInstance instanceof FactoryBean)) {
throw new BeanIsNotAFactoryException(transformedBeanName(name), beanInstance.getClass());
}
}
// Now we have the bean instance, which may be a normal bean or a FactoryBean.
// If it's a FactoryBean, we use it to create a bean instance, unless the
// caller actually wants a reference to the factory.
// 如果beanInstance不是FactoryBean(也就是普通bean),则直接返回beanInstance
// 如果beanInstance是FactoryBean,并且name以“&”为前缀,则直接返回beanInstance(以“&”为前缀代表想获取的是FactoryBean本身)
if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) {
return beanInstance;
}
// 加载工厂 bean
// 从上面排除信息可以知道,这里开始beanInstance是FactoryBean,但name不带有“&”前缀,表示想要获取的是FactoryBean创建的对象实例
Object object = null;
if (mbd == null) {
// 如果mbd为空,先从缓存factoryBeanObjectCache中获取
object = getCachedObjectForFactoryBean(beanName);
}
if (object == null) {
// Return bean instance from factory.
// 到了这一步,很确定是工厂 bean 类型了,类型强转
FactoryBean> factory = (FactoryBean>) beanInstance;
// Caches object obtained from FactoryBean if it is a singleton.
// mbd为空,但是该bean的BeanDefinition在缓存中存在,则获取该bean的MergedBeanDefinitio
if (mbd == null && containsBeanDefinition(beanName)) {
mbd = getMergedLocalBeanDefinition(beanName);
}
// 判断mbd 是否是合成的BeanDefinition
boolean synthetic = (mbd != null && mbd.isSynthetic());
//注释1. 通过 getObjectFromFactoryBean方法获取对象实例
object = getObjectFromFactoryBean(factory, beanName, !synthetic);
}
return object;
}
FactoryBean 前文中已经介绍过其定义了,是一种特殊的bean即工厂 bean,可以自己创建 bean 实例。
这里主要继续解析getObjectFromFactoryBean方法,获取对象实例(见源码解析1-1)
【源码解析1-1】 FactoryBeanRegistrySupport类中,从 FactoryBean 获取对象实例:getObjectFromFactoryBean
protected Object getObjectFromFactoryBean(FactoryBean> factory, String beanName, boolean shouldPostProcess) {
//factoryBean如果是单例,并且已经存在于单例对象缓存中
if (factory.isSingleton() && containsSingleton(beanName)) {
//加锁操作
synchronized (getSingletonMutex()) {
//获取缓存
Object object = this.factoryBeanObjectCache.get(beanName);
if (object == null) {
// 注释1. 缓存不存在,通过factoryBean获取beanName实例对象
object = doGetObjectFromFactoryBean(factory, beanName);
// Only post-process and store if not put there already during getObject() call above
// (e.g. because of circular reference processing triggered by custom getBean calls)
//获取缓存
Object alreadyThere = this.factoryBeanObjectCache.get(beanName);
if (alreadyThere != null) {
//如果该beanName已经在缓存中存在,则将object替换成缓存中的alreadyThere
object = alreadyThere;
}
else {
if (shouldPostProcess) {
//该beanName被标记了属于正在创建的bean,存在于Set singletonsCurrentlyInCreation中
//则直接返回该实例对象
if (isSingletonCurrentlyInCreation(beanName)) {
// Temporarily return non-post-processed object, not storing it yet..
return object;
}
// 创建单例前的前置操作,校验下存不存在,不存在抛异常
beforeSingletonCreation(beanName);
try {
//注释2. 对bean实例进行后置处理,执行所有已注册的BeanPostProcessor的postProcessAfterInitialization方法
object = postProcessObjectFromFactoryBean(object, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(beanName,
"Post-processing of FactoryBean's singleton object failed", ex);
}
finally {
// 创建单例后的后置操作,校验下存不存在,不存在抛异常
afterSingletonCreation(beanName);
}
}
if (containsSingleton(beanName)) {
this.factoryBeanObjectCache.put(beanName, object);
}
}
}
return object;
}
}
else {
//factoryBean不是单例的情况下,通过factoryBean获取beanName实例对象
Object object = doGetObjectFromFactoryBean(factory, beanName);
if (shouldPostProcess) {
try {
// 对bean实例进行后置处理,执行所有已注册的BeanPostProcessor的postProcessAfterInitialization方法
object = postProcessObjectFromFactoryBean(object, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(beanName, "Post-processing of FactoryBean's object failed", ex);
}
}
//返回object实例对象
return object;
}
}
这个方法执行流程看下来,真正做事的得看doGetObjectFromFactoryBean这个方法。
注释1. 缓存不存在,通过factoryBean获取beanName实例对象,doGetObjectFromFactoryBean(见源码解析1-2)
注释2. 对bean实例进行后置处理,执行所有已注册的BeanPostProcessor的postProcessAfterInitialization方法,postProcessObjectFromFactoryBean(见源码解析1-3)
【源码解析1-2】 通过FactoryBean来获取到对象实例 : doGetObjectFromFactoryBean
private Object doGetObjectFromFactoryBean(final FactoryBean> factory, final String beanName)
throws BeanCreationException {
Object object;
try {
//系统安全管理验证
if (System.getSecurityManager() != null) {
AccessControlContext acc = getAccessControlContext();
try {
//调用FactoryBean的getObject方法获取bean对象实例
object = AccessController.doPrivileged((PrivilegedExceptionAction
这里主要就是通过factory bean 的 getObject方法来获取对象。
【源码解析1-3】 子类AbstractAutowireCapableBeanFactory postProcessObjectFromFactoryBean,重写了父类FactoryBeanRegistrySupport里的方法:
@Override
protected Object postProcessObjectFromFactoryBean(Object object, String beanName) {
//
return applyBeanPostProcessorsAfterInitialization(object, beanName);
}
@Override
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
//遍历所有注册的BeanPostProcessor实现类,调用postProcessAfterInitialization方法
for (BeanPostProcessor processor : getBeanPostProcessors()) {
//在bean初始化后,调用postProcessAfterInitialization方法
Object current = processor.postProcessAfterInitialization(result, beanName);
if (current == null) {
//如果返回null,则直接返回结果
return result;
}
//有值则直接返回
result = current;
}
return result;
}
我们创建的beanFactory为DefaultListableBeanFactory类型,这边走的是 AbstractAutowireCapableBeanFactory 里的方法,它为DefaultListableBeanFactory父类,FactoryBeanRegistrySupport的子类,@Override重写父类FactoryBeanRegistrySupport的postProcessObjectFromFactoryBean方法。
前面的registerBeanPostProcessors已经帮我们注册了各类BeanPostProcessors,它会再bean实例化后,初始化阶段执行被执行其接口的方法。
【源码解析2】 将beanName放到alreadyCreated缓存 : markBeanAsCreated方法
protected void markBeanAsCreated(String beanName) {
if (!this.alreadyCreated.contains(beanName)) {
synchronized (this.mergedBeanDefinitions) {
//如果alreadyCreated缓存中不包含beanName
if (!this.alreadyCreated.contains(beanName)) {
// Let the bean definition get re-merged now that we're actually creating
// the bean... just in case some of its metadata changed in the meantime.
//将beanName的MergedBeanDefinition从mergedBeanDefinitions缓存中移除,在之后重新获取MergedBeanDefinition
clearMergedBeanDefinition(beanName);
//将beanName添加到alreadyCreated缓存中,代表该beanName的bean实例已经创建(或即将创建)
this.alreadyCreated.add(beanName);
}
}
}
}
alreadyCreated 是一个Set集合作为已经创建(或即将创建)的实例集合,
这边会将 beanName 对应的 MergedBeanDefinition 移除,然后在之后的代码重新获取,主要是为了使用最新的 MergedBeanDefinition 来进行创建操作。
【源码解析3】 保证当前 bean 所依赖的bean的实例化
检查循环依赖:来自DefaultSingletonBeanRegistry类中的 isDependent方法
protected boolean isDependent(String beanName, String dependentBeanName) {
synchronized (this.dependentBeanMap) {
return isDependent(beanName, dependentBeanName, null);
}
}
private boolean isDependent(String beanName, String dependentBeanName, @Nullable Set alreadySeen) {
if (alreadySeen != null && alreadySeen.contains(beanName)) {
return false;
}
// 点开canonicalName方法可以看到将别名解析为真正的名称
String canonicalName = canonicalName(beanName);
//依赖canonicalName的beanName集合
Set dependentBeans = this.dependentBeanMap.get(canonicalName);
if (dependentBeans == null) {
//如果dependentBeans为空,则两者必然还未确定依赖关系,返回fals
return false;
}
if (dependentBeans.contains(dependentBeanName)) {
//如果dependentBeans包含dependentBeanName,则表示两者已确定依赖关系,返回true
return true;
}
//循环检查,即检查依赖canonicalName的所有beanName是否存在被dependentBeanName依赖的(即隔层依赖)
for (String transitiveDependency : dependentBeans) {
if (alreadySeen == null) {
alreadySeen = new HashSet<>();
}
//已经检查过的添加到alreadySeen,避免重复检查
alreadySeen.add(beanName);
//隔层依赖检查
if (isDependent(transitiveDependency, dependentBeanName, alreadySeen)) {
return true;
}
}
return false;
}
registerDependentBean方法:记录beanName所对应的bean和所依赖的ben(dep)之间的依赖关系,注册到缓存中
public void registerDependentBean(String beanName, String dependentBeanName) {
//解析别名
String canonicalName = canonicalName(beanName);
//加锁操作
synchronized (this.dependentBeanMap) {
//如果依赖关系还没有注册,则将两者的关系注册到dependentBeanMap和dependenciesForBeanMap缓存
Set dependentBeans =
this.dependentBeanMap.computeIfAbsent(canonicalName, k -> new LinkedHashSet<>(8));
if (!dependentBeans.add(dependentBeanName)) {
return;
}
}
//加锁操作
synchronized (this.dependenciesForBeanMap) {
//将canonicalName添加到dependentBeanName依赖的beanName集合中
Set dependenciesForBean =
this.dependenciesForBeanMap.computeIfAbsent(dependentBeanName, k -> new LinkedHashSet<>(8));
dependenciesForBean.add(canonicalName);
}
}
dependenciesForBeanMap:beanName 对应的 bean 依赖的所有 bean 的 beanName 集合,
前面提到的dependentBeanMap 是所有依赖 beanName 对应的 bean 的 beanName 集合
【源码解析4】 前文介绍过getSingleton(String beanName)方法,这里调用的是 DefaultSingletonBeanRegistry类中的getSingleton(String beanName, ObjectFactory> singletonFactory)方法
public Object getSingleton(String beanName, ObjectFactory> singletonFactory) {
Assert.notNull(beanName, "Bean name must not be null");
// 注释 全局变量,加锁
synchronized (this.singletonObjects) {
// 首先检查beanName对应的bean实例是否在缓存中存在
Object singletonObject = this.singletonObjects.get(beanName);
//如果不存在,则进入创建操作
if (singletonObject == null) {
//当bean factory的单例处于destruction状态时,不允许进行单例bean创建,抛出异常
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 + "'");
}
// 注释1-1. 创建单例前的操作,校验是否 beanName 是否有别的线程在初始化,并加入初始化状态中
beforeSingletonCreation(beanName);
boolean newSingleton = false;
boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
if (recordSuppressedExceptions) {
this.suppressedExceptions = new LinkedHashSet<>();
}
try {
// 执行singletonFactory的getObject方法获取bean实例
//实际执行的是 createBean 方法
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;
}
//注释1-2. 创建单例后的操作,移除初始化状态
afterSingletonCreation(beanName);
}
if (newSingleton) {
// 注释2. 如果是新的单例对象,加入缓存
//singletonObjects、registeredSingletons
addSingleton(beanName, singletonObject);
}
}
// 返回单例对象
return singletonObject;
}
}
这个方法传入的第二个参数为lambda表达式的匿名函数,ObjectFactory为函数式接口,所以singletonFactory的getObject方法会触发匿名函数里的createBean方法。
注释1-1、1-2. 创建单例前的操作、创建单例后的操作,出现在创建单例的前后(见源码解析5)
注释2. 如果是新的单例对象,将 beanName 和对应的单例对象添加到缓存中:addSingleton方法
protected void addSingleton(String beanName, Object singletonObject) {
synchronized (this.singletonObjects) {
// 1.添加到单例对象缓存
this.singletonObjects.put(beanName, (singletonObject != null ? singletonObject : NULL_OBJECT));
// 2.将单例工厂缓存移除(已经不需要)
this.singletonFactories.remove(beanName);
// 3.将早期单例对象缓存移除(已经不需要)
this.earlySingletonObjects.remove(beanName);
// 4.添加到已经注册的单例对象缓存
this.registeredSingletons.add(beanName);
}
}
这个方法是比较重要的,多关注一下,乍一看就是对各类Map的操作,前文我们已经了解过singletonObjects、earlySingletonObjects、singletonFactories 构成了三级缓存的概念,这里是把创建好的实例对象移动到一级缓存singletonObjects 和注册的单例对象缓存registeredSingletons中,移除掉二级、三级缓存。一级缓存中的是完全态创建的实例对象,二级、三级是创建中的半成品实例对象。
【源码解析5】
protected void beforeSingletonCreation(String beanName) {
// 先校验beanName是否为要在创建检查排除掉的(inCreationCheckExclusions缓存),如果不是,
// 则将beanName加入到正在创建bean的缓存中(Set),如果beanName已经存在于该缓存,会返回false抛出异常(这种情况出现在构造器的循环依赖)
if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.add(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
}
protected void afterSingletonCreation(String beanName) {
// 先校验beanName是否为要在创建检查排除掉的(inCreationCheckExclusions缓存),如果不是,
// 则将beanName从正在创建bean的缓存中(Set)移除,如果beanName不存在于该缓存,会返回false抛出异常
if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.remove(beanName)) {
throw new IllegalStateException("Singleton '" + beanName + "' isn't currently in creation");
}
inCreationCheckExclusions 是要在创建检查排除掉的 beanName 集合,正常为空,可以不管。这边主要是引入了 singletonsCurrentlyInCreation 缓存:当前正在创建的 bean 的 beanName 集合。在 beforeSingletonCreation 方法中,通过添加 beanName 到该缓存,可以预防出现构造器循环依赖的情况。
为什么这里无法解决构造器循环依赖?
我们之前在前文提过,getSingleton 方法是解决循环引用的核心代码。有一句话:“我们先用构造函数创建一个 “不完整” 的 bean 实例”,从这句话可以看出,构造器循环依赖是无法解决的,因为当构造器出现循环依赖,我们连 “不完整” 的 bean 实例都构建不出来。Spring 能解决这类循环依赖的方案有:通过 setter 注入的循环依赖、通过属性注入的循环依赖,使用@lazy注解,使用@PostConstruct等,优先建议使用setter注入来解决。
总结
这里介绍了bean实例化部分的getBean方法,包括先从缓存中获取实例(getSingleton)、 FactoryBean 的 bean 创建(getObjectForBeanInstance)、实例化自己的依赖即dependOn变量(递归调用getBean来创建实例)、创建 bean 实例(createBean),以及创建前后一些标记等,createBean方法也是个很重要的方法,下一文中详细介绍它。