美文网首页
Spring5IOC容器解析——getBean()方法分析

Spring5IOC容器解析——getBean()方法分析

作者: 小波同学 | 来源:发表于2020-06-26 19:22 被阅读0次

前言

接着Spring5IOC容器解析--refresh()方法分析,我们正式开始学习获取 bean 实例方法,该方法是 Spring 最核心的方法。

正文

进入refresh()方法跟进实例化所有的类方法finishBeanFactoryInitialization()方法,然后继续跟进finishBeanFactoryInitialization方法,接着单击 preInstantiateSingletons 方法里的 getBean(beanName) 代码,进入该方法。

getBean

@Override
public Object getBean(String name) throws BeansException {
    // 获取name对应的bean实例,如果不存在,则创建一个
    return doGetBean(name, null, null, false);
}

doGetBean

public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {

    protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
            @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {

        // 1.解析beanName,主要是解析别名、去掉FactoryBean的前缀“&”
        final String beanName = transformedBeanName(name);
        Object bean;

        // Eagerly check singleton cache for manually registered singletons.
        // 2.尝试从缓存中获取beanName对应的实例
        Object sharedInstance = getSingleton(beanName);
        if (sharedInstance != null && args == null) {
            // 3.如果beanName的实例存在于缓存中
            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 返回beanName对应的实例对象(主要用于FactoryBean的特殊处理,普通Bean会直接返回sharedInstance本身)
            bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
        }

        else {
            // Fail if we're already creating this bean instance:
            // We're assumably within a circular reference.
            // 4.scope为prototype的循环依赖校验:如果beanName已经正在创建Bean实例中,而此时我们又要再一次创建beanName的实例,则代表出现了循环依赖,需要抛出异常。
            // 例子:如果存在A中有B的属性,B中有A的属性,那么当依赖注入的时候,就会产生当A还未创建完的时候因为对于B的创建再次返回创建A,造成循环依赖
            if (isPrototypeCurrentlyInCreation(beanName)) {
                throw new BeanCurrentlyInCreationException(beanName);
            }

            // Check if bean definition exists in this factory.
            // 5.获取parentBeanFactory
            BeanFactory parentBeanFactory = getParentBeanFactory();
            // 5.1 如果parentBeanFactory存在,并且beanName在当前BeanFactory不存在Bean定义,则尝试从parentBeanFactory中获取bean实例
            if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
                // Not found -> check parent.
                // 5.2 将别名解析成真正的beanName
                String nameToLookup = originalBeanName(name);
                if (parentBeanFactory instanceof AbstractBeanFactory) {
                    return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
                            nameToLookup, requiredType, args, typeCheckOnly);
                }
                // 5.3 尝试在parentBeanFactory中获取bean对象实例
                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);
                }
            }

            if (!typeCheckOnly) {
                // 6.如果不是仅仅做类型检测,而是创建bean实例,这里要将beanName放到alreadyCreated缓存
                markBeanAsCreated(beanName);
            }

            try {
                // 7.根据beanName重新获取MergedBeanDefinition(步骤6将MergedBeanDefinition删除了,这边获取一个新的)
                final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
                // 7.1 检查MergedBeanDefinition
                checkMergedBeanDefinition(mbd, beanName, args);

                // Guarantee initialization of beans that the current bean depends on.
                // 8.拿到当前bean依赖的bean名称集合,在实例化自己之前,需要先实例化自己依赖的bean
                String[] dependsOn = mbd.getDependsOn();
                if (dependsOn != null) {
                    // 8.1 遍历当前bean依赖的bean名称集合
                    for (String dep : dependsOn) {
                        // 8.2 检查dep是否依赖于beanName,即检查是否存在循环依赖
                        if (isDependent(beanName, dep)) {
                            // 8.3 如果是循环依赖则抛异常
                            throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                    "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
                        }
                        // 8.4 将dep和beanName的依赖关系注册到缓存中
                        registerDependentBean(dep, beanName);
                        try {
                            // 8.5 获取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.
                // 9.针对不同的scope进行bean的创建
                if (mbd.isSingleton()) {
                    // 9.1 scope为singleton的bean创建(新建了一个ObjectFactory,并且重写了getObject方法)
                    sharedInstance = getSingleton(beanName, () -> {
                        try {
                            // 9.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;
                        }
                    });
                    // 9.1.2 返回beanName对应的实例对象
                    bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
                }

                else if (mbd.isPrototype()) {
                    // It's a prototype -> create a new instance.
                    // 9.2 scope为prototype的bean创建
                    Object prototypeInstance = null;
                    try {
                        // 9.2.1 创建实例前的操作(将beanName保存到prototypesCurrentlyInCreation缓存中)
                        beforePrototypeCreation(beanName);
                        // 9.2.2 创建Bean实例
                        prototypeInstance = createBean(beanName, mbd, args);
                    }
                    finally {
                        // 9.2.3 创建实例后的操作(将创建完的beanName从prototypesCurrentlyInCreation缓存中移除)
                        afterPrototypeCreation(beanName);
                    }
                    // 9.2.4 返回beanName对应的实例对象
                    bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
                }

                else {
                    // 9.3 其他scope的bean创建,可能是request之类的
                    // 9.3.1 根据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 {
                        // 9.3.2 其他scope的bean创建(新建了一个ObjectFactory,并且重写了getObject方法)
                        Object scopedInstance = scope.get(beanName, () -> {
                            // 9.3.3 创建实例前的操作(将beanName保存到prototypesCurrentlyInCreation缓存中)
                            beforePrototypeCreation(beanName);
                            try {
                                // 9.3.4 创建bean实例
                                return createBean(beanName, mbd, args);
                            }
                            finally {
                                // 9.3.5 创建实例后的操作(将创建完的beanName从prototypesCurrentlyInCreation缓存中移除)
                                afterPrototypeCreation(beanName);
                            }
                        });
                        // 9.3.6 返回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) {
                // 如果创建bean实例过程中出现异常,则将beanName从alreadyCreated缓存中移除
                cleanupAfterBeanCreationFailure(beanName);
                throw ex;
            }
        }

        // Check if required type matches the type of the actual bean instance.
        // 10.检查所需类型是否与实际的bean对象的类型匹配
        if (requiredType != null && !requiredType.isInstance(bean)) {
            try {
                // 10.1 类型不对,则尝试转换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());
            }
        }
        // 11.返回创建出来的bean实例对象
        return (T) bean;
    }
}
  • 1、解析 beanName,主要是解析别名、去掉 FactoryBean 的修饰符 “&”。

  • 2、尝试从缓存中获取 beanName 对应的实例。

  • 3.1、返回 beanName 对应的实例对象(主要用于 FactoryBean 的特殊处理,普通 bean 会直接返回 sharedInstance 本身)。

  • 6、如果不是仅仅做类型检测,而是创建 bean 实例,这里要将 beanName 放到 alreadyCreated 缓存。

  • 7、根据 beanName 重新获取 MergedBeanDefinition。

  • 8.2、检查 dep 是否依赖于 beanName,即检查是否存在循环依赖。

  • 8.4、将 dep 和 beanName 的依赖关系注册到缓存中。

  • 9.1、scope 为 singleton 的 bean 创建(新建了一个 ObjectFactory,并且重写了 getObject 方法)。

  • 9.1.1、9.2.2、9.3.4、创建 bean 实例。

  • 9.1.2、9.2.4、9.3.6、返回 beanName 对应的实例对象。

  • 9.2.1、scope 为 prototype 时创建实例前的操作、9.2.3 scope 为 prototype 时 创建实例后的操作,相对应的两个方法。

Object sharedInstance = getSingleton(beanName)

尝试从缓存中获取beanName对应的实例

public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {

    /** Cache of singleton objects: bean name to bean instance. */
    //一级缓存:单例对象缓存池,beanName->Bean,其中存储的就是实例化,属性赋值成功之后的单例对象
    private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);

    /** Cache of singleton factories: bean name to ObjectFactory. */
    //三级缓存:单例工厂的缓存,beanName->ObjectFactory,添加进去的时候实例还未具备属性
    // 用于保存beanName和创建bean的工厂之间的关系map,单例Bean在创建之初过早的暴露出去的Factory,
    // 为什么采用工厂方式,是因为有些Bean是需要被代理的,总不能把代理前的暴露出去那就毫无意义了
    private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);

    /** Cache of early singleton objects: bean name to bean instance. */
    //二级缓存:早期的单例对象,beanName->Bean,其中存储的是实例化之后,属性未赋值的单例对象
    // 执行了工厂方法生产出来的Bean,bean被放进去之后,
    // 那么当bean在创建过程中,就可以通过getBean方法获取到
    private final Map<String, Object> earlySingletonObjects = new HashMap<>(16);
    
    @Override
    @Nullable
    public Object getSingleton(String beanName) {
        return getSingleton(beanName, true);
    }

    @Nullable
    protected Object getSingleton(String beanName, boolean allowEarlyReference) {
        // 1.从单例对象缓存中获取beanName对应的单例对象
        Object singletonObject = this.singletonObjects.get(beanName);
        // 2.如果单例对象缓存中没有,并且该beanName对应的单例bean正在创建中
        if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
            // 3.加锁进行操作
            synchronized (this.singletonObjects) {
                // 4.从早期单例对象缓存中获取单例对象(之所称成为早期单例对象,是因为earlySingletonObjects里
                // 的对象的都是通过提前曝光的ObjectFactory创建出来的,还未进行属性填充等操作)
                singletonObject = this.earlySingletonObjects.get(beanName);
                // 5.如果在早期单例对象缓存中也没有,并且允许创建早期单例对象引用
                if (singletonObject == null && allowEarlyReference) {
                    // 6.从单例工厂缓存中获取beanName的单例工厂
                    ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
                    if (singletonFactory != null) {
                        // 7.如果存在单例对象工厂,则通过工厂创建一个单例对象
                        singletonObject = singletonFactory.getObject();
                        // 8.将通过单例对象工厂创建的单例对象,放到早期单例对象缓存中
                        this.earlySingletonObjects.put(beanName, singletonObject);
                        // 9.移除该beanName对应的单例对象工厂,因为该单例工厂已经创建了一个实例对象,并且放到earlySingletonObjects缓存了,
                        // 因此,后续获取beanName的单例对象,可以通过earlySingletonObjects缓存拿到,不需要在用到该单例工厂
                        this.singletonFactories.remove(beanName);
                    }
                }
            }
        }
        // 10.返回单例对象
        return singletonObject;
    }
}

