Spring IoC源码解析之getBean

一、实例化所有的非懒加载的单实例Bean

  从org.springframework.context.support.AbstractApplicationContext#refresh方法开发,进入到实例化所有的非懒加载的单实例Bean的finishBeanFactoryInitialization(beanFactory)的方法:

protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
        // 为Bean工厂设置类型转化器
        if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
                beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
            beanFactory.setConversionService(
                    beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
        }

        // Register a default embedded value resolver if no bean post-processor
        // (such as a PropertyPlaceholderConfigurer bean) registered any before:
        // at this point, primarily for resolution in annotation attribute values.
        if (!beanFactory.hasEmbeddedValueResolver()) {
            beanFactory.addEmbeddedValueResolver(new StringValueResolver() {
                @Override
                public String resolveStringValue(String strVal) {
                    return getEnvironment().resolvePlaceholders(strVal);
                }
            });
        }

        // Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
        String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
        for (String weaverAwareName : weaverAwareNames) {
            getBean(weaverAwareName);
        }

        // Stop using the temporary ClassLoader for type matching.
        beanFactory.setTempClassLoader(null);

        //冻结所有的Bean定义 , 至此注册的Bean定义将不被修改或任何进一步的处理
        beanFactory.freezeConfiguration();

        //实例化剩下的单实例Bean
        beanFactory.preInstantiateSingletons();
    }

  进入实例化剩下的单实例Bean的beanFactory.preInstantiateSingletons()的方法:

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

        //获取我们容器中所有Bean定义的名称
        List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);

        //循环我们所有的bean定义名称
        for (String beanName : beanNames) {
            //合并我们的bean定义
            RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
            //根据bean定义判断是不是抽象的 && 不是单例的 && 不是懒加载的
            if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
                //是否FactoryBean
                if (isFactoryBean(beanName)) {
                    //是 给beanName+前缀 & 符号
                    Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
                    if (bean instanceof FactoryBean) {
                        final FactoryBean<?> factory = (FactoryBean<?>) bean;
                        boolean isEagerInit;
                        if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
                            isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
                                            ((SmartFactoryBean<?>) factory)::isEagerInit,
                                    getAccessControlContext());
                        }
                        else {
                            isEagerInit = (factory instanceof SmartFactoryBean &&
                                    ((SmartFactoryBean<?>) factory).isEagerInit());
                        }
                        //调用真正的getBean
                        if (isEagerInit) {
                            getBean(beanName);
                        }
                    }
                }
                else {//非工厂Bean就是普通的bean
                    getBean(beanName);
                }
            }
        }

        //获取所有的bean的名称 至此所有的单实例的bean已经加入到单实例Bean的缓存池中,所谓的单实例缓存池实际上就是一个ConcurrentHashMap
        for (String beanName : beanNames) {
            //从单例缓存池中获取所有的对象
            Object singletonInstance = getSingleton(beanName);
            //判断当前的bean是否实现了SmartInitializingSingleton接口
            if (singletonInstance instanceof SmartInitializingSingleton) {
                final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
                if (System.getSecurityManager() != null) {
                    AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
                        smartSingleton.afterSingletonsInstantiated();
                        return null;
                    }, getAccessControlContext());
                }
                else {
                    //触发实例化之后的方法afterSingletonsInstantiated
                    smartSingleton.afterSingletonsInstantiated();
                }
            }
        }
    }

  前面多个地方涉及到getBean,接下来就分析下getBean(很重要

二、getBean流程

  进入getBean(beanName)的方法:

@Override
    public Object getBean(String name) throws BeansException {
        //真正的获取Bean的逻辑
        return doGetBean(name, null, null, false);
    }

  该方法啥没干,又交给了干活的doGetBean(name, null, null, false)方法:

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

        //在这里传入进来的name可能是别名、也有可能是工厂beanName,所以在这里需要转换
        final String beanName = transformedBeanName(name);
        Object bean;

        //先尝试去缓存中获取对象
        Object sharedInstance = getSingleton(beanName);

        if (sharedInstance != null && args == null) {
            if (logger.isDebugEnabled()) {
                if (isSingletonCurrentlyInCreation(beanName)) {
                    logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
                            "' that is not fully initialized yet - a consequence of a circular reference");
                }
                else {
                    logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
                }
            }
            /**
             * 如果sharedInstance是普通的单例bean,下面的方法会直接返回。但如果
             * sharedInstance是FactoryBean类型的,则需调用getObject工厂方法获取真正的
             * bean实例。如果用户想获取 FactoryBean 本身,这里也不会做特别的处理,直接返回
             * 即可。毕竟 FactoryBean 的实现类本身也是一种 bean,只不过具有一点特殊的功能而已。
             */
            bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
        }

        else {

            //Spring只能解决单例对象的setter注入的循环依赖,不能解决构造器注入,也不能解决多实例的循环依赖
            if (isPrototypeCurrentlyInCreation(beanName)) {
                throw new BeanCurrentlyInCreationException(beanName);
            }

            //判断是否有父工厂
            BeanFactory parentBeanFactory = getParentBeanFactory();
            //若存在父工厂,切当前的bean工厂不存在当前的bean定义,那么bean定义是存在于父beanFactory中
            if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
                //获取bean的原始名称
                String nameToLookup = originalBeanName(name);
                //若为AbstractBeanFactory类型,委托父类处理
                if (parentBeanFactory instanceof AbstractBeanFactory) {
                    return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
                            nameToLookup, requiredType, args, typeCheckOnly);
                }
                else if (args != null) {
                    // 委托给构造函数getBean()处理
                    return (T) parentBeanFactory.getBean(nameToLookup, args);
                }
                else {
                    // 没有args,委托给标准的getBean()处理
                    return parentBeanFactory.getBean(nameToLookup, requiredType);
                }
            }

            /**
             * 方法参数typeCheckOnly ,是用来判断调用getBean(...) 方法时,表示是否为仅仅进行类型检查获取Bean对象
             * 如果不是仅仅做类型检查,而是创建Bean对象,则需要调用markBeanAsCreated(String beanName) 方法,进行记录
             */
            if (!typeCheckOnly) {
                markBeanAsCreated(beanName);
            }

            try {
                //从容器中获取beanName相应的GenericBeanDefinition对象,并将其转换为RootBeanDefinition对象
                final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
                //检查当前创建的bean定义是不是抽象的bean定义
                checkMergedBeanDefinition(mbd, beanName, args);

                //处理dependsOn的依赖(这个不是我们所谓的循环依赖 而是bean创建前后的依赖)
                //依赖bean的名称
                String[] dependsOn = mbd.getDependsOn();
                if (dependsOn != null) {
                    for (String dep : dependsOn) {
                        //beanName是当前正在创建的bean,dep是正在创建的bean的依赖的bean的名称
                        if (isDependent(beanName, dep)) {
                            throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                    "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
                        }
                        //保存的是依赖beanName之间的映射关系:依赖beanName -> beanName的集合
                        registerDependentBean(dep, beanName);
                        try {
                            //获取dependsOn的bean
                            getBean(dep);
                        }
                        catch (NoSuchBeanDefinitionException ex) {
                            throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                    "'" + beanName + "' depends on missing bean '" + dep + "'", ex);
                        }
                    }
                }

                //是单例则创建单例Bean
                if (mbd.isSingleton()) {
                    //把beanName和一个singletonFactory匿名内部类传入用于回调
                    sharedInstance = getSingleton(beanName, () -> {
                        try {
                            //创建bean的逻辑
                            return createBean(beanName, mbd, args);
                        }
                        catch (BeansException ex) {
                            //创建bean的过程中发生异常,需要销毁关于当前bean的所有信息
                            destroySingleton(beanName);
                            throw ex;
                        }
                    });
                    bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
                }

                else if (mbd.isPrototype()) {
                    // It's a prototype -> create a new instance.
                    Object prototypeInstance = null;
                    try {
                        beforePrototypeCreation(beanName);
                        prototypeInstance = createBean(beanName, mbd, args);
                    }
                    finally {
                        afterPrototypeCreation(beanName);
                    }
                    bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
                }

                else {
                    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 {
                        Object scopedInstance = scope.get(beanName, () -> {
                            beforePrototypeCreation(beanName);
                            try {
                                return createBean(beanName, mbd, args);
                            }
                            finally {
                                afterPrototypeCreation(beanName);
                            }
                        });
                        bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
                    }
                    catch (IllegalStateException ex) {
                        throw new BeanCreationException(beanName,
                                "Scope '" + scopeName + "' is not active for the current thread; consider " +
                                "defining a scoped proxy for this bean if you intend to refer to it from a singleton",
                                ex);
                    }
                }
            }
            catch (BeansException ex) {
                cleanupAfterBeanCreationFailure(beanName);
                throw ex;
            }
        }

        // Check if required type matches the type of the actual bean instance.
        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.isDebugEnabled()) {
                    logger.debug("Failed to convert bean '" + name + "' to required type '" +
                            ClassUtils.getQualifiedName(requiredType) + "'", ex);
                }
                throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
            }
        }
        return (T) bean;
    }

  第一步:先尝试去缓存中获取对象

  Object sharedInstance = getSingleton(beanName),由于第一次肯定为空:

