spring--2--bean的生命周期

spring–bean的生命周期

文章目录


上篇文章我们粗略的说了 spring的启动流程,从这篇文章开始,我们就来探讨一下容器的功能细节。

1 什么是bean的生命周期?

简单来说就是从bean开始创建到bean被销毁的过程称为bean的生命周期。概念知道了,那么spring在这个过程中具体做了哪些事呢?这就是这篇文章要说的内容。

2 预实例化bean

spring在启动过程中会将所有非懒加载的单例bean提前实例化,直接定位到DefaultListableBeanFactory类的preInstantiateSingletons方法

public void preInstantiateSingletons() throws BeansException {
    if (logger.isTraceEnabled()) {
        logger.trace("Pre-instantiating singletons in " + this);
    }

    // Iterate over a copy to allow for init methods which in turn register new bean definitions.
    // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
    //beanDefinitionNames的副本
    List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);

    // Trigger initialization of all non-lazy singleton beans...
    //遍历beanDefinitionNames,获取对应的beanDefinition,然后实例化
    for (String beanName : beanNames) {
        //根据beanName获取本地的已经合并的beanDefinition
        RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
        // 非抽象,单例,非懒加载
        if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
            //是FactoryBean
            if (isFactoryBean(beanName)) {
                //名字前加&符,先创建工厂对象
                Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
                if (bean instanceof FactoryBean) {
                    FactoryBean<?> factory = (FactoryBean<?>) bean;
                    boolean isEagerInit;
                    if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
                        isEagerInit = AccessController.doPrivileged(
                            (PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit,
                            getAccessControlContext());
                    }
                    else {
                        //SmartFactoryBean接口可以指定是否需要提前实例化化FactoryBean创建的对象
                        isEagerInit = (factory instanceof SmartFactoryBean &&
                                       ((SmartFactoryBean<?>) factory).isEagerInit());
                    }
                    if (isEagerInit) {
                        //实例化beanName对应的bean
                        getBean(beanName);
                    }
                }
            }
            //非FactoryBean
            else {
                //直接实例化beanName对应的bean
                getBean(beanName);
            }
        }
    }

    // Trigger post-initialization callback for all applicable beans...
    //遍历调用所有实现了SmartInitializingSingleton接口的bean的afterSingletonsInstantiated方法
    for (String beanName : beanNames) {
        Object singletonInstance = getSingleton(beanName);
        if (singletonInstance instanceof SmartInitializingSingleton) {
            SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
            if (System.getSecurityManager() != null) {
                AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
                    smartSingleton.afterSingletonsInstantiated();
                    return null;
                }, getAccessControlContext());
            }
            else {
                smartSingleton.afterSingletonsInstantiated();
            }
        }
    }
}

总结:

  1. 容器调用getBean(beanName)方法来实例化bean
  2. 当所有的bean实例化完成后才会调用实现了SmartInitializingSingleton接口beanafterSingletonsInstantiated方法。
  3. 如果beanName对应的BeanDefinition是一个FactoryBean,那么会先创建FactoryBean对象,然后再判断是否需要提前初始化FactoryBean创建的对象。

3 getBean(beanName)实例化bean

//空方法,接着调用doGetBean,这个方法才是实例化的
public Object getBean(String name) throws BeansException {
    return doGetBean(name, null, null, false);
}

实例化

/**
 * Return an instance, which may be shared or independent, of the specified bean.
 * @param name the name of the bean to retrieve
 * @param requiredType the required type of the bean to retrieve
 * @param args arguments to use when creating a bean instance using explicit arguments
 * (only applied when creating a new instance as opposed to retrieving an existing one)
 * @param typeCheckOnly whether the instance is obtained for a type check,
 * not for actual use
 * @return an instance of the bean
 * @throws BeansException if the bean could not be created
 */
@SuppressWarnings("unchecked")
protected <T> T doGetBean(
    String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
    throws BeansException {

    //去&符
    String beanName = transformedBeanName(name);
    Object bean;

    // Eagerly check singleton cache for manually registered singletons.
    //先从容器中拿该beanName对应的bean对象,见3.1
    Object sharedInstance = getSingleton(beanName);

    //该beanName对应的bean已经实例化了
    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 + "'");
            }
        }
        //根据用户需要来获取bean对象(是要FactoryBean还是FactoryBean生产的对象,name决定),见3.2
        bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
    }
    //该beanName对应的bean还没有实例化
    else {

        // Fail if we're already creating this bean instance:
        // We're assumably within a circular reference.
        /**
         * 判断一个prototype bean是否产生了循环依赖,去set集合判断是否已经存在这个beanName,
         * 存在就是循环依赖。原理很简单,容器中有一个set集合(主要是为了set集合不会重复的特性),
         * 每次需要创建一个beanName对应实例的时候,先去set集合判断是否已经存在这个beanName,
         * 存在就报循环依赖异常,否则就去检测该bean是否有依赖的bean,有就将该依赖bean的beanName
         * 加入到set集合中,然后去创建这个依赖的Bean,重复上述流程,没有就实例化该bean,
         * 然后清空set集合
         */
        if (isPrototypeCurrentlyInCreation(beanName)) {
            throw new BeanCurrentlyInCreationException(beanName);
        }

        // Check if bean definition exists in this factory.
        //当前容器中不存在beanName对应的BeanDefinition,就去父容器中检查
        BeanFactory parentBeanFactory = getParentBeanFactory();
        if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
            // Not found -> check parent.
            String nameToLookup = originalBeanName(name);
            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);
            }
        }

        //将该bean标记为已创建,见3.3
        if (!typeCheckOnly) {
            markBeanAsCreated(beanName);
        }

        try {
            //获取当前容器中beanName对应的已经合并的BeanDefinition
            RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
            checkMergedBeanDefinition(mbd, beanName, args);

            // Guarantee initialization of beans that the current bean depends on.
            //获取当前bean依赖的bean的名字(就是@DependsOn,或者xml中的depend-on="xxx")
            String[] dependsOn = mbd.getDependsOn();
            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 + "'");
                    }
                    //去实例化依赖的Bean
                    registerDependentBean(dep, beanName);
                    try {
                        getBean(dep);
                    }
                    catch (NoSuchBeanDefinitionException ex) {
                        throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                                        "'" + beanName + "' depends on missing bean '" + dep + "'", ex);
                    }
                }
            }

            /**************************单例bean实例化*******************************/
            // Create bean instance.
            //单例bean实例化
            if (mbd.isSingleton()) {
                /**
                 *	第二个参数是ObjectFactory,spring中第三级缓存中保存的就是这个对象
                 *	getSingleton方法就是调用这个ObjectFactory来创建对象
                 *	该方法里面有spring解决循环依赖的方法
                 *	beforeSingletonCreation 在此方法中记录创建的对象的名字
                 *	afterSingletonCreation 对象创建成功,删掉名字
                 *	addSingleton 对象创建成功,缓存对象,见3.4
                 */
                sharedInstance = getSingleton(beanName, () -> {
                    try {
                        //ObjectFactory调用createBean方法来创建对象,见3.5
                        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;
                    }
                });
                //根据用户需要来获取bean对象,见3.2
                bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
            }

            /**************************prototype bean实例化***************************/
            else if (mbd.isPrototype()) {
                // It's a prototype -> create a new instance.
                Object prototypeInstance = null;
                try {
                    //对当前正在创建的bean做一个记录(循环依赖问题)
                    beforePrototypeCreation(beanName);
                    //调用createBean方法来创建对象,见3.5
                    prototypeInstance = createBean(beanName, mbd, args);
                }
                finally {
                    //消除记录
                    afterPrototypeCreation(beanName);
                }
                bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
            }

            /**************************自定义scope的bean创建过程**************************/
            else {
                String scopeName = mbd.getScope();
                if (!StringUtils.hasLength(scopeName)) {
                    throw new IllegalStateException("No scope name defined for bean ´" + beanName + "'");
                }
                //获取当前bean的scope
                Scope scope = this.scopes.get(scopeName);
                if (scope == null) {
                    throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
                }
                try {
                    /**
                     * get方法的第二个参数又是一个ObjectFactory工厂
                     * 自定义作用域为何能在get方法中创建对象,就是借助了工厂
                     * 里面的createBean方法
                     * 调用的方法是不是和prototype差不多
					 */
                    Object scopedInstance = scope.get(beanName, () -> {
                        beforePrototypeCreation(beanName);
                        try {
                            //ObjectFactory调用createBean方法来创建对象,见3.5
                            return createBean(beanName, mbd, args);
                        }
                        finally {
                            afterPrototypeCreation(beanName);
                        }
                    });
                    //根据用户需要来获取bean对象,见3.2
                    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.
    //检查所需类型与实际类型是否匹配
    //requiredType 我们在调用getBean方法获取容器实例的时候可以自己指定
    if (requiredType != null && !requiredType.isInstance(bean)) {
        try {
            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());
        }
    }
    return (T) bean;
}