这段代码很重要,在正常情况下,该代码很普通,只是正常的检查下我们要拿的 bean 实例是否存在于缓存中,如果有就返回缓存中的 bean 实例,否则就返回 null。

这段代码之所以重要,是因为该段代码是 Spring 解决循环引用的核心代码。

解决循环引用逻辑:使用构造函数创建一个 “不完整” 的 bean 实例(之所以说不完整,是因为此时该 bean 实例还未初始化),并且提前曝光该 bean 实例的 ObjectFactory(提前曝光就是将 ObjectFactory 放到 singletonFactories 缓存),通过 ObjectFactory 我们可以拿到该 bean 实例的引用,如果出现循环引用,我们可以通过缓存中的 ObjectFactory 来拿到 bean 实例,从而避免出现循环引用导致的死循环。这边通过缓存中的 ObjectFactory 拿到的 bean 实例虽然拿到的是 “不完整” 的 bean 实例,但是由于是单例,所以后续初始化完成后,该 bean 实例的引用地址并不会变,所以最终我们看到的还是完整 bean 实例。

另外这个代码块中引进了4个重要缓存:

  • singletonObjects 缓存:beanName -> 单例 bean 对象。
  • earlySingletonObjects 缓存:beanName -> 单例 bean 对象,该缓存存放的是早期单例 bean 对象,可以理解成还未进行属性填充、初始化。
  • singletonFactories 缓存:beanName -> ObjectFactory。
  • singletonsCurrentlyInCreation 缓存:当前正在创建单例 bean 对象的 beanName 集合。

