Spring IOC源码解析三:Bean实例化过程源码分析

作者: 蓝梅 | 来源:发表于2021-06-27 14:39 被阅读0次

    一、bean的实例化过程源码分析

    上一章节讲了bean定义的加载过程,现在我们来讲一下bean的实例化过程,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));
        }
        if (!beanFactory.hasEmbeddedValueResolver()) {
            beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
        }
        //实例化AOP相关的组件
        String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
        for (String weaverAwareName : weaverAwareNames) {
            //调用工厂实例化方法,这个方法我们后面来分析
            getBean(weaverAwareName);
        }
        beanFactory.setTempClassLoader(null);
        //冻结所有bean定义,在实例化时,不允许再修改bean定义
        beanFactory.freezeConfiguration();
        //实例化所有bean
        beanFactory.preInstantiateSingletons();
    }
    //实例化所有bean
    public void preInstantiateSingletons() throws BeansException {
        if (logger.isTraceEnabled()) {
            logger.trace("Pre-instantiating singletons in " + this);
        }
        //获取到容器中所有bean定义
        List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
        //循环去实例化
        for (String beanName : beanNames) {
            //把各种不同类型的bean定义转换成RootBeanDefinition类型
            RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
            //只有不是抽象类,必须是单例类,并且不是懒加载才会去实例化
            if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
                //判断是不是工厂类
                if (isFactoryBean(beanName)) {
                    //工厂类会先生存实际的bean,用 &+bean名称
                    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 {
                            isEagerInit = (factory instanceof SmartFactoryBean &&
                                    ((SmartFactoryBean<?>) factory).isEagerInit());
                        }
                        //实际去实例化bean
                        if (isEagerInit) {
                            getBean(beanName);
                        }
                    }
                }
                else {
                    //调用实例化bean
                    getBean(beanName);
                }
            }
        }
        //遍历所有的bean,如果实现了SmartInitializingSingleton接口,调用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();
                }
            }
        }
    }
    public Object getBean(String name) throws BeansException {
        //真实去实例化bean
        return doGetBean(name, null, null, false);
    }
    //这个方法就是最重要的实例化过程,最主要是关注spring是怎么解决循环依赖的
    protected <T> T doGetBean(
            String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
            throws BeansException {
        String beanName = transformedBeanName(name);
        Object bean;
        //先从一级缓存中去获取bean,可以看下getSingleton方法
        Object sharedInstance = getSingleton(beanName);
        if (sharedInstance != null && args == null) {
            if (logger.isTraceEnabled()) {
                if (isSingletonCurrentlyInCreation(beanName)) {
                    logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
                            "' that is not fully initialized yet - a consequence of a circular reference");
                }
                else {
                    logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
                }
            }
            //如果是普通单例bean则会直接返回,如果是工厂bean。则会调用getObject获取真正的bean实例
            bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
        }
        else {
            //Spring只解决单例bean的循环依赖,如果非单例,循环依赖则会直接报错
            if (isPrototypeCurrentlyInCreation(beanName)) {
                throw new BeanCurrentlyInCreationException(beanName);
            }
            BeanFactory parentBeanFactory = getParentBeanFactory();
            //获取到父工厂,如果当前工厂中没有当前bean,则委托给父工厂实例化
            if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
                String nameToLookup = originalBeanName(name);
                if (parentBeanFactory instanceof AbstractBeanFactory) {
                    return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
                            nameToLookup, requiredType, args, typeCheckOnly);
                }
                else if (args != null) {
                    return (T) parentBeanFactory.getBean(nameToLookup, args);
                }
                else if (requiredType != null) {
                    return parentBeanFactory.getBean(nameToLookup, requiredType);
                }
                else {
                    return (T) parentBeanFactory.getBean(nameToLookup);
                }
            }
            //如果不仅仅做类型检查,而是创建bean,则标记这个bean
            if (!typeCheckOnly) {
                markBeanAsCreated(beanName);
            }
    
            try {
                //把不同类型的bean定义转换成RootBeanDefinition
                RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
                //如果是抽象类,则会报错
                checkMergedBeanDefinition(mbd, beanName, args);
                //处理dependsOn依赖
                String[] dependsOn = mbd.getDependsOn();
                if (dependsOn != null) {
                    for (String dep : dependsOn) {
                        //如果依赖的bean是正在创建的bean,就是循环依赖问题,这个直接抛异常,这种是@Bean依赖,不给解决循环依赖
                        if (isDependent(beanName, dep)) {
                            throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                    "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
                        }
                        registerDependentBean(dep, beanName);
                        try {
                            getBean(dep);
                        }
                        catch (NoSuchBeanDefinitionException ex) {
                            throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                    "'" + beanName + "' depends on missing bean '" + dep + "'", ex);
                        }
                    }
                }
                //如果是单例bean放进三级缓存,并且获取依赖注入的bean
                if (mbd.isSingleton()) {
                    sharedInstance = getSingleton(beanName, () -> {
                        try {
                            //这段逻辑可以看下面createBean的详解
                            return createBean(beanName, mbd, args);
                        }
                        catch (BeansException ex) {
                            //创建bean的逻辑报异常,则销毁当前bean相关的所有信息
                            destroySingleton(beanName);
                            throw ex;
                        }
                    });
                    bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
                }
    
                else if (mbd.isPrototype()) {
                    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();
                    if (!StringUtils.hasLength(scopeName)) {
                        throw new IllegalStateException("No scope name defined for bean ´" + beanName + "'");
                    }
                    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;
            }
        }
        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;
    }
    
    protected Object getSingleton(String beanName, boolean allowEarlyReference) {
        //第一步先从缓存中获取bean
        Object singletonObject = this.singletonObjects.get(beanName);
        //如果没有获取到bean,并且这个bean正在实例化过程中
        if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
            //从二级缓存中获取bean
            singletonObject = this.earlySingletonObjects.get(beanName);
            //如果未获取到
            if (singletonObject == null && allowEarlyReference) {
                //先对一级缓存加锁,保证获取到的是一个实例化完成的bean
                synchronized (this.singletonObjects) {
                    //因为获取到锁,可能这个bean已经放入到一级缓存,如果不去获取一次,可能出现多个bean
                    singletonObject = this.singletonObjects.get(beanName);
                    if (singletonObject == null) {
                        //然后再去从二级缓存中获取
                        singletonObject = this.earlySingletonObjects.get(beanName);
                        if (singletonObject == null) {
                            //然后从三级缓存中获取生成bean的方法,这是解决循环依赖的关键,生成代理类,也是这个方法调用后才生成的
                            ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
                            if (singletonFactory != null) {
                                //调用这个钩子方法,我们在上一个方法doGetBean中可以找到这个钩子方法
                                singletonObject = singletonFactory.getObject();
                                //然后把这个类放到二级缓存中
                                this.earlySingletonObjects.put(beanName, singletonObject);
                                //再从三级缓存中移除这个bean
                                this.singletonFactories.remove(beanName);
                            }
                        }
                    }
                }
            }
        }
    }
    //获取单例bean
    public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
        Assert.notNull(beanName, "Bean name must not be null");
        //加锁,如果同时有多线程在获取bean,保证不会获取到不完成的bean
        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正在被创建,用于判断是否是循环依赖,就是加入到set集合中
                beforeSingletonCreation(beanName);
                boolean newSingleton = false;
                boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
                if (recordSuppressedExceptions) {
                    this.suppressedExceptions = new LinkedHashSet<>();
                }
                try {
                    //调用外面传入进来的createBean钩子方法
                    singletonObject = singletonFactory.getObject();
                    newSingleton = true;
                }
                catch (IllegalStateException ex) {
                    singletonObject = this.singletonObjects.get(beanName);
                    if (singletonObject == null) {
                        throw ex;
                    }
                }
                catch (BeanCreationException ex) {
                    if (recordSuppressedExceptions) {
                        for (Exception suppressedException : this.suppressedExceptions) {
                            ex.addRelatedCause(suppressedException);
                        }
                    }
                    throw ex;
                }
                finally {
                    if (recordSuppressedExceptions) {
                        this.suppressedExceptions = null;
                    }
                    afterSingletonCreation(beanName);
                }
                if (newSingleton) {
                    addSingleton(beanName, singletonObject);
                }
            }
            //返回实例化后的bean
            return singletonObject;
        }
    }
    //实例化bean
    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;
        if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
            mbdToUse = new RootBeanDefinition(mbd);
            mbdToUse.setBeanClass(resolvedClass);
        }
        try {
            mbdToUse.prepareMethodOverrides();
        }
        catch (BeanDefinitionValidationException ex) {
            throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
                    beanName, "Validation of method overrides failed", ex);
        }
        try {
            //调用第一个bean的后置处理器,生成代理对象,我们一般的AOP是不会在这里生成代理的,因为还没实例化出对象
            //这里这里是AOP和事务的关键,会生成切面信息进行缓存
            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);
        }
        try {
            //真正去创建bean,进行实例化
            Object beanInstance = doCreateBean(beanName, mbdToUse, args);
            if (logger.isTraceEnabled()) {
                logger.trace("Finished creating instance of bean '" + beanName + "'");
            }
            return beanInstance;
        }
        catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
            throw ex;
        }
        catch (Throwable ex) {
            throw new BeanCreationException(
                    mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
        }
    }
    
    protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
            throws BeanCreationException {
        BeanWrapper instanceWrapper = null;
        if (mbd.isSingleton()) {
            instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
        }
        if (instanceWrapper == null) {
            instanceWrapper = createBeanInstance(beanName, mbd, args);
        }
        //获取早期对象
        Object bean = instanceWrapper.getWrappedInstance();
        Class<?> beanType = instanceWrapper.getWrappedClass();
        if (beanType != NullBean.class) {
            mbd.resolvedTargetType = beanType;
        }
        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);
                }
                mbd.postProcessed = true;
            }
        }
        /**
         * 这一段就是循环依赖最关键的点,会先放一个早期对象到钩子方法(getEarlyBeanReference)中
         * 这个钩子方法如果这个对象不用创建动态代理,则会直接返回当前对象
         * 把这个钩子方法放入三级缓存,等真正来获取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");
            }
            addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
        }
    
        Object exposedObject = bean;
        try {
            //去注入属性,比如有依赖的属性,则是从这里去注入,会循环调用getBean
            populateBean(beanName, mbd, instanceWrapper);
            /**
             * 到这一步,对象已经实例化完成
             * 如果有经过循环依赖的话,需要进行动态代理的类,已经生成了动态代理
             * 但是,未经过循环依赖的类,需要我们实例化完成后,进行动态代理
             * 动态代理时,还需判断是否是接口,需要用JDK的动态代理,还是CGLIB的动态代理
             **/
            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) {
            //如果是正常bean,一般只会从一级或者二级缓存中获取,不会把bean存入三级缓存
            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.");
                    }
                }
            }
        }
        try {
            //注册销毁的bean的销毁接口
            registerDisposableBeanIfNecessary(beanName, bean, mbd);
        }
        catch (BeanDefinitionValidationException ex) {
            throw new BeanCreationException(
                    mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
        }
        return exposedObject;
    }
    

    Spring IOC容器bean的实例化,到这基本就完成了,这次所有源代码的分析把重要的地方,大致流程都讲了一遍,但是有很多细节没有讲到,比如说怎么创建动态代理,怎么具体怎么实例化,这些代码根据我的注释找到相应的地方,可以去自己看看源码,我基本上都标注了每一步的步骤,只是如果要深挖的话,层级太深,绕也会绕晕,最好是自己脑子里有个框架,然后再根据框架去找细节,这样就会事半功倍。

    二、最后总结

    这次Spring只分析了源代码,这次就分析到这里,其实我们分析源代码不仅仅只是知道它内部逻辑是怎么运行的,关键是需要学习这个框架的思想,以及各种设计模式的运用,这样才算学到了东西;其实Spring的代码,你过了一段时间,基本上就只会记得大概,主要是确实层级太深,所以我们需要了解到精髓,例如里面生成bean用到的工厂模式,去解析@ComponentScan,里面的这个type用到的策略模式,还有AOP切面调用advisor使用的责任链模式,等等,都是我们学习的点;

    相关文章

      网友评论

        本文标题:Spring IOC源码解析三:Bean实例化过程源码分析

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