总结:在spring中,除了FactoryBean创建的对象,其它对象的创建都是调用createBean方法

3.1 getSingleton(beanName)方法(获取容器中beanName对应的已经实例化的bean对象)

@Override
@Nullable
public Object getSingleton(String beanName) {
    return getSingleton(beanName, true);
}


/**
 * Return the (raw) singleton object registered under the given name.
 * <p>Checks already instantiated singletons and also allows for an early
 * reference to a currently created singleton (resolving a circular reference).
 * @param beanName the name of the bean to look for
 * @param allowEarlyReference whether early references should be created or not
 * @return the registered singleton object, or {@code null} if none found
 */
@Nullable
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
    // Quick check for existing instance without full singleton lock
    //先从第1,2级缓存中获取bean对象
    Object singletonObject = this.singletonObjects.get(beanName);
    if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
        singletonObject = this.earlySingletonObjects.get(beanName);
        //只有第1,2级缓存中获取不到,并且允许早期引用,才可以从第3级缓存中得到bean对象
        if (singletonObject == null && allowEarlyReference) {
            synchronized (this.singletonObjects) {
                // Consistent creation of early reference within full singleton lock
                singletonObject = this.singletonObjects.get(beanName);
                if (singletonObject == null) {
                    singletonObject = this.earlySingletonObjects.get(beanName);
                    if (singletonObject == null) {
                        ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
                        if (singletonFactory != null) {
                            //调用第3级缓存中的ObjectFactory的getObject方法来获取bean对象
                            singletonObject = singletonFactory.getObject();
                            //放入第2级缓存中
                            this.earlySingletonObjects.put(beanName, singletonObject);
                            //删掉第3级缓存
                            this.singletonFactories.remove(beanName);
                        }
                    }
                }
            }
        }
    }
    return singletonObject;
}

总结:

  • 这个方法会获取到容器中beanName对应的bean对象
  • 要想从第2级缓存中获取早期暴露的对象,那么该对象必须正在创建中(创建成功了,对象会被缓存到第1级缓存中,见3.4.2
  • allowEarlyReference必须为true,即允许早期引用,才会从第3级缓存中的ObjectFactorygetObject方法来获取bean对象

3.2 getObjectForBeanInstance方法(根据用户需要来获取相应的bean对象)

/**
 * Overridden in order to implicitly register the currently created bean as
 * dependent on further beans getting programmatically retrieved during a
 * {@link Supplier} callback.
 * @since 5.0
 * @see #obtainFromSupplier
 * 这是spring5.0重写的方法, 增加了对Supplier的支持
 */
@Override
protected Object getObjectForBeanInstance(
    Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) {

    //Supplier相关,直接忽略掉
    String currentlyCreatedBean = this.currentlyCreatedBean.get();
    if (currentlyCreatedBean != null) {
        registerDependentBean(beanName, currentlyCreatedBean);
    }

    //重点是这个方法
    return super.getObjectForBeanInstance(beanInstance, name, beanName, mbd);
}

父类的getObjectForBeanInstance方法

/**
 * Get the object for the given bean instance, either the bean
 * instance itself or its created object in case of a FactoryBean.
 * @param beanInstance the shared bean instance
 * @param name the name that may include factory dereference prefix
 * @param beanName the canonical bean name
 * @param mbd the merged bean definition
 * @return the object to expose for the bean
 * 如果是普通bean,就直接返回对象,如果是工厂,就根据名字决定返回工厂还是工厂创建的对象
 */
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.
    //name以&开头,说明需要FactoryBean对象
    if (BeanFactoryUtils.isFactoryDereference(name)) {
        if (beanInstance instanceof NullBean) {
            return beanInstance;
        }
        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.
    //name不以&开头,并且对象不是FactoryBean,直接返回
    if (!(beanInstance instanceof FactoryBean)) {
        return beanInstance;
    }

    Object object = null;
    if (mbd != null) {
        mbd.isFactoryBean = true;
    }
    //先从缓存中获取FactoryBean创建的对象
    else {
        
        object = getCachedObjectForFactoryBean(beanName);
    }
    //缓存中获取不到FactoryBean创建的对象
    if (object == null) {
        // Return bean instance from factory.
        FactoryBean<?> factory = (FactoryBean<?>) beanInstance;
        // Caches object obtained from FactoryBean if it is a singleton.
        if (mbd == null && containsBeanDefinition(beanName)) {
            mbd = getMergedLocalBeanDefinition(beanName);
        }
        boolean synthetic = (mbd != null && mbd.isSynthetic());
        //调用FactoryBean对象的getObject方法创建对象,并缓存
        object = getObjectFromFactoryBean(factory, beanName, !synthetic);
    }
    return object;
}
3.2.1 从缓存中获取FactoryBean生产的对象
//缓存着所有FactoryBean生产的对象
private final Map<String, Object> factoryBeanObjectCache = new ConcurrentHashMap<>(16);


/**
 * Obtain an object to expose from the given FactoryBean, if available
 * in cached form. Quick check for minimal synchronization.
 * @param beanName the name of the bean
 * @return the object obtained from the FactoryBean,
 * or {@code null} if not available
 */
@Nullable
protected Object getCachedObjectForFactoryBean(String beanName) {
    //从缓存中获取beanName对应的bean对象
    return this.factoryBeanObjectCache.get(beanName);
}
3.2.2 调用FactoryBean对象的getObject方法创建对象,并缓存
/**
 * Obtain an object to expose from the given FactoryBean.
 * @param factory the FactoryBean instance
 * @param beanName the name of the bean
 * @param shouldPostProcess whether the bean is subject to post-processing
 * @return the object obtained from the FactoryBean
 * @throws BeanCreationException if FactoryBean object creation failed
 * @see org.springframework.beans.factory.FactoryBean#getObject()
 */
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) {
                //调用FactoryBean对象的getObject方法获取bean对象
                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) {
                    object = alreadyThere;
                }
                else {
                    //这个增强的过程直接跳过,synthetic相关,不用管,我们不会进入下面这个流程
                    if (shouldPostProcess) {
                        if (isSingletonCurrentlyInCreation(beanName)) {
                            // Temporarily return non-post-processed object, not storing it yet..
                            return object;
                        }
                        beforeSingletonCreation(beanName);
                        try {
                            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;
        }
    }
    //FactoryBean对象非单例的,或者没有注册到容器,此时不缓存
    else {
        Object object = doGetObjectFromFactoryBean(factory, beanName);
        if (shouldPostProcess) {
            try {
                object = postProcessObjectFromFactoryBean(object, beanName);
            }
            catch (Throwable ex) {
                throw new BeanCreationException(beanName, "Post-processing of FactoryBean's object failed", ex);
            }
        }
        return object;
    }
}