singletonObjects、earlySingletonObjects、singletonFactories 在这边构成了一个类似于 “三级缓存” 的概念。

getObjectForBeanInstance

返回beanName对应的实例对象(主要用于FactoryBean的特殊处理,普通Bean会直接返回sharedInstance本身)

public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {

    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.
        if (BeanFactoryUtils.isFactoryDereference(name)) {
            if (beanInstance instanceof NullBean) {
                return beanInstance;
            }
            // 1.如果name以“&”为前缀,但是beanInstance不是FactoryBean,则抛异常
            if (!(beanInstance instanceof FactoryBean)) {
                throw new BeanIsNotAFactoryException(beanName, beanInstance.getClass());
            }
            if (mbd != null) {
                mbd.isFactoryBean = true;
            }
            return beanInstance;
        }

        // 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.
        // 2.1 如果beanInstance不是FactoryBean(也就是普通bean),则直接返回beanInstance
        if (!(beanInstance instanceof FactoryBean)) {
            return beanInstance;
        }
        //FactoryBean创建出bean实例返回
        // 3.走到这边,代表beanInstance是FactoryBean,但name不带有“&”前缀,表示想要获取的是FactoryBean创建的对象实例
        Object object = null;
        if (mbd != null) {
            mbd.isFactoryBean = true;
        }
        else {
            // 4.如果mbd为空,则尝试从factoryBeanObjectCache缓存中获取该FactoryBean创建的对象实例
            object = getCachedObjectForFactoryBean(beanName);
        }
        if (object == null) {
            // Return bean instance from factory.
            // 5.只有beanInstance是FactoryBean才能走到这边,因此直接强转
            FactoryBean<?> factory = (FactoryBean<?>) beanInstance;
            // Caches object obtained from FactoryBean if it is a singleton.
            if (mbd == null && containsBeanDefinition(beanName)) {
                // 6.mbd为空,但是该bean的BeanDefinition在缓存中存在,则获取该bean的MergedBeanDefinition
                mbd = getMergedLocalBeanDefinition(beanName);
            }
            // 7.mbd是否是合成的(这个字段比较复杂,mbd正常情况都不是合成的,也就是false,有兴趣的可以自己查阅资料看看)
            boolean synthetic = (mbd != null && mbd.isSynthetic());
            // 8.从FactoryBean获取对象实例
            object = getObjectFromFactoryBean(factory, beanName, !synthetic);
        }
        // 9.返回对象实例
        return object;
    }
}