public Object getSingleton(String beanName) {
        //在这里系统一般是允许早期对象引用的allowEarlyReference通过这个参数可以控制解决循环依赖
        return getSingleton(beanName, true);
    }

  进入到getSingleton(beanName, true)方法,这里涉及到Spring的三级缓存,用它来解决循环依赖:

protected Object getSingleton(String beanName, boolean allowEarlyReference) {
        /**
         * 第一步:我们尝试去一级缓存(单例缓存池中去获取对象,一般情况从该map中获取的对象是直接可以使用的)
         * Spring IoC容器初始化加载单实例bean的时候第一次进来的时候 该map中一般返回空
         */
        Object singletonObject = this.singletonObjects.get(beanName);
        /**
         * 若在第一级缓存中没有获取到对象,并且singletonsCurrentlyInCreation正在创建的单实例的list包含该beanName
         * Spring IoC容器初始化加载单实例bean的时候第一次进来的时候 该list中一般返回空,但是循环依赖的时候可以满足该条件
         */
        if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
            synchronized (this.singletonObjects) {
                /**
                 * 尝试去二级缓存中获取对象(二级缓存中的对象是一个早期对象)
                 * 何为早期对象:就是bean刚刚调用了构造方法,还没给bean的属性进行赋值的对象就是早期对象
                 */
                singletonObject = this.earlySingletonObjects.get(beanName);
                /**
                 * 二级缓存中也没有获取到对象,allowEarlyReference为true(参数是有上一个方法传递进来的true)
                 */
                if (singletonObject == null && allowEarlyReference) {
                    /**
                     * 直接从三级缓存中获取ObjectFactory对象 这个对接就是用来解决循环依赖的关键所在
                     * 在getBean的过程中,当bean调用了构造方法的时候,把早期对象包裹成一个ObjectFactory暴露到三级缓存中
                     */
                    ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
                    //从三级缓存中获取到对象不为空
                    if (singletonFactory != null) {
                        /**
                         * 在这里通过暴露的ObjectFactory包装对象中,通过调用他的getObject()来获取我们的早期对象
                         * 在这个环节中会调用到 getEarlyBeanReference()来进行后置处理
                         */
                        singletonObject = singletonFactory.getObject();
                        //把早期对象放置在二级缓存,
                        this.earlySingletonObjects.put(beanName, singletonObject);
                        //ObjectFactory 包装对象从三级缓存中删除掉
                        this.singletonFactories.remove(beanName);
                    }
                }
            }
        }
        return singletonObject;
    }

  第二步:走else逻辑

  Spring只能解决单例对象的setter注入的循环依赖,不能解决构造器注入,也不能解决多实例的循环依赖,所以会抛Bean当前正在创建的异常,接着判断是否有父工厂,有就调用父工厂的getBean,如果不是仅仅做类型检查,而是创建Bean对象,则需要调用markBeanAsCreated方法进行标识

  合并Bean定义信息

  ① 接着调用getMergedLocalBeanDefinition(beanName)进行Bean定义的合并方法如下:

protected RootBeanDefinition getMergedLocalBeanDefinition(String beanName) throws BeansException {
        // 快速从缓存中获取,如果不为空,则直接返回
        RootBeanDefinition mbd = this.mergedBeanDefinitions.get(beanName);
        if (mbd != null) {
            return mbd;
        }
        //获取 RootBeanDefinition 对象。若获取的 BeanDefinition 为子 BeanDefinition,则需要合并父类的相关属性.
        return getMergedBeanDefinition(beanName, getBeanDefinition(beanName));
    }

  ② 进入到getMergedBeanDefinition(beanName, getBeanDefinition(beanName))方法:

protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd)
            throws BeanDefinitionStoreException {
        //调用重载的方法
        return getMergedBeanDefinition(beanName, bd, null);
    }

  ③ 进入到getMergedBeanDefinition(beanName, bd, null)的方法:

protected RootBeanDefinition getMergedBeanDefinition(
            String beanName, BeanDefinition bd, @Nullable BeanDefinition containingBd)
            throws BeanDefinitionStoreException {

        //加锁
        synchronized (this.mergedBeanDefinitions) {
            RootBeanDefinition mbd = null;

            // Check with full lock now in order to enforce the same merged instance.
            if (containingBd == null) {
                mbd = this.mergedBeanDefinitions.get(beanName);
            }

            if (mbd == null) {
                //bd.getParentName() == null,表明无父配置,这时直接将当前的BeanDefinition升级为RootBeanDefinition
                if (bd.getParentName() == null) {
                    //直接把原始的bean定义升级为RootBeanDefinition
                    if (bd instanceof RootBeanDefinition) {
                        mbd = ((RootBeanDefinition) bd).cloneBeanDefinition();
                    }
                    else {
                        //包裹为RootBeanDefinition
                        mbd = new RootBeanDefinition(bd);
                    }
                }
                //有父定义
                else {
                    BeanDefinition pbd;
                    try {
                        /*
                         * 判断父类beanName与子类beanName名称是否相同。若相同,则父类bean一定
                         * 在父容器中。原因也很简单,容器底层是用Map缓存<beanName, bean> 键值对
                         * 的。同一个容器下,使用同一个 beanName 映射两个bean实例显然是不合适的
                         */
                        String parentBeanName = transformedBeanName(bd.getParentName());
                        if (!beanName.equals(parentBeanName)) {
                            pbd = getMergedBeanDefinition(parentBeanName);
                        }
                        else {
                            /*
                             * 这里再次调用getMergedBeanDefinition,只不过参数值变为了
                             * parentBeanName,用于合并父BeanDefinition 和爷爷辈的
                             * BeanDefinition。如果爷爷辈的BeanDefinition仍有父
                             * BeanDefinition,则继续合并
                             */
                            BeanFactory parent = getParentBeanFactory();
                            if (parent instanceof ConfigurableBeanFactory) {
                                pbd = ((ConfigurableBeanFactory) parent).getMergedBeanDefinition(parentBeanName);
                            }
                            else {
                                throw new NoSuchBeanDefinitionException(parentBeanName,
                                        "Parent name '" + parentBeanName + "' is equal to bean name '" + beanName +
                                        "': cannot be resolved without an AbstractBeanFactory parent");
                            }
                        }
                    }
                    catch (NoSuchBeanDefinitionException ex) {
                        throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName,
                                "Could not resolve parent bean definition '" + bd.getParentName() + "'", ex);
                    }
                    //以父BeanDefinition的配置信息为基本创建RootBeanDefinition
                    mbd = new RootBeanDefinition(pbd);
                    //用子BeanDefinition中的属性覆盖父BeanDefinition中的属性
                    mbd.overrideFrom(bd);
                }

                // 如果用户未配置scope属性,则默认将该属性配置为singleton
                if (!StringUtils.hasLength(mbd.getScope())) {
                    mbd.setScope(RootBeanDefinition.SCOPE_SINGLETON);
                }

                // A bean contained in a non-singleton bean cannot be a singleton itself.
                // Let's correct this on the fly here, since this might be the result of
                // parent-child merging for the outer bean, in which case the original inner bean
                // definition will not have inherited the merged outer bean's singleton status.
                if (containingBd != null && !containingBd.isSingleton() && mbd.isSingleton()) {
                    mbd.setScope(containingBd.getScope());
                }

                //缓存合并后的BeanDefinition
                if (containingBd == null && isCacheBeanMetadata()) {
                    this.mergedBeanDefinitions.put(beanName, mbd);
                }
            }

            return mbd;
        }
    }

  ④ 然后检查当前创建的bean定义是不是抽象的bean定义,checkMergedBeanDefinition(mbd, beanName, args)方法:

protected void checkMergedBeanDefinition(RootBeanDefinition mbd, String beanName, @Nullable Object[] args)
            throws BeanDefinitionStoreException {

        //抽象的bean定义是不能够被实例化的
        if (mbd.isAbstract()) {
            throw new BeanIsAbstractException(beanName);
        }
    }

  处理dependsOn的依赖(这个不是我们所谓的循环依赖 而是bean创建前后的依赖)

Spring IoC源码解析之getBean

  ① 进入到isDependent(beanName, dependentBeanName, null)的方法:

private boolean isDependent(String beanName, String dependentBeanName, @Nullable Set<String> alreadySeen) {
        //alreadySeen已经检测的依赖bean
        if (alreadySeen != null && alreadySeen.contains(beanName)) {
            return false;
        }
        //获取原始beanName
        String canonicalName = canonicalName(beanName);
        //获取创建当前bean所依赖的bean的名称集合
        Set<String> dependentBeans = this.dependentBeanMap.get(canonicalName);
        //不依赖任何前置Bean直接返回
        if (dependentBeans == null) {
            return false;
        }
        //存在,则证明存在已经注册的依赖
        if (dependentBeans.contains(dependentBeanName)) {
            return true;
        }
        //递归检测依赖
        for (String transitiveDependency : dependentBeans) {
            if (alreadySeen == null) {
                alreadySeen = new HashSet<>();
            }
            //添加到alreadySeen 中
            alreadySeen.add(beanName);
            //递归检查依赖
            if (isDependent(transitiveDependency, dependentBeanName, alreadySeen)) {
                return true;
            }
        }
        return false;
    }

  ② 进入到保存的是依赖beanName之间的映射关系:依赖beanName -> beanName的集合registerDependentBean(dep, beanName)的方法:

public void registerDependentBean(String beanName, String dependentBeanName) {
        //获取原始的beanName
        String canonicalName = canonicalName(beanName);

        // 添加 <canonicalName, dependentBeanName> 到 dependentBeanMap 中
        synchronized (this.dependentBeanMap) {
            Set<String> dependentBeans =
                    this.dependentBeanMap.computeIfAbsent(canonicalName, k -> new LinkedHashSet<>(8));
            if (!dependentBeans.add(dependentBeanName)) {
                return;
            }
        }

        // 添加 <dependentBeanName, canonicalName> 到 dependenciesForBeanMap 中
        synchronized (this.dependenciesForBeanMap) {
            Set<String> dependenciesForBean =
                    this.dependenciesForBeanMap.computeIfAbsent(dependentBeanName, k -> new LinkedHashSet<>(8));
            dependenciesForBean.add(canonicalName);
        }
    }

  ③ 然后到获取dependsOn的Bean的getBean(dep),就获取到了dependsOn的Bean了;

  处理单实例Bean

  ① 把beanName和一个singletonFactory匿名内部类传入用于回调的getSingleton方法:

public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
        Assert.notNull(beanName, "Bean name must not be null");
        //加锁
        synchronized (this.singletonObjects) {
            //尝试从单例缓存池中获取对象
            Object singletonObject = this.singletonObjects.get(beanName);
            if (singletonObject == null) {
                if (this.singletonsCurrentlyInDestruction) {
                    throw new BeanCreationNotAllowedException(beanName,
                            "Singleton bean creation not allowed while 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 + "'");
                }
                /**
                 * 标记当前的bean马上就要被创建了
                 * singletonsCurrentlyInCreation 在这里会把beanName加入进来,若第二次循环依赖(构造器注入会抛出异常)
                 */
                beforeSingletonCreation(beanName);
                boolean newSingleton = false;
                boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
                if (recordSuppressedExceptions) {
                    this.suppressedExceptions = new LinkedHashSet<>();
                }
                try {
                    //创建bean这个过程其实是调用 createBean() 方法
                    singletonObject = singletonFactory.getObject();
                    newSingleton = true;
                }
                catch (IllegalStateException ex) {
                    //回调我们singletonObjects的get方法,进行正在的创建bean的逻辑
                    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;
                    }
                    //后置处理主要做的事情就是把singletonsCurrentlyInCreation标记正在创建的bean从集合中移除
                    afterSingletonCreation(beanName);
                }
                if (newSingleton) {
                    //加入缓存中
                    addSingleton(beanName, singletonObject);
                }
            }
            return singletonObject;
        }
    }

   第一:先到单例缓存池中获取对象获取,有就返回没有就继续往下

   第二:调用标记当前的bean马上就要被创建了beforeSingletonCreation(beanName)方法:

protected void beforeSingletonCreation(String beanName) {
        //若singletonsCurrentlyInCreation没有 则添加成功
        if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.add(beanName)) {
            throw new BeanCurrentlyInCreationException(beanName);
        }
    

  第三:创建Bean的singletonFactory.getObject()其实是调用createBean()方法:

protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) throws BeanCreationException {
        if (logger.isDebugEnabled()) {
            logger.debug("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.
        Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
        if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
            mbdToUse = new RootBeanDefinition(mbd);
            mbdToUse.setBeanClass(resolvedClass);
        }

        // Prepare method overrides.
        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.
            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);
        }

        Object beanInstance = doCreateBean(beanName, mbdToUse, args);
        if (logger.isDebugEnabled()) {
            logger.debug("Finished creating instance of bean '" + beanName + "'");
        }
        return beanInstance;
    }

  真正的创建我们的bean的实例对象的doCreateBean(beanName, mbdToUse, args)的方法:

protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
            throws BeanCreationException {

        //BeanWrapper是对Bean的包装,其接口中所定义的功能很简单包括设置获取被包装的对象,获取被包装bean的属性描述器
        BeanWrapper instanceWrapper = null;
        if (mbd.isSingleton()) {
            //从没有完成的FactoryBean中移除
            instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
        }
        if (instanceWrapper == null) {
            //使用合适的实例化策略来创建新的实例:工厂方法、构造函数自动注入、简单初始化 比较复杂也很重要
            instanceWrapper = createBeanInstance(beanName, mbd, args);
        }
        //从beanWrapper中获取我们的早期对象
        final Object bean = instanceWrapper.getWrappedInstance();
        Class<?> beanType = instanceWrapper.getWrappedClass();
        if (beanType != NullBean.class) {
            mbd.resolvedTargetType = beanType;
        }

        // Allow post-processors to modify the merged bean definition.
        synchronized (mbd.postProcessingLock) {
            if (!mbd.postProcessed) {
                try {
                    //进行后置处理@AutoWired的注解的预解析
                    applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
                }
                catch (Throwable ex) {
                    throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                            "Post-processing of merged bean definition failed", ex);
                }
                mbd.postProcessed = true;
            }
        }

        /**
         * 该对象进行判断是否能够暴露早期对象的条件
         * 单实例 this.allowCircularReferences 默认为true
         * isSingletonCurrentlyInCreation(表示当前的bean对象正在创建singletonsCurrentlyInCreation包含当前正在创建的bean)
         */
        boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
                isSingletonCurrentlyInCreation(beanName));
        //上述条件满足,允许中期暴露对象
        if (earlySingletonExposure) {
            if (logger.isDebugEnabled()) {
                logger.debug("Eagerly caching bean '" + beanName +
                        "' to allow for resolving potential circular references");
            }
            //把我们的早期对象包装成一个singletonFactory对象 该对象提供了一个getObject方法,该方法内部调用getEarlyBeanReference方法
            addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
        }

        // Initialize the bean instance.
        Object exposedObject = bean;
        try {
            //给我们的属性进行赋值(调用set方法进行赋值)
            populateBean(beanName, mbd, instanceWrapper);
            //进行对象初始化操作(在这里可能生成代理对象)
            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);
            }
        }

        //允许早期对象的引用
        if (earlySingletonExposure) {
            /**
             * 去缓存中获取到我们的对象 由于传递的allowEarlyReference 是false 要求只能在一级二级缓存中去获取
             * 正常普通的bean(不存在循环依赖的bean) 创建的过程中,压根不会把三级缓存提升到二级缓存中
             */
            Object earlySingletonReference = getSingleton(beanName, false);
            //能够获取到
            if (earlySingletonReference != null) {
                //经过后置处理的bean和早期的bean引用还相等的话(表示当前的bean没有被代理过)
                if (exposedObject == bean) {
                    exposedObject = earlySingletonReference;
                }
                //处理依赖的bean
                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 " +
                                "'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
                    }
                }
            }
        }

        // Register bean as disposable.
        try {
            //注册销毁的bean的销毁接口
            registerDisposableBeanIfNecessary(beanName, bean, mbd);
        }
        catch (BeanDefinitionValidationException ex) {
            throw new BeanCreationException(
                    mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
        }

        return exposedObject;
    }

  ① 实例化:使用合适的实例化策略来创建新的实例:工厂方法、构造函数自动注入、简单初始化的createBeanInstance方法:

protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
        //从bean定义中解析出当前bean的class对象
        Class<?> beanClass = resolveBeanClass(mbd, beanName);

        //检测类的访问权限。默认情况下,对于非 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<?> instanceSupplier = mbd.getInstanceSupplier();
        if (instanceSupplier != null) {
            return obtainFromSupplier(instanceSupplier, beanName);
        }

        //工厂方法,我们通过配置类来进行配置的话 采用的就是工厂方法
        if (mbd.getFactoryMethodName() != null) {
            return instantiateUsingFactoryMethod(beanName, mbd, args);
        }

        //判断当前构造函数是否被解析过
        boolean resolved = false;
        //有没有必须进行依赖注入
        boolean autowireNecessary = false;
        /**
         * 通过getBean传入进来的构造函数是否来指定需要推断构造函数
         * 若传递进来的args不为空,那么就可以直接选出对应的构造函数
         */
        if (args == null) {
            synchronized (mbd.constructorArgumentLock) {
                //判断我们的bean定义信息中的resolvedConstructorOrFactoryMethod(用来缓存我们的已经解析的构造函数或者工厂方法)
                if (mbd.resolvedConstructorOrFactoryMethod != null) {
                    //修改已经解析过的构造函数的标志
                    resolved = true;
                    //修改标记为ture 标识构造函数或者工厂方法已经解析过
                    autowireNecessary = mbd.constructorArgumentsResolved;
                }
            }
        }
        //若被解析过
        if (resolved) {
            if (autowireNecessary) {
                //通过有参的构造函数进行反射调用
                return autowireConstructor(beanName, mbd, null, null);
            }
            else {
                //调用无参数的构造函数进行创建对象
                return instantiateBean(beanName, mbd);
            }
        }

        //通过bean的后置处理器进行选举出合适的构造函数对象
        Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
        //通过后置处理器解析出构造器对象不为null或获取bean定义中的注入模式是构造器注入或bean定义信息ConstructorArgumentValues或获取通过getBean的方式传入的构造器函数参数类型不为null
        if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
                mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
            //通过构造函数创建对象
            return autowireConstructor(beanName, mbd, ctors, args);
        }
        //使用无参数的构造函数调用创建对象
        return instantiateBean(beanName, mbd);
    }

  判断是否暴露早期对象条件满足就暴露早期对象,把我们的早期对象包装成一个singletonFactory对象 该对象提供了一个getObject方法,该方法内部调用getEarlyBeanReference方法:

protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) {
        Object exposedObject = bean;
        //判读我们容器中是否有InstantiationAwareBeanPostProcessors类型的后置处理器
        if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
            //获取我们所有的后置处理器
            for (BeanPostProcessor bp : getBeanPostProcessors()) {
                //判断我们的后置处理器是不是实现了SmartInstantiationAwareBeanPostProcessor接口
                if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
                    //进行强制转换
                    SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
                    //挨个调用SmartInstantiationAwareBeanPostProcessor的getEarlyBeanReference
                    exposedObject = ibp.getEarlyBeanReference(exposedObject, beanName);
                }
            }
        }
        return exposedObject;
    }

  addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean))方法:

/**
     * 该方法用于把早期对象包装成一个ObjectFactory 暴露到三级缓存中 用于将解决循环依赖
     * @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) {
            //单例缓存池中没有包含当前的bean
            if (!this.singletonObjects.containsKey(beanName)) {
                //加入到三级缓存中 暴露早期对象用于解决循环依赖
                this.singletonFactories.put(beanName, singletonFactory);
                this.earlySingletonObjects.remove(beanName);
                this.registeredSingletons.add(beanName);
            }
        }
    }

  ②属性赋值:给我们的属性进行赋值(调用set方法进行赋值)populateBean(beanName, mbd, instanceWrapper)方法:

/**
     *给我们的对象BeanWrapper属性赋值
     * @param beanName bean的名称
     * @param mbd bean的定义
     * @param bw bean实例包装对象
     */
    protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
        //若bw为null的话,则说明对象没有实例化
        if (bw == null) {
            //进入if说明对象有属性,bw为空,不能为他设置属性,那就在下面就执行抛出异常
            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;
            }
        }

        /**
         * 在属性被填充前,给 InstantiationAwareBeanPostProcessor 类型的后置处理器一个修改
         * bean 状态的机会。官方的解释是:让用户可以自定义属性注入。比如用户实现一
         * 个 InstantiationAwareBeanPostProcessor 类型的后置处理器,并通过
         * postProcessAfterInstantiation 方法向 bean 的成员变量注入自定义的信息。
         *当时我们发现系统中的的InstantiationAwareBeanPostProcessor.postProcessAfterInstantiationM没有进行任何处理,
         *若我们自己实现了这个接口 可以自定义处理.....spring 留给我们自己扩展接口的
         *特殊需求,直接使用配置中的信息注入即可。
         */
        boolean continueWithPropertyPopulation = true;
        //是否持有 InstantiationAwareBeanPostProcessor
        if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
            //获取容器中的所有的BeanPostProcessor
            for (BeanPostProcessor bp : getBeanPostProcessors()) {
                //判断我们的后置处理器是不是InstantiationAwareBeanPostProcessor
                if (bp instanceof InstantiationAwareBeanPostProcessor) {
                    //进行强制转化
                    InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                    //若存在后置处理器给我们属性赋值了,那么返回false 可以来修改我们的开关变量,就不会走下面的逻辑了
                    if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
                        // 返回值为是否继续填充 bean
                        // postProcessAfterInstantiation:如果应该在 bean上面设置属性则返回 true,否则返回 false
                        // 一般情况下,应该是返回true 。
                        // 返回 false 的话,将会阻止在此 Bean 实例上调用任何后续的 InstantiationAwareBeanPostProcessor 实
                        continueWithPropertyPopulation = false;
                        break;
                    }
                }
            }
        }
        // 如果后续处理器发出停止填充命令,则终止后续操作
        if (!continueWithPropertyPopulation) {
            return;
        }

        //获取bean定义的属性
        PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);

        /**
         * 判断我们的bean的属性注入模型
         * AUTOWIRE_BY_NAME 根据名称注入
         * AUTOWIRE_BY_TYPE 根据类型注入
         */

        if (mbd.getResolvedAutowireMode() == AUTOWIRE_BY_NAME || mbd.getResolvedAutowireMode() == AUTOWIRE_BY_TYPE) {
            //把PropertyValues封装成为MutablePropertyValues
            MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
            //根据bean的属性名称注入
            if (mbd.getResolvedAutowireMode() == AUTOWIRE_BY_NAME) {
                autowireByName(beanName, mbd, bw, newPvs);
            }
            //根据bean的类型进行注入
            if (mbd.getResolvedAutowireMode() == AUTOWIRE_BY_TYPE) {
                autowireByType(beanName, mbd, bw, newPvs);
            }
            //把处理过的 属性覆盖原来的
            pvs = newPvs;
        }

        /**
         * 这里又是一种后置处理,用于在 Spring 填充属性到 bean 对象前,对属性的值进行相应的处理,
         * 比如可以修改某些属性的值。这时注入到 bean 中的值就不是配置文件中的内容了,
         * 而是经过后置处理器修改后的内容
         */
        boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
        //判断是否需要检查依赖
        boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);

        if (hasInstAwareBpps || needsDepCheck) {
            if (pvs == null) {
                pvs = mbd.getPropertyValues();
            }
            //提出当前正在创建的beanWrapper 依赖的对象
            PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
            if (hasInstAwareBpps) {
                //获取所有的后置处理器
                for (BeanPostProcessor bp : getBeanPostProcessors()) {
                    if (bp instanceof InstantiationAwareBeanPostProcessor) {
                        InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                        //对依赖对象进行后置处理
                        pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
                        if (pvs == null) {
                            return;
                        }
                    }
                }
            }
            //判断是否检查依赖
            if (needsDepCheck) {
                checkDependencies(beanName, mbd, filteredPds, pvs);
            }
        }

        /**
         * 其实,上面只是完成了所有注入属性的获取,将获取的属性封装在 PropertyValues 的实例对象 pvs 中,
         * 并没有应用到已经实例化的 bean 中。而 #applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) 方法,
         * 则是完成这一步骤的
         */
        if (pvs != null) {
            applyPropertyValues(beanName, mbd, bw, pvs);
        }
    }

   ③初始化:进行对象初始化操作调用initializeBean,这里会进行Aware接口进行方法的回调,然后调用Bean的后置处理器的Before方法(postProcessorsBeforeInitialization),然后Bean的初始化方法,最后调用Bean的后置处理器的After方法(PostProcessorsAfterInitialization)

protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
        if (System.getSecurityManager() != null) {
            AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
                invokeAwareMethods(beanName, bean);
                return null;
            }, getAccessControlContext());
        }
        else {
            //若我们的Bean实现了Aware接口进行方法的回调
            invokeAwareMethods(beanName, bean);
        }

        Object wrappedBean = bean;
        if (mbd == null || !mbd.isSynthetic()) {
            //调用我们的bean的后置处理器的postProcessorsBeforeInitialization方法
            wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
        }

        try {
            //调用初始化方法
            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()) {
            //调用我们bean的后置处理器的PostProcessorsAfterInitialization方法 动态代理就是在这里实现的
            wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
        }

        return wrappedBean;
    }

  进入调用初始化方法invokeInitMethods(beanName, wrappedBean, mbd)方法:

protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd)
            throws Throwable {

        //判断我们的容器中是否实现了InitializingBean接口
        boolean isInitializingBean = (bean instanceof InitializingBean);
        if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
            if (logger.isDebugEnabled()) {
                logger.debug("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的afterPropertiesSet()方法
                ((InitializingBean) bean).afterPropertiesSet();
            }
        }

        if (mbd != null && bean.getClass() != NullBean.class) {
            //我们beanClass中看是否有自己定义的init方法
            String initMethodName = mbd.getInitMethodName();
            //判断自定义的init方法名称不叫afterPropertiesSet
            if (StringUtils.hasLength(initMethodName) &&
                    !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
                    !mbd.isExternallyManagedInitMethod(initMethodName)) {
                //调用我们自己的初始化方法
                invokeCustomInitMethod(beanName, bean, mbd);
            }
        }
    }

  至此doCreateBean创建完成然后返回

Spring IoC源码解析之getBean

  接下来bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd),为什么不直接等于sharedInstance,原因可能刚刚创建的Bean是FactoryBean类型的Bean,如果是就要调用getObject方法来获取真正的Bean,运用场景就是那些创建Bean的逻辑比较复杂的情况下可以用这个,比如Spring整合Mybatis的SqlSessionFactoryBean

Spring IoC源码解析之getBean

  getObjectForBeanInstance(sharedInstance, name, beanName, mbd)方法:

protected Object getObjectForBeanInstance(
            Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) {

        // 如果name以&开头,但beanInstance却不是FactoryBean,则认为有问题。
        if (BeanFactoryUtils.isFactoryDereference(name)) {
            if (beanInstance instanceof NullBean) {
                return beanInstance;
            }
            if (!(beanInstance instanceof FactoryBean)) {
                throw new BeanIsNotAFactoryException(beanName, beanInstance.getClass());
            }
        }

          /**
             * 如果上面的判断通过了,表明 beanInstance 可能是一个普通的 bean,也可能是一个
             * FactoryBean。如果是一个普通的 bean,这里直接返回 beanInstance 即可。如果是
             * FactoryBean,则要调用工厂方法生成一个 bean 实例。
           */
        if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) {
            return beanInstance;
        }

        Object object = null;
        if (mbd == null) {
            /**
             * 如果 mbd 为空,则从缓存中加载 bean。FactoryBean 生成的单例 bean 会被缓存
             * 在 factoryBeanObjectCache 集合中,不用每次都创建
             */
            object = getCachedObjectForFactoryBean(beanName);
        }
        if (object == null) {
            // 经过前面的判断,到这里可以保证beanInstance是 FactoryBean类型的,所以可以进行类型转换
            FactoryBean<?> factory = (FactoryBean<?>) beanInstance;
            // 如果 mbd 为空,则判断是否存在名字为 beanName 的 BeanDefinition
            if (mbd == null && containsBeanDefinition(beanName)) {
                mbd = getMergedLocalBeanDefinition(beanName);
            }
            //synthetic 字面意思是"合成的"。通过全局查找,我发现在 AOP 相关的类中会将该属性设为 true。
            //所以我觉得该字段可能表示某个 bean 是不是被 AOP 增强过,也就是 AOP 基于原始类合成了一个新的代理类。
            //不过目前只是猜测,没有深究
            boolean synthetic = (mbd != null && mbd.isSynthetic());
            //调用 getObjectFromFactoryBean 方法继续获取实例
            object = getObjectFromFactoryBean(factory, beanName, !synthetic);
        }
        return object;
    }

  接着调用object = getObjectFromFactoryBean(factory, beanName, !synthetic)方法:

protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) {
           /**
             * FactoryBean 也有单例和非单例之分,针对不同类型的 FactoryBean,这里有两种处理方式:
             *   1. 单例 FactoryBean 生成的 bean 实例也认为是单例类型。需放入缓存中,供后续重复使用
             *   2. 非单例 FactoryBean 生成的 bean 实例则不会被放入缓存中,每次都会创建新的实例
             **/
        if (factory.isSingleton() && containsSingleton(beanName)) {
            synchronized (getSingletonMutex()) {
                //从缓存中取bean实例,避免多次创建bean实例
                Object object = this.factoryBeanObjectCache.get(beanName);
                if (object == null) {
                    //使用工厂对象中创建实例
                    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 {
                        if (shouldPostProcess) {
                            //判断当地的bean是否正在创建
                            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);
                            }
                        }
                        // 这里的beanName对应于FactoryBean的实现类,FactoryBean的实现类也会被实例化,并被缓存在singletonObjects中
                        if (containsSingleton(beanName)) {
                            // 这里的beanName对应于FactoryBean的实现类,FactoryBean的实现类也会被实例化,并被缓存在singletonObjects中
                            this.factoryBeanObjectCache.put(beanName, object);
                        }
                    }
                }
                return object;
            }
        }
        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;
        }
    }

  使用工厂对象中创建实例object = doGetObjectFromFactoryBean(factory, beanName)方法:

private Object doGetObjectFromFactoryBean(final FactoryBean<?> factory, final 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 {
                //真正的调用工厂bean的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.
        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;
    }

  处理多实例Bean

  mbd.isPrototype(),每次使用的时候再创建,因为不会加入到单实例缓冲池中,也就无法解决循环依赖问题。

  至此doGetBean创建完成,返回,getBean创建完成

Spring IoC源码解析之getBean

  最后获取所有的bean的名称(至此所有的单实例的bean已经加入到单实例Bean的缓存池中,所谓的单实例缓存池实际上就是一个ConcurrentHashMap),遍历所有的bean名称,根据beanName从单例缓存池中获取所有的对象,然后判断是否是SmartInitializingSingleton类型,是再触发实例化之后的方法afterSingletonsInstantiated;

  完整的Spring IoC源码解析见:Spring系列(三):Spring IoC源码解析

三、getBean流程图

  ① getBean流程图:

Spring IoC源码解析之getBean

  ② @AutoWired注入属性和set方法注入流程图:

 Spring IoC源码解析之getBean

  总结:通过对Spring IoC的getBean流程分析,了解了Bean的创建过程,先到单实例缓存池(ConcurrentHashMap)中获取,取不到就调用createBean创建,创建成功后加入到单实例缓存池(ConcurrentHashMap)中,下次获取的时候直接从JVM级别的缓存中获取,Bean的生命周期,以及如何用三级缓存解决循环依赖,还有就是多实例为什么不能解决循环依赖问题。

上一篇:SOLIDWORKS帮助企业开启MBE大门


下一篇:Spring IoC bean 的加载