最终在doGetObjectFromFactoryBean方法中调用了FactoryBean对象的getObject方法创建对象

/**
 * Obtain an object to expose from the given FactoryBean.
 * @param factory the FactoryBean instance
 * @param beanName the name of the bean
 * @return the object obtained from the FactoryBean
 * @throws BeanCreationException if FactoryBean object creation failed
 * @see org.springframework.beans.factory.FactoryBean#getObject()
 */
private Object doGetObjectFromFactoryBean(FactoryBean<?> factory, String beanName) throws BeanCreationException {
    Object object;
    try {
        if (System.getSecurityManager() != null) {
            AccessControlContext acc = getAccessControlContext();
            try {
                object = AccessController.doPrivileged((PrivilegedExceptionAction<Object>) factory::getObject, acc);
            }
            catch (PrivilegedActionException pae) {
                throw pae.getException();
            }
        }
        else {
            //调用了getObject生产对象
            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.
    //用NullBean对象来替换FactoryBean生产的null
    if (object == null) {
        if (isSingletonCurrentlyInCreation(beanName)) {
            throw new BeanCurrentlyInCreationException(
                beanName, "FactoryBean which is currently in creation returned null from getObject");
        }
        object = new NullBean();
    }
    return object;
}

总结:这个方法完成用户需要FactoryBean本身还是它生产的对象的逻辑判断,最终返回用户需要的bean对象

3.3 markBeanAsCreated方法(标记beanName对应的bean为已创建)

/**
 * Mark the specified bean as already created (or about to be created).
 * <p>This allows the bean factory to optimize its caching for repeated
 * creation of the specified bean.
 * @param beanName the name of the bean
 */
protected void markBeanAsCreated(String beanName) {
    if (!this.alreadyCreated.contains(beanName)) {
        synchronized (this.mergedBeanDefinitions) {
            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对应的已合并的BeanDefinition
                clearMergedBeanDefinition(beanName);
                //记录已经创建的bean对象的名字
                this.alreadyCreated.add(beanName);
            }
        }
    }
}

@Override
protected void clearMergedBeanDefinition(String beanName) {
    //设置需要重新合并
    super.clearMergedBeanDefinition(beanName);
    //删除缓存
    this.mergedBeanDefinitionHolders.remove(beanName);
}


/*****************************************父类****************************************/
/**
 * Remove the merged bean definition for the specified bean,
 * recreating it on next access.
 * @param beanName the bean name to clear the merged definition for
 */
protected void clearMergedBeanDefinition(String beanName) {
    RootBeanDefinition bd = this.mergedBeanDefinitions.get(beanName);
    //将stale属性置为true,表明需要重新合并
    if (bd != null) {
        bd.stale = true;
    }
}

记录已创建的bean的名字,方便后续使用

3.4 getSingleton(String beanName, ObjectFactory<?> singletonFactory)(解决循环依赖,并使用ObjectFactory创建对象)

/**
 * Return the (raw) singleton object registered under the given name,
 * creating and registering a new one if none registered yet.
 * @param beanName the name of the bean
 * @param singletonFactory the ObjectFactory to lazily create the singleton
 * with, if necessary
 * @return the registered singleton object
 */
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
    Assert.notNull(beanName, "Bean name must not be null");
    synchronized (this.singletonObjects) {
        //先从单例对象池中获取beanName对应的对象
        Object singletonObject = this.singletonObjects.get(beanName);
        //beanName对象还没有创建
        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 + "'");
            }
            //创建之前将beanName放入set集合中
            beforeSingletonCreation(beanName);
            boolean newSingleton = false;
            boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
            if (recordSuppressedExceptions) {
                this.suppressedExceptions = new LinkedHashSet<>();
            }
            try {
                //调用ObjectFactory的getObject方法来创建对象
                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;
                }
                //创建成功之后将beanName从set集合移除
                afterSingletonCreation(beanName);
            }
            //缓存ObjectFactory创建的对象,见3.4.2
            if (newSingleton) {
                addSingleton(beanName, singletonObject);
            }
        }
        return singletonObject;
    }
}
3.4.1 spring解决循环依赖所做的工作
//记录了当前正在创建的对象及其依赖对象的beanName
private final Set<String> singletonsCurrentlyInCreation =
    Collections.newSetFromMap(new ConcurrentHashMap<>(16));

//记录了不需要检查循环依赖的对象的beanName
private final Set<String> inCreationCheckExclusions =
    Collections.newSetFromMap(new ConcurrentHashMap<>(16));

/**
 * Callback before singleton creation.
 * <p>The default implementation register the singleton as currently in creation.
 * @param beanName the name of the singleton about to be created
 * @see #isSingletonCurrentlyInCreation
 */
protected void beforeSingletonCreation(String beanName) {
    /**
     * 先检查inCreationCheckExclusions集合中是否已经包含了该beanName,不包含说明
     * 该beanName需要检查循环依赖,就将该beanName添加到singletonsCurrentlyInCreation集合中
     * set集合是无法添加相同值的元素的,所以如果singletonsCurrentlyInCreation集合已经添加过该
     * beanName,那么add方法就会返回false,表明当前循环依赖
     */
    if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.add(beanName)) {
        throw new BeanCurrentlyInCreationException(beanName);
    }
}


/**
 * Callback after singleton creation.
 * <p>The default implementation marks the singleton as not in creation anymore.
 * @param beanName the name of the singleton that has been created
 * @see #isSingletonCurrentlyInCreation
 */
protected void afterSingletonCreation(String beanName) {
    /**
     * 先检查inCreationCheckExclusions集合中是否已经包含了该beanName,不包含说明
     * 该beanName需要检查循环依赖,就将该beanName从singletonsCurrentlyInCreation集合中移除
     */
    if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.remove(beanName)) {
        throw new IllegalStateException("Singleton '" + beanName + "' isn't currently in creation");
    }
}

总结:

  • 每次在对象创建之间记录beanNamesingletonsCurrentlyInCreation集合中,创建完毕从这个set集合中删除beanName
  • 如果在对象创建之前调用singletonsCurrentlyInCreation集合的add方法发现返回值为false,那么表明这个beanName对应的对象正在创建中,现在又要再次创建,循环依赖了。