getObjectFromFactoryBean

从FactoryBean获取对象实例

public abstract class FactoryBeanRegistrySupport extends DefaultSingletonBeanRegistry {

    protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) {
        // 1.如果是单例,并且已经存在于单例对象缓存中
        if (factory.isSingleton() && containsSingleton(beanName)) {
            //又见双重检查锁机制,尝试再从缓存中获取,防止多线程下可能有别的线程已完成该单例Bean的创建
            synchronized (getSingletonMutex()) {
                // 2.从FactoryBean创建的单例对象的缓存中获取该bean实例
                Object object = this.factoryBeanObjectCache.get(beanName);
                if (object == null) {
                    // 3.调用FactoryBean的getObject方法获取对象实例
                    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);
                    // 4.如果该beanName已经在缓存中存在,则将object替换成缓存中的
                    if (alreadyThere != null) {
                        object = alreadyThere;
                    }
                    else {
                        if (shouldPostProcess) {
                            if (isSingletonCurrentlyInCreation(beanName)) {
                                // Temporarily return non-post-processed object, not storing it yet..
                                return object;
                            }
                            beforeSingletonCreation(beanName);
                            try {
                                // 5.对bean实例进行后置处理,执行所有已注册的BeanPostProcessor的postProcessAfterInitialization方法
                                //触发BeanPostProcessor,第三方框架可以在此用AOP来包装Bean实例
                                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)) {
                            // 6.将beanName和object放到factoryBeanObjectCache缓存中
                            this.factoryBeanObjectCache.put(beanName, object);
                        }
                    }
                }
                // 7.返回object对象实例
                return object;
            }
        }
        else {
            // 8.调用FactoryBean的getObject方法获取对象实例
            Object object = doGetObjectFromFactoryBean(factory, beanName);
            if (shouldPostProcess) {
                try {
                    // 9.对bean实例进行后置处理,执行所有已注册的BeanPostProcessor的postProcessAfterInitialization方法
                    object = postProcessObjectFromFactoryBean(object, beanName);
                }
                catch (Throwable ex) {
                    throw new BeanCreationException(beanName, "Post-processing of FactoryBean's object failed", ex);
                }
            }
            // 10.返回object对象实例
            return object;
        }
    }
}