3.4.2 addSingleton(beanName, singletonObject)(缓存ObjectFactory创建的对象)
/**
 * 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) {
        //保存到单例对象池中
        this.singletonObjects.put(beanName, singletonObject);
        //删除三级缓存值
        this.singletonFactories.remove(beanName);
        //删除二级缓存值
        this.earlySingletonObjects.remove(beanName);
        //缓存已注册到容器中的对象的beanName
        this.registeredSingletons.add(beanName);
    }
}

3.5 createBean(beanName, mbd, args)方法创建对象

/**
 * Central method of this class: creates a bean instance,
 * populates the bean instance, applies post-processors, etc.
 * @see #doCreateBean
 * ObjectFactory接口方法通过调用createBean方法实例化对象
 */
@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
    throws BeanCreationException {

    if (logger.isTraceEnabled()) {
        logger.trace("Creating instance of bean '" + beanName + "'");
    }
    RootBeanDefinition mbdToUse = mbd;

    // Make sure bean class is actually resolved at this point, and
    // clone the bean definition in case of a dynamically resolved Class
    // which cannot be stored in the shared merged bean definition.
    //解析用于当前创建bean的class对象,见3.5.1
    Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
    /**
     * 类名中包含了SpEL表达式,此时是动态加载到虚拟机的,不能将clazz对象保存到BeanDefinition中
     * 应为SpEL表达式的值可以改变,所以每次需要clazz对象的时候,应该重新解析。
     * 此处重新拷贝一份BeanDefinition,然后将动态解析到clazz对象设置进去,
     * 不改变原来的BeanDefinition。
     */
    if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
        mbdToUse = new RootBeanDefinition(mbd);
        mbdToUse.setBeanClass(resolvedClass);
    }

    // Prepare method overrides.
    //方法重写,look-up和replace-up功能实现
    try {
        mbdToUse.prepareMethodOverrides();
    }
    catch (BeanDefinitionValidationException ex) {
        throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
                                               beanName, "Validation of method overrides failed", ex);
    }

    try {
        // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
        //InstantiationAwareBeanPostProcessor返回一个代理对象来代替目标对象,见3.5.2
        Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
        //创建了代理对象,直接返回,跳过后面的生命周期流程
        if (bean != null) {
            return bean;
        }
    }
    catch (Throwable ex) {
        throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
                                        "BeanPostProcessor before instantiation of bean failed", ex);
    }

    //经过上面一系列判断之后,开始创建bean
    try {
        //实例化bean,见3.5.3
        Object beanInstance = doCreateBean(beanName, mbdToUse, args);
        if (logger.isTraceEnabled()) {
            logger.trace("Finished creating instance of bean '" + beanName + "'");
        }
        return beanInstance;
    }
    catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
        // A previously detected exception with proper bean creation context already,
        // or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.
        throw ex;
    }
    catch (Throwable ex) {
        throw new BeanCreationException(
            mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
    }
}

3.5.1resolveBeanClass(mbd, beanName)(解析用于当前创建beanclass对象)
/**
 * Resolve the bean class for the specified bean definition,
 * resolving a bean class name into a Class reference (if necessary)
 * and storing the resolved Class in the bean definition for further use.
 * @param mbd the merged bean definition to determine the class for
 * @param beanName the name of the bean (for error handling purposes)
 * @param typesToMatch the types to match in case of internal type matching purposes
 * (also signals that the returned {@code Class} will never be exposed to application code)
 * @return the resolved bean class (or {@code null} if none)
 * @throws CannotLoadBeanClassException if we failed to load the class
 */
@Nullable
protected Class<?> resolveBeanClass(RootBeanDefinition mbd, String beanName, Class<?>... typesToMatch)
    throws CannotLoadBeanClassException {

    try {
        //mbd对应的类已经加载到JVM中,见3.5.1.1
        if (mbd.hasBeanClass()) {
            //直接获取beanClass
            return mbd.getBeanClass();
        }
        if (System.getSecurityManager() != null) {
            return AccessController.doPrivileged((PrivilegedExceptionAction<Class<?>>)
                                                 () -> doResolveBeanClass(mbd, typesToMatch), getAccessControlContext());
        }
        //mbd对应的类没有加载到JVM中,开始解析,见3.5.1.2
        else {
            return doResolveBeanClass(mbd, typesToMatch);
        }
    }
    catch (PrivilegedActionException pae) {
        ClassNotFoundException ex = (ClassNotFoundException) pae.getException();
        throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), ex);
    }
    catch (ClassNotFoundException ex) {
        throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), ex);
    }
    catch (LinkageError err) {
        throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), err);
    }
}
3.5.1.1 判断mbd对应的类是否已经加载到JVM
//可以是完全限定名,也可以是clazz对象
@Nullable
private volatile Object beanClass;

/**
 * Return whether this definition specifies a bean class.
 * @see #getBeanClass()
 * @see #setBeanClass(Class)
 * @see #resolveBeanClass(ClassLoader)
 */
public boolean hasBeanClass() {
    //beanClass是clazz对象,表明已经加载到虚拟机
    return (this.beanClass instanceof Class);
}

某个类只有需要被使用的时候,才会加载到虚拟机中,节省虚拟机内存

3.5.1.2 开始解析mbd对应的类,并加载到JVM
@Nullable
private Class<?> doResolveBeanClass(RootBeanDefinition mbd, Class<?>... typesToMatch)
    throws ClassNotFoundException {

    //获取当前工厂的类加载器
    ClassLoader beanClassLoader = getBeanClassLoader();
    ClassLoader dynamicLoader = beanClassLoader;
    boolean freshResolve = false;

    if (!ObjectUtils.isEmpty(typesToMatch)) {
        // When just doing type checks (i.e. not creating an actual instance yet),
        // use the specified temporary class loader (e.g. in a weaving scenario).
        ClassLoader tempClassLoader = getTempClassLoader();
        if (tempClassLoader != null) {
            dynamicLoader = tempClassLoader;
            freshResolve = true;
            if (tempClassLoader instanceof DecoratingClassLoader) {
                DecoratingClassLoader dcl = (DecoratingClassLoader) tempClassLoader;
                for (Class<?> typeToMatch : typesToMatch) {
                    dcl.excludeClass(typeToMatch.getName());
                }
            }
        }
    }

    /**
     * 获取完全限定名,实际上就是RootBeanDefinition的beanClass属性
     * 如果beanClass是clazz类型,则返回((Class<?>) beanClass).getName()
     * 否则直接返回beanClass
     */
    String className = mbd.getBeanClassName();
    if (className != null) {
        //(1)解析className上的SpEL表达式
        Object evaluated = evaluateBeanDefinitionString(className, mbd);
        //解析之后className不相同
        if (!className.equals(evaluated)) {
            // A dynamically resolved expression, supported as of 4.2...
            if (evaluated instanceof Class) {
                return (Class<?>) evaluated;
            }
            //freshResolve置为true,需要加载解析后className
            else if (evaluated instanceof String) {
                className = (String) evaluated;
                freshResolve = true;
            }
            else {
                throw new IllegalStateException("Invalid class name expression result: " + evaluated);
            }
        }
        //加载解析后className,动态的,没有将clazz对象记录下来
        if (freshResolve) {
            // When resolving against a temporary class loader, exit early in order
            // to avoid storing the resolved Class in the bean definition.
            if (dynamicLoader != null) {
                try {
                    return dynamicLoader.loadClass(className);
                }
                catch (ClassNotFoundException ex) {
                    if (logger.isTraceEnabled()) {
                        logger.trace("Could not load class [" + className + "] from " + dynamicLoader + ": " + ex);
                    }
                }
            }
            return ClassUtils.forName(className, dynamicLoader);
        }
    }

    //(2)直接使用类加载器加载原始的className
    // Resolve regularly, caching the result in the BeanDefinition...
    return mbd.resolveBeanClass(beanClassLoader);
}
(1)解析className上的SpEL表达式
/**
 * Evaluate the given String as contained in a bean definition,
 * potentially resolving it as an expression.
 * @param value the value to check
 * @param beanDefinition the bean definition that the value comes from
 * @return the resolved value
 * @see #setBeanExpressionResolver
 */
@Nullable
protected Object evaluateBeanDefinitionString(@Nullable String value, @Nullable BeanDefinition beanDefinition) {
    if (this.beanExpressionResolver == null) {
        return value;
    }

    //如果当前bean使用的是自定义的Scope,那么就得到这个Scope对象
    Scope scope = null;
    if (beanDefinition != null) {
        String scopeName = beanDefinition.getScope();
        if (scopeName != null) {
            scope = getRegisteredScope(scopeName);
        }
    }
    //解析value中的SpEL表达式
    return this.beanExpressionResolver.evaluate(value, new BeanExpressionContext(this, scope));
}
(2)直接使用类加载器加载原始的className
/**
 * Determine the class of the wrapped bean, resolving it from a
 * specified class name if necessary. Will also reload a specified
 * Class from its name when called with the bean class already resolved.
 * @param classLoader the ClassLoader to use for resolving a (potential) class name
 * @return the resolved bean class
 * @throws ClassNotFoundException if the class name could be resolved
 */
@Nullable
public Class<?> resolveBeanClass(@Nullable ClassLoader classLoader) throws ClassNotFoundException {
    String className = getBeanClassName();
    if (className == null) {
        return null;
    }
    //加载className到JVM中
    Class<?> resolvedClass = ClassUtils.forName(className, classLoader);
    //保存clazz对象,将完全限定名换为clazz对象
    this.beanClass = resolvedClass;
    return resolvedClass;
}

最终通过ClassUtils.forName(className, classLoader)方法获取clazz对象,并且会将这个clazz对象记录到BeanDefinition中。

3.5.2 InstantiationAwareBeanPostProcessor返回一个代理对象来代替目标对象,并跳过后续的生命周期
/**
 * Apply before-instantiation post-processors, resolving whether there is a
 * before-instantiation shortcut for the specified bean.
 * @param beanName the name of the bean
 * @param mbd the bean definition for the bean
 * @return the shortcut-determined bean instance, or {@code null} if none
 */
@Nullable
protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
    Object bean = null;
    /**
     * mbd.beforeInstantiationResolved,true标识该bean已经经过
     * InstantiationAwareBeanPostProcessor处理
     */
    if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
        // Make sure bean class is actually resolved at this point.
        /**
         * 容器中有InstantiationAwareBeanPostProcessor,执行该对象的
         * postProcessBeforeInstantiation方法,见3.5.1.1
         */
        if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
            //获取给定BeanDefinition的目标类型,见3.5.2.2
            Class<?> targetType = determineTargetType(beanName, mbd);
            if (targetType != null) {
                /**
                 * 这个方法返回一个代理对象
                 * 我们完全可以在这个InstantiationAwareBeanPostProcessor的
                 * postProcessBeforeInstantiation方法中自己创建一个代理对象,
                 * 然后返回.事实上,spring的aop就是这么做的
                 */
                bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
                if (bean != null) {
                    /**
                     * 如果如果成功创建了一个代理对象,就执行InstantiationAwareBeanPostProcessor
                     * 的postProcessAfterInitialization方法,此方法中给它填充属性值
                     */
                    bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
                }
            }
        }
        //设置该bean已经经过InstantiationAwareBeanPostProcessor处理
        mbd.beforeInstantiationResolved = (bean != null);
    }

    return bean;
}
3.5.2.1 判断容器中是否已经注册过InstantiationAwareBeanPostProcessor对象
/** Indicates whether any InstantiationAwareBeanPostProcessors have been registered. */
private volatile boolean hasInstantiationAwareBeanPostProcessors;
/**
 * Return whether this factory holds a InstantiationAwareBeanPostProcessor
 * that will get applied to singleton beans on creation.
 * @see #addBeanPostProcessor
 * @see org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor
 */
protected boolean hasInstantiationAwareBeanPostProcessors() {
    return this.hasInstantiationAwareBeanPostProcessors;
}

addBeanPostProcessor方法(向容器中注册一个BeanPostProcessor

@Override
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
    Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
    // Remove from old position, if any
    //先删除以前的
    this.beanPostProcessors.remove(beanPostProcessor);
    // Track whether it is instantiation/destruction aware
    //根据BeanPostProcessor的类型修改容器的标志位
    if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
        //表明容器中有InstantiationAwareBeanPostProcessor对象
        this.hasInstantiationAwareBeanPostProcessors = true;
    }
    if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
        //表明容器中有DestructionAwareBeanPostProcessor对象
        this.hasDestructionAwareBeanPostProcessors = true;
    }
    // Add to end of list
    //所有的BeanPostProcessor对象都放在这个集合中
    this.beanPostProcessors.add(beanPostProcessor);
}

注册BeanPostProcessor的时候,就已经判断它的类型。

3.5.2.2 获取给定BeanDefinition的目标类型
/**
 * Determine the target type for the given bean definition.
 * @param beanName the name of the bean (for error handling purposes)
 * @param mbd the merged bean definition for the bean
 * @param typesToMatch the types to match in case of internal type matching purposes
 * (also signals that the returned {@code Class} will never be exposed to application code)
 * @return the type for the bean if determinable, or {@code null} otherwise
 */