主要步骤:

  • 调用 FactoryBean 的 getObject 方法获取对象实例。

  • 对 bean 实例进行后续处理,执行所有已注册的 BeanPostProcessor 的 postProcessAfterInitialization 方法。

doGetObjectFromFactoryBean

用FactoryBean的getObject方法获取对象实例

public abstract class FactoryBeanRegistrySupport extends DefaultSingletonBeanRegistry {

    private Object doGetObjectFromFactoryBean(final FactoryBean<?> factory, final String beanName)
            throws BeanCreationException {

        Object object;
        try {
            // 1.调用FactoryBean的getObject方法获取bean对象实例
            if (System.getSecurityManager() != null) {
                AccessControlContext acc = getAccessControlContext();
                try {
                    // 1.1 带有权限验证的
                    object = AccessController.doPrivileged((PrivilegedExceptionAction<Object>) factory::getObject, acc);
                }
                catch (PrivilegedActionException pae) {
                    throw pae.getException();
                }
            }
            else {
                // 1.2 不带权限
                object = factory.getObject();
            }
        }
        catch (FactoryBeanNotInitializedException ex) {
            throw new BeanCurrentlyInCreationException(beanName, ex.toString());
        }
        catch (Throwable ex) {
            throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation", ex);
        }

        // Do not accept a null value for a FactoryBean that's not fully
        // initialized yet: Many FactoryBeans just return null then.
        // 2.getObject返回的是空值,并且该FactoryBean正在初始化中,则直接抛异常,
        // 不接受一个尚未完全初始化的FactoryBean的getObject返回的空值
        if (object == null) {
            if (isSingletonCurrentlyInCreation(beanName)) {
                throw new BeanCurrentlyInCreationException(
                        beanName, "FactoryBean which is currently in creation returned null from getObject");
            }
            object = new NullBean();
        }
        // 3.返回创建好的bean对象实例
        return object;
    }
}

很简单的方法,就是直接调用 FactoryBean 的 getObject 方法来获取到对象实例。

细心的同学可以发现,该方法是以 do 开头,以 do 开头的方法是最终进行实际操作的方法,例如本方法就是 FactoryBean 最终实际进行创建 bean 对象实例的方法。

postProcessObjectFromFactoryBean

对bean实例进行后置处理,执行所有已注册的BeanPostProcessor的postProcessAfterInitialization方法

public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory
        implements AutowireCapableBeanFactory {

    @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;
        // 1.遍历所有注册的BeanPostProcessor实现类,调用postProcessAfterInitialization方法
        for (BeanPostProcessor processor : getBeanPostProcessors()) {
            // 2.在bean初始化后,调用postProcessAfterInitialization方法
            Object current = processor.postProcessAfterInitialization(result, beanName);
            if (current == null) {
                // 3.如果返回null,则不会调用后续的BeanPostProcessors
                return result;
            }
            result = current;
        }
        return result;
    }
}

这边走的是 AbstractAutowireCapableBeanFactory 里的方法。通过前面的介绍,我们知道创建的 BeanFactory 为 DefaultListableBeanFactory,而 DefaultListableBeanFactory 继承了 AbstractAutowireCapableBeanFactory,因此这边会走 AbstractAutowireCapableBeanFactory 的重写方法。