@Nullable
protected Class<?> determineTargetType(String beanName, RootBeanDefinition mbd, Class<?>... typesToMatch) {
    //普通bean
    Class<?> targetType = mbd.getTargetType();
    //工厂方法创建的bean
    if (targetType == null) {
        targetType = (mbd.getFactoryMethodName() != null ?
                      getTypeForFactoryMethod(beanName, mbd, typesToMatch) :
                      resolveBeanClass(mbd, beanName, typesToMatch));
        if (ObjectUtils.isEmpty(typesToMatch) || getTempClassLoader() == null) {
            mbd.resolvedTargetType = targetType;
        }
    }
    return targetType;
}
3.5.3 doCreateBean(beanName, mbdToUse, args)方法创建bean对象
/**
 * Actually create the specified bean. Pre-creation processing has already happened
 * at this point, e.g. checking {@code postProcessBeforeInstantiation} callbacks.
 * <p>Differentiates between default bean instantiation, use of a
 * factory method, and autowiring a constructor.
 * @param beanName the name of the bean
 * @param mbd the merged bean definition for the bean
 * @param args explicit arguments to use for constructor or factory method invocation
 * @return a new instance of the bean
 * @throws BeanCreationException if the bean could not be created
 * @see #instantiateBean
 * @see #instantiateUsingFactoryMethod
 * @see #autowireConstructor
 */
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
    throws BeanCreationException {

    // Instantiate the bean.
    BeanWrapper instanceWrapper = null;
    if (mbd.isSingleton()) {
        instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
    }
    if (instanceWrapper == null) {
        //实例化bean,见3.5.3.1
        instanceWrapper = createBeanInstance(beanName, mbd, args);
    }
    //获取真正的实例
    Object bean = instanceWrapper.getWrappedInstance();
    //实际就是bean.getClass()
    Class<?> beanType = instanceWrapper.getWrappedClass();
    if (beanType != NullBean.class) {
        //设置以解析的bean的类型
        mbd.resolvedTargetType = beanType;
    }

    // Allow post-processors to modify the merged bean definition.
    /**
     * 应用MergedBeanDefinitionPostProcessor的postProcessMergedBeanDefinition方法
     * 修改当前bean的BeanDefinition
     */
    synchronized (mbd.postProcessingLock) {
        if (!mbd.postProcessed) {
            try {
                applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
            }
            catch (Throwable ex) {
                throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                                "Post-processing of merged bean definition failed", ex);
            }
            //设置已经被MergedBeanDefinitionPostProcessor处理过了
            mbd.postProcessed = true;
        }
    }

    // Eagerly cache singletons to be able to resolve circular references
    // even when triggered by lifecycle interfaces like BeanFactoryAware.
    //spring为了解决循环依赖而做的工作,见3.5.3.2
    boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
                                      isSingletonCurrentlyInCreation(beanName));
    if (earlySingletonExposure) {
        if (logger.isTraceEnabled()) {
            logger.trace("Eagerly caching bean '" + beanName +
                         "' to allow for resolving potential circular references");
        }
        /**
         * 创建一个ObjectFactory,它的getObject方法返回的是经过getEarlyBeanReference方法
         * 增强的bean(此时的bean才刚实例化完成,还没有经过属性填充和初始化)
         * 并把这个ObjectFactory工厂放入第三级缓存中
         */
        addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
    }

    // Initialize the bean instance.
    Object exposedObject = bean;
    try {
        //bean实例填充属性,见3.5.3.3
        populateBean(beanName, mbd, instanceWrapper);
        //初始化bean,见3.5.3.4
        exposedObject = initializeBean(beanName, exposedObject, mbd);
    }
    catch (Throwable ex) {
        if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
            throw (BeanCreationException) ex;
        }
        else {
            throw new BeanCreationException(
                mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
        }
    }

    //允许提前暴露单例bean
    if (earlySingletonExposure) {
        /**
         * allowEarlyReference为false,表示不允许使用ObjectFactory来获取早期的单例对象
         * getSingleton(beanName, false)从1,2,3级缓存中获取beanName对应的对象
         * 注意,第3级缓存中保存的是ObjectFactory,此时被禁用了,见3.1
         */
        Object earlySingletonReference = getSingleton(beanName, false);
        if (earlySingletonReference != null) {
            if (exposedObject == bean) {
                exposedObject = earlySingletonReference;
            }
            else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
                String[] dependentBeans = getDependentBeans(beanName);
                Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);
                for (String dependentBean : dependentBeans) {
                    if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
                        actualDependentBeans.add(dependentBean);
                    }
                }
                if (!actualDependentBeans.isEmpty()) {
                    throw new BeanCurrentlyInCreationException(beanName,
                                                               "Bean with name '" + beanName + "' has been injected into other beans [" +
                                                               StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
                                                               "] in its raw version as part of a circular reference, but has eventually been " +
                                                               "wrapped. This means that said other beans do not use the final version of the " +
                                                               "bean. This is often the result of over-eager type matching - consider using " +
                                                               "'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.");
                }
            }
        }
    }

    // Register bean as disposable.
    /**
     * 会将有销毁方法的beanName和bean保存到一个map集合中
     * 方便执行销毁方法
     */
    try {
        registerDisposableBeanIfNecessary(beanName, bean, mbd);
    }
    catch (BeanDefinitionValidationException ex) {
        throw new BeanCreationException(
            mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
    }

    return exposedObject;
}

3.5.3.1 实例化bean
/**
 * Create a new instance for the specified bean, using an appropriate instantiation strategy:
 * factory method, constructor autowiring, or simple instantiation.
 * @param beanName the name of the bean
 * @param mbd the bean definition for the bean
 * @param args explicit arguments to use for constructor or factory method invocation
 * @return a BeanWrapper for the new instance
 * @see #obtainFromSupplier
 * @see #instantiateUsingFactoryMethod
 * @see #autowireConstructor
 * @see #instantiateBean
 */
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
    // Make sure bean class is actually resolved at this point.
    //获取用于实例化的bean的clazz对象
    Class<?> beanClass = resolveBeanClass(mbd, beanName);

    /**
     * Modifier.isPublic(beanClass.getModifiers()) 判断当前类的访问修饰符是不是public
     * mbd.isNonPublicAccessAllowed() 是否允许spring使用非public的构造方法实例化对象,
     * 或访问普通方法
     */
    if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
        throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                        "Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
    }

    //Supplier是JDK中定义的一个结果的提供者,类似于spring中FactoryBean
    Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
    if (instanceSupplier != null) {
        return obtainFromSupplier(instanceSupplier, beanName);
    }

    //获取工厂方法的名字
    if (mbd.getFactoryMethodName() != null) {
        //使用工厂方法实例化对象
        return instantiateUsingFactoryMethod(beanName, mbd, args);
    }

    /***************************重新创建bean的快捷方式*******************************/
    // Shortcut when re-creating the same bean...
    /**
     * 对象只要被创建过一次,那么创建这个对象使用的构造方法和参数就会被缓存起来
     * 方便下次快速创建这个bean,对于prototype作用域的bean很有用,节省了时间
     */
    boolean resolved = false;
    boolean autowireNecessary = false;
    //指定构造方法参数就不能使用快速创建bean了
    if (args == null) {
        synchronized (mbd.constructorArgumentLock) {
            /**
             * 工厂方法或构造方法不为空
             * 说明当前bean使用的构造方法之前已经解析过了,缓存了
             * 现在直接使用缓存的构造方法和参数快速实例化对象
             */
            if (mbd.resolvedConstructorOrFactoryMethod != null) {
                resolved = true;
                autowireNecessary = mbd.constructorArgumentsResolved;
            }
        }
    }
    //构造方法已经被解析
    if (resolved) {
        //构造方法参数也解析过了
        if (autowireNecessary) {
            return autowireConstructor(beanName, mbd, null, null);
        }
        //构造方法参数未解析,使用默认的构造方法实例化对象
        else {
            return instantiateBean(beanName, mbd);
        }
    }

    // Candidate constructors for autowiring?
    /**
     * 获取用于实例化的候选构造函数,见(1)
     * SmartInstantiationAwareBeanPostProcessor通过这个增强器,可以人为的指定使用的构造方法
     * @Autowired注解标注在构造方法上,就可以使用该构造方法实例化对象
     * 该功能就是在此处实现的
     */
    Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
    /**
     * 只要满足下述四种情况之一,就可以进入构造器自动注入,实例化对象
     * 构造方法上标注了@Autowired注解
     * 指定自动注入的模式为AUTOWIRE_CONSTRUCTOR
     * 之前使用有参构造方法实例化过对象(prototype)
     * getBean方法上指定了构造器参数
     */ 
    if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
        mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
        //使用构造器自动注入实例化对象
        return autowireConstructor(beanName, mbd, ctors, args);
    }

    // Preferred constructors for default construction?
    /**
     * 获取首选的默认构造方法
     * 对于RootBeanDefinition,该方法返回null
     * 在实例化前,所有其他的非RootBeanDefinition都会转换成RootBeanDefinition
     * 所以此处直接忽略
     */
    ctors = mbd.getPreferredConstructors();
    if (ctors != null) {
        return autowireConstructor(beanName, mbd, ctors, null);
    }

    // No special handling: simply use no-arg constructor.
    //(2)使用默认的构造方法实例化对象
    return instantiateBean(beanName, mbd);
}

(1)determineConstructorsFromBeanPostProcessors(beanClass, beanName)方法(获取用于实例化的候选构造函数)
/**
 * Determine candidate constructors to use for the given bean, checking all registered
 * {@link SmartInstantiationAwareBeanPostProcessor SmartInstantiationAwareBeanPostProcessors}.
 * @param beanClass the raw class of the bean
 * @param beanName the name of the bean
 * @return the candidate constructors, or {@code null} if none specified
 * @throws org.springframework.beans.BeansException in case of errors
 * @see org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor#determineCandidateConstructors
 */
@Nullable
protected Constructor<?>[] determineConstructorsFromBeanPostProcessors(@Nullable Class<?> beanClass, String beanName)
    throws BeansException {

    //容器中有InstantiationAwareBeanPostProcessor,见3.5.2.1
    if (beanClass != null && hasInstantiationAwareBeanPostProcessors()) {
        //遍历所有的BeanPostProcessor
        for (BeanPostProcessor bp : getBeanPostProcessors()) {
            /**
             * 执行SmartInstantiationAwareBeanPostProcessor接口的
             * determineCandidateConstructors方法,来获取候选的构造方法
             */
            if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
                SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
                Constructor<?>[] ctors = ibp.determineCandidateConstructors(beanClass, beanName);
                if (ctors != null) {
                    return ctors;
                }
            }
        }
    }
    return null;
}

总结:

  • 通过SmartInstantiationAwareBeanPostProcessor这个增强器,可以人为的指定使用的构造方法
  • 处理@Autowired注解的类实现了SmartInstantiationAwareBeanPostProcessor这个接口,并且重写了determineCandidateConstructors方法的逻辑,找到有@Autowired注解的构造方法。所以@Autowired标注在构造方法上,就可以使用该构造方法实例化对象。
(2)使用默认的构造方法实例化对象
/**
 * Instantiate the given bean using its default constructor.
 * @param beanName the name of the bean
 * @param mbd the bean definition for the bean
 * @return a BeanWrapper for the new instance
 */
protected BeanWrapper instantiateBean(String beanName, RootBeanDefinition mbd) {
    try {
        Object beanInstance;
        //安全检查,忽略
        if (System.getSecurityManager() != null) {
            beanInstance = AccessController.doPrivileged(
                (PrivilegedAction<Object>) () -> getInstantiationStrategy().instantiate(mbd, beanName, this),
                getAccessControlContext());
        }
        /** 
         * 实例化对象
         * 系统提供的默认实例化策略:private InstantiationStrategy instantiationStrategy = 
         * new CglibSubclassingInstantiationStrategy();
         * 具体如何实例化,这里就不详细解释了
         */
        else {
            beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this);
        }
        //包装一下对象
        BeanWrapper bw = new BeanWrapperImpl(beanInstance);
        //初始化BeanWrapper,向里面放一些PropertyEditor对象
        initBeanWrapper(bw);
        return bw;
    }
    catch (Throwable ex) {
        throw new BeanCreationException(
            mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
    }
}

总结:

  • 系统默认使用CglibSubclassingInstantiationStrategy这个策略类来实例化bean
  • 实例化完成的bean会被包装为BeanWrapper,主要是为了可以向里面放一些PropertyEditor对象,方便后面进行类型转换。
3.5.3.2 spring为了解决循环依赖而做的工作

这里结合3.4.1一起看

/**
 * 判断是否需要提前将该bean实例暴露
 * isSingletonCurrentlyInCreation(beanName) 判断当前beanName对应的bean是否正在创建
 * 只会暴露当前正在创建的单例bean
 */
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
                                  isSingletonCurrentlyInCreation(beanName));
if (earlySingletonExposure) {
    if (logger.isTraceEnabled()) {
        logger.trace("Eagerly caching bean '" + beanName +
                     "' to allow for resolving potential circular references");
    }
    /**
     * (1)创建一个ObjectFactory,它的getObject方法返回的是经过getEarlyBeanReference方法
     * 增强的bean
     * (2)把这个ObjectFactory工厂放入第三级缓存中
     */
    addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}
(1)ObjectFactory调用SmartInstantiationAwareBeanPostProcessor接口的getEarlyBeanReference方法增强实例化后的bean
/**
 * Obtain a reference for early access to the specified bean,
 * typically for the purpose of resolving a circular reference.
 * @param beanName the name of the bean (for error handling purposes)
 * @param mbd the merged bean definition for the bean
 * @param bean the raw bean instance
 * @return the object to expose as bean reference
 */
protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) {
    Object exposedObject = bean;
    if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
        for (BeanPostProcessor bp : getBeanPostProcessors()) {
            if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
                SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
                //可以调用getEarlyBeanReference对实例化后的bean增强
                exposedObject = ibp.getEarlyBeanReference(exposedObject, beanName);
            }
        }
    }
    return exposedObject;
}
(2)addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory)方法(保存ObjectFactory到容器第三级缓存中)
/**
 * Add the given singleton factory for building the specified singleton
 * if necessary.
 * <p>To be called for eager registration of singletons, e.g. to be able to
 * resolve circular references.
 * @param beanName the name of the bean
 * @param singletonFactory the factory for the singleton object
 */
protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
    Assert.notNull(singletonFactory, "Singleton factory must not be null");
    synchronized (this.singletonObjects) {
        //单例对象池中没有
        if (!this.singletonObjects.containsKey(beanName)) {
            //保存ObjectFactory到第三级缓存中
            this.singletonFactories.put(beanName, singletonFactory);
            this.earlySingletonObjects.remove(beanName);
            //beanName对应的bean已经注册到容器中
            this.registeredSingletons.add(beanName);
        }
    }
}

总结:

  • 放入第三级缓存的是新创建的匿名ObjectFactory对象,这个对象中调用了SmartInstantiationAwareBeanPostProcessor接口的getEarlyBeanReference方法
  • 每次调用ObjectFactory对象的getObject方法,都会调用getEarlyBeanReference方法
  • getEarlyBeanReference方法可以对之前实例化的bean对象增强
  • 此时的bean才刚实例化完成,还没有经过属性填充和初始化
3.5.3.3 bean实例填充属性
/**
 * Populate the bean instance in the given BeanWrapper with the property values
 * from the bean definition.
 * @param beanName the name of the bean
 * @param mbd the bean definition for the bean
 * @param bw the BeanWrapper with bean instance
 */