在registerBeanPostProcessors 详解 中已经学过 BeanPostProcessor,在创建完 bean 实例后,会执行 BeanPostProcessor 的 postProcessAfterInitialization 方法。

markBeanAsCreated

如果不是仅仅做类型检测,而是创建bean实例,这里要将beanName放到alreadyCreated缓存

public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {

    protected void markBeanAsCreated(String beanName) {
        if (!this.alreadyCreated.contains(beanName)) {
            synchronized (this.mergedBeanDefinitions) {
                // 1.如果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.
                    // 2.将beanName的MergedBeanDefinition从mergedBeanDefinitions缓存中移除,
                    // 在之后重新获取MergedBeanDefinition,避免BeanDefinition在创建过程中发生变化
                    clearMergedBeanDefinition(beanName);
                    // 3.将beanName添加到alreadyCreated缓存中,代表该beanName的bean实例已经创建(或即将创建)
                    this.alreadyCreated.add(beanName);
                }
            }
        }
    }

    protected void clearMergedBeanDefinition(String beanName) {
        RootBeanDefinition bd = this.mergedBeanDefinitions.get(beanName);
        if (bd != null) {
            bd.stale = true;
        }
    }
}

这边会将 beanName 对应的 MergedBeanDefinition 移除,然后在之后的代码重新获取,主要是为了使用最新的 MergedBeanDefinition 来进行创建操作。

isDependent

检查dep是否依赖于beanName,即检查是否存在循环依赖

public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {

    /** Map between dependent bean names: bean name to Set of dependent bean names. */
    //dependentBeanMap(被依赖关系:key被value所依赖)
    private final Map<String, Set<String>> dependentBeanMap = new ConcurrentHashMap<>(64);
    
    protected boolean isDependent(String beanName, String dependentBeanName) {
        synchronized (this.dependentBeanMap) {
            return isDependent(beanName, dependentBeanName, null);
        }
    }

    private boolean isDependent(String beanName, String dependentBeanName, @Nullable Set<String> alreadySeen) {
        // 已经检查过的直接跳过
        if (alreadySeen != null && alreadySeen.contains(beanName)) {
            return false;
        }
        // 1.将别名解析为真正的名称
        String canonicalName = canonicalName(beanName);
        // 2.拿到依赖canonicalName的beanName集合
        Set<String> dependentBeans = this.dependentBeanMap.get(canonicalName);
        // 3.如果dependentBeans为空,则两者必然还未确定依赖关系,返回false
        if (dependentBeans == null) {
            return false;
        }
        // 4.如果dependentBeans包含dependentBeanName,则表示两者已确定依赖关系,返回true
        if (dependentBeans.contains(dependentBeanName)) {
            return true;
        }
        // 5.循环检查,即检查依赖canonicalName的所有beanName是否存在被dependentBeanName依赖的(即隔层依赖)
        for (String transitiveDependency : dependentBeans) {
            if (alreadySeen == null) {
                alreadySeen = new HashSet<>();
            }
            // 6.已经检查过的添加到alreadySeen,避免重复检查
            alreadySeen.add(beanName);
            if (isDependent(transitiveDependency, dependentBeanName, alreadySeen)) {
                return true;
            }
        }
        return false;
    }
}

这边引入了一个缓存 dependentBeanMap:beanName -> 所有依赖 beanName 对应的 bean 的 beanName 集合。内容比较简单,就是检查依赖 beanName 的集合中是否包含 dependentBeanName,隔层依赖也算。例如:A 依赖了 B,B 依赖了 C,则 A 也算依赖了 C。

registerDependentBean

将dep和beanName的依赖关系注册到缓存中

public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {

    /** Map between dependent bean names: bean name to Set of dependent bean names. */
    //dependentBeanMap(被依赖关系:key被value所依赖)
    private final Map<String, Set<String>> dependentBeanMap = new ConcurrentHashMap<>(64);

    /** Map between depending bean names: bean name to Set of bean names for the bean's dependencies. */
    //依赖关系:key依赖于value
    private final Map<String, Set<String>> dependenciesForBeanMap = new ConcurrentHashMap<>(64);

    public void registerDependentBean(String beanName, String dependentBeanName) {
        // 1.解析别名
        String canonicalName = canonicalName(beanName);

        // computeIfAbsent:若key对应的value为空,会将第二个参数的返回值存入并返回
        //dependentBeanMap中存放着当前Bean被引用的Bean的集合
        //比如当前需要实例化的是Bean的名字是userInfo,userInfo中有个Human类型的属性human,
        // 那么就有human被userInfo引用的关系 human=[userInfo]
        synchronized (this.dependentBeanMap) {
            //dependenciesForBeanMap中存放的是当前Bean所依赖的Bean的集合
            Set<String> dependentBeans =
                    this.dependentBeanMap.computeIfAbsent(canonicalName, k -> new LinkedHashSet<>(8));
            // 3.如果dependentBeans包含dependentBeanName,则表示依赖关系已经存在,直接返回
            if (!dependentBeans.add(dependentBeanName)) {
                return;
            }
        }

        // 4.如果依赖关系还没有注册,则将两者的关系注册到dependentBeanMap和dependenciesForBeanMap缓存
        synchronized (this.dependenciesForBeanMap) {
            Set<String> dependenciesForBean =
                    this.dependenciesForBeanMap.computeIfAbsent(dependentBeanName, k -> new LinkedHashSet<>(8));
            dependenciesForBean.add(canonicalName);
        }
    }
}

这边又引入了一个跟 dependentBeanMap 类似的缓存,dependenciesForBeanMap:beanName -> beanName 对应的 bean 依赖的所有 bean 的 beanName 集合。

这两个缓存很容易搞混,举个简单例子:例如 B 依赖了 A,则 dependentBeanMap 缓存中应该存放一对映射:其中 key 为 A,value 为含有 B 的 Set;而 dependenciesForBeanMap 缓存中也应该存放一对映射:其中 key 为:B,value 为含有 A 的 Set。

getSingleton

scope为singleton的bean创建(新建了一个ObjectFactory,并且重写了getObject方法)

public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {

    public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
        Assert.notNull(beanName, "Bean name must not be null");
        // 1.加锁,避免重复创建单例对象
        synchronized (this.singletonObjects) {
            // 2.首先检查beanName对应的bean实例是否在缓存中存在,如果已经存在,则直接返回
            Object singletonObject = this.singletonObjects.get(beanName);
            if (singletonObject == null) {
                // 3.beanName对应的bean实例不存在于缓存中,则进行Bean的创建
                if (this.singletonsCurrentlyInDestruction) {
                    // 4.当bean工厂的单例处于destruction状态时,不允许进行单例bean创建,抛出异常
                    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 + "'");
                }
                // 5.创建单例前的操作
                beforeSingletonCreation(beanName);
                boolean newSingleton = false;
                // suppressedExceptions用于记录异常相关信息
                boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
                if (recordSuppressedExceptions) {
                    this.suppressedExceptions = new LinkedHashSet<>();
                }
                try {
                    // 6.执行singletonFactory的getObject方法获取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;
                    }
                    // 7.创建单例后的操作
                    afterSingletonCreation(beanName);
                }
                if (newSingleton) {
                    // 8.如果是新的单例对象,将beanName和对应的bean实例添加到缓存中(singletonObjects、registeredSingletons)
                    addSingleton(beanName, singletonObject);
                }
            }
            // 9.返回创建出来的单例对象
            return singletonObject;
        }
    }
}

beforeSingletonCreation、afterSingletonCreation