@SuppressWarnings("deprecation")  // for postProcessPropertyValues
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
    //参数检查
    if (bw == null) {
        if (mbd.hasPropertyValues()) {
            throw new BeanCreationException(
                mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
        }
        else {
            // Skip property population phase for null instance.
            return;
        }
    }

    // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
    // state of the bean before properties are set. This can be used, for example,
    // to support styles of field injection.
    /**
     * 执行InstantiationAwareBeanPostProcessor接口的postProcessAfterInstantiation方法
     * 返回false表明跳过该bean的属性填充
     * 此处可以为对象设置属性(相当于对象的默认属性)
     */
    if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
        for (BeanPostProcessor bp : getBeanPostProcessors()) {
            if (bp instanceof InstantiationAwareBeanPostProcessor) {
                InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
                    return;
                }
            }
        }
    }

    //获取需要填充的属性值(XML配置文件property标签配置的属性)
    PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);

    /**
     * 获取自动注入的模式,这种方式的自动注入已经被注解方法取代了,spring不推荐使用
     * 有no,byName,byType,AUTOWIRE_CONSTRUCTOR,4种(可以在bean标签中指定autowire="no")
     * AUTOWIRE_CONSTRUCTOR在实例化bean的时候提到过
     */
    int resolvedAutowireMode = mbd.getResolvedAutowireMode();
    if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
        MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
        // Add property values based on autowire by name if applicable.
        if (resolvedAutowireMode == AUTOWIRE_BY_NAME) {
            autowireByName(beanName, mbd, bw, newPvs);
        }
        // Add property values based on autowire by type if applicable.
        if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
            autowireByType(beanName, mbd, bw, newPvs);
        }
        pvs = newPvs;
    }

    boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
    boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);

    PropertyDescriptor[] filteredPds = null;
    /**
     * 通过InstantiationAwareBeanPostProcessor的postProcessProperties方法
     * 和postProcessPropertyValues可以获取需要进行自动注入的属性
     * spring自动注入@Autowired注解和@Resources注解标注的属性,就是在postProcessProperties完成的
     * postProcessPropertyValues方法已经过时了,不推荐使用
     */
    if (hasInstAwareBpps) {
        if (pvs == null) {
            pvs = mbd.getPropertyValues();
        }
        for (BeanPostProcessor bp : getBeanPostProcessors()) {
            if (bp instanceof InstantiationAwareBeanPostProcessor) {
                InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                PropertyValues pvsToUse = ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);
                if (pvsToUse == null) {
                    if (filteredPds == null) {
                        filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
                    }
                    pvsToUse = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
                    if (pvsToUse == null) {
                        return;
                    }
                }
                pvs = pvsToUse;
            }
        }
    }
    if (needsDepCheck) {
        if (filteredPds == null) {
            filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
        }
        checkDependencies(beanName, mbd, filteredPds, pvs);
    }

    //填充属性
    if (pvs != null) {
        applyPropertyValues(beanName, mbd, bw, pvs);
    }
}
3.5.3.4 初始化bean
/**
 * Initialize the given bean instance, applying factory callbacks
 * as well as init methods and bean post processors.
 * <p>Called from {@link #createBean} for traditionally defined beans,
 * and from {@link #initializeBean} for existing bean instances.
 * @param beanName the bean name in the factory (for debugging purposes)
 * @param bean the new bean instance we may need to initialize
 * @param mbd the bean definition that the bean was created with
 * (can also be {@code null}, if given an existing bean instance)
 * @return the initialized bean instance (potentially wrapped)
 * @see BeanNameAware
 * @see BeanClassLoaderAware
 * @see BeanFactoryAware
 * @see #applyBeanPostProcessorsBeforeInitialization
 * @see #invokeInitMethods
 * @see #applyBeanPostProcessorsAfterInitialization
 */
protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
    //安全检查
    if (System.getSecurityManager() != null) {
        AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
            invokeAwareMethods(beanName, bean);
            return null;
        }, getAccessControlContext());
    }
    else {
        /**
         * (1)执行aware接口方法,注入容器内部的对象
         * 先后顺序为BeanNameAware,BeanClassLoaderAware,BeanFactoryAware
         */
        invokeAwareMethods(beanName, bean);
    }

    Object wrappedBean = bean;
    if (mbd == null || !mbd.isSynthetic()) {
        //执行所有BeanPostProcessor的postProcessBeforeInitialization方法
        wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
    }

    try {
        //(2)执行初始化方法
        invokeInitMethods(beanName, wrappedBean, mbd);
    }
    catch (Throwable ex) {
        throw new BeanCreationException(
            (mbd != null ? mbd.getResourceDescription() : null),
            beanName, "Invocation of init method failed", ex);
    }
    if (mbd == null || !mbd.isSynthetic()) {
        //执行所有BeanPostProcessor的postProcessAfterInitialization方法
        wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
    }

    return wrappedBean;
}

(1)执行aware接口方法,注入容器内部的对象
private void invokeAwareMethods(String beanName, Object bean) {
    if (bean instanceof Aware) {
        if (bean instanceof BeanNameAware) {
            ((BeanNameAware) bean).setBeanName(beanName);
        }
        if (bean instanceof BeanClassLoaderAware) {
            ClassLoader bcl = getBeanClassLoader();
            if (bcl != null) {
                ((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
            }
        }
        if (bean instanceof BeanFactoryAware) {
            ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
        }
    }
}

BeanNameAwareBeanClassLoaderAwareBeanFactoryAware这三个aware接口在其它aware接口之前执行

(2)执行初始化方法
/**
 * Give a bean a chance to react now all its properties are set,
 * and a chance to know about its owning bean factory (this object).
 * This means checking whether the bean implements InitializingBean or defines
 * a custom init method, and invoking the necessary callback(s) if it does.
 * @param beanName the bean name in the factory (for debugging purposes)
 * @param bean the new bean instance we may need to initialize
 * @param mbd the merged bean definition that the bean was created with
 * (can also be {@code null}, if given an existing bean instance)
 * @throws Throwable if thrown by init methods or by the invocation process
 * @see #invokeCustomInitMethod
 */
protected void invokeInitMethods(String beanName, Object bean, @Nullable RootBeanDefinition mbd)
    throws Throwable {

    //先执行InitializingBean接口的afterPropertiesSet的方法
    boolean isInitializingBean = (bean instanceof InitializingBean);
    if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
        if (logger.isTraceEnabled()) {
            logger.trace("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
        }
        if (System.getSecurityManager() != null) {
            try {
                AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
                    ((InitializingBean) bean).afterPropertiesSet();
                    return null;
                }, getAccessControlContext());
            }
            catch (PrivilegedActionException pae) {
                throw pae.getException();
            }
        }
        else {
            ((InitializingBean) bean).afterPropertiesSet();
        }
    }

    //然后执行xml配置(init-method="XXX")或@Bean注解指定初始化方法
    if (mbd != null && bean.getClass() != NullBean.class) {
        String initMethodName = mbd.getInitMethodName();
        if (StringUtils.hasLength(initMethodName) &&
            !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
            !mbd.isExternallyManagedInitMethod(initMethodName)) {
            invokeCustomInitMethod(beanName, bean, mbd);
        }
    }
}

spring的三种初始化的方式

(1)xmlinit-method="xxx"@Bean注解中指定的初始化方法

(2)注解@PostConstruct,该注解由BeanPostProcessorpostProcessBeforeInitialization方法处理

(3)实现InitializingBean接口

4 bean生命周期流程图

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值