public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {

    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 注入的循环依赖、通过属性注入的循环依赖。

addSingleton

如果是新的单例对象,将beanName和对应的bean实例添加到缓存中(singletonObjects、registeredSingletons)

public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {

    /**
     * Add the given singleton object to the singleton cache of this factory.
     * <p>To be called for eager registration of singletons.
     * 添加给定单例对象到工厂的单例缓存中
     * 用来被提早注册的单例调用
     * @param beanName the name of the bean
     * @param singletonObject the singleton object
     */
    protected void addSingleton(String beanName, Object singletonObject) {
        synchronized (this.singletonObjects) {
            //singletonObjects是一个ConcurrentHashMap
            // 1.添加到单例对象缓存,用来缓存单例对象,Bean实例完成创建之后,只保留一级缓存以及注册beanName的顺序,其余的清除
            this.singletonObjects.put(beanName, singletonObject);

            //singletonFactories是一个HashMap
            // 2.将单例工厂缓存移除(已经不需要)里面缓存着单例工厂
            this.singletonFactories.remove(beanName);

            //earlySingletonObjects是一个HashMap
            // 3.将早期单例对象缓存移除(已经不需要)
            this.earlySingletonObjects.remove(beanName);

            //registeredSingletons是一个LinkedHashSet
            // 4.添加到已经注册的单例对象缓存,被注册单例的集合,以注册的顺序包含着bean name
            this.registeredSingletons.add(beanName);
        }
    }
}

beforePrototypeCreation、afterPrototypeCreation

beforePrototypeCreation:创建实例前的操作(将beanName保存到prototypesCurrentlyInCreation缓存中)

afterPrototypeCreation:创建实例后的操作(将创建完的beanName从prototypesCurrentlyInCreation缓存中移除)

public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {

    protected void beforePrototypeCreation(String beanName) {
        // 1.拿到当前线程中正在创建的prototype的bean的beanName集合
        Object curVal = this.prototypesCurrentlyInCreation.get();
        // 2.如果为空,则将ThreadLocal设置成当前的beanName
        if (curVal == null) {
            this.prototypesCurrentlyInCreation.set(beanName);
        }
        // 3.如果不为空,并且是String类型,则代表目前只有一个beanName,将之前和当前的一起封装成Set<String>,设置到ThreadLocal中
        else if (curVal instanceof String) {
            Set<String> beanNameSet = new HashSet<>(2);
            beanNameSet.add((String) curVal);
            beanNameSet.add(beanName);
            this.prototypesCurrentlyInCreation.set(beanNameSet);
        }
        // 4.如果不为空,并且不是String,则必然是Set<String>类型,将当前的beanName加到Set中去
        else {
            Set<String> beanNameSet = (Set<String>) curVal;
            beanNameSet.add(beanName);
        }
    }

    protected void afterPrototypeCreation(String beanName) {
        // 1.拿到当前线程中正在创建的prototype的bean的beanName集合
        Object curVal = this.prototypesCurrentlyInCreation.get();
        // 2.如果是String类型,则代表目前只有一个beanName,则直接移除
        if (curVal instanceof String) {
            this.prototypesCurrentlyInCreation.remove();
        }
        else if (curVal instanceof Set) {
            // 3.如果是Set类型,则从Set从移除beanName
            Set<String> beanNameSet = (Set<String>) curVal;
            beanNameSet.remove(beanName);
            if (beanNameSet.isEmpty()) {
                this.prototypesCurrentlyInCreation.remove();
            }
        }
    }
}

该方法主要是在进行 bean 实例的创建前,将 beanName 添加到 prototypesCurrentlyInCreation 缓存;bean 实例创建后,将 beanName 从 prototypesCurrentlyInCreation 缓存中移除。这边 prototypesCurrentlyInCreation 存放的类型为 Object,在只有一个 beanName 的时候,直接存该 beanName,也就是 String 类型;当有多个 beanName 时,转成 Set 来存放。

总结:

本文介绍了获取 bean 实例的大部分内容,包括先从缓存中检查、 FactoryBean 的 bean 创建、实例化自己的依赖(depend-on 属性)、创建 bean 实例的前后一些标记等,在下篇文章中,将解析创建 bean 的内容。

参考:
https://zhuanlan.zhihu.com/p/83489878

相关文章

网友评论

      本文标题:Spring5IOC容器解析——getBean()方法分析

      本文链接:https://www.haomeiwen.com/subject/eerufktx.html