(一)Spring IoC源码-2.bean的加载-01整体概览

本文已收录于【Spring源码札记专栏】

关联文章:
(一)Spring IoC源码-2.bean的加载-01整体概览
(一)Spring IoC源码-2.bean的加载-02从缓存中获取单例bean
(一)Spring IoC源码-2.bean的加载-03从bean实例中获取对象
(一)Spring IoC源码-2.bean的加载-04创建bean

BeanFactory bf = new XmlBeanFactory(new ClassPathResource("MyBean.xml"));
MyBean myBean = (MyBean)bf.getBean("myBean");

前面几篇文章都是在学习BeanFactory bf = new XmlBeanFactory(new ClassPathResource("MyBean.xml"))的实现原理,即IoC容器是如何初始化的。本文要学习的是MyBean myBean = (MyBean)bf.getBean("myBean");的实现原理,一般称其为bean的加载

跟踪getBean(String)方法,找到AbstractBeanFactory.getBean(String)方法的源码。

@Override
public Object getBean(String name) throws BeansException {
    return doGetBean(name, null, null, false);
}

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

    //获取bean的真正名字(name参数可能是bean的别名)
    final String beanName = transformedBeanName(name);
    Object bean;

    //在创建单例bean时会进行依赖注入,为了避免在依赖注入时发生循环依赖
    //spring不等bean创建完成就将bean的ObjectFactory加入缓存中。
    //如果下个bean创建时需要依赖此bean,则直接使用ObjectFactory。

    // 尝试从缓存或者sigletonFactories中的ObjectFactory中加载单例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并不一定是我们最终想要的bean。举个例子,假如我们需要对工厂bean,即FactoryBean实例进行的处理,
        //那么sharedInstance其实是工厂bean的初始状态,但我们真正想要的是工厂bean中定义的factory-method方法中返回的bean,
        //而getObjectForBeanInstance方法就是完成这个工作的。
        bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
    }

    else {
        //原型模式的依赖检查
        //prototype的Bean,Spring容器不进行缓存。
        //如果是prototype的Bean处于正在创建的状态,直接抛出异常
        if (isPrototypeCurrentlyInCreation(beanName)) {
            throw new BeanCurrentlyInCreationException(beanName);
        }

        //因为在缓存中没有加载到单例bean,所以检查ParentBeanFactory中是否包含了bean
        BeanFactory parentBeanFactory = getParentBeanFactory();     
        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) {
                // Delegation to parent with explicit args.
                return (T) parentBeanFactory.getBean(nameToLookup, args);
            }
            else {
                // No args -> delegate to standard getBean method.
                return parentBeanFactory.getBean(nameToLookup, requiredType);
            }
        }

        if (!typeCheckOnly) {
            markBeanAsCreated(beanName);
        }

        try {
            final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
            checkMergedBeanDefinition(mbd, beanName, args);

            // 如果此bean依赖了其他的bean,则需要递归加载依赖的bean
            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 + "'");
                    }
                    registerDependentBean(dep, beanName);
                    getBean(dep);
                }
            }

            // 创建bean
            if (mbd.isSingleton()) {
                //创建单例bean
                sharedInstance = getSingleton(beanName, () -> {
                    try {
                        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;
                    }
                });
                //处理sharedInstance,获取真正想要的bean。
                bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
            }

            else if (mbd.isPrototype()) {//如果是原型模式
                // 原型模式的创建
                Object prototypeInstance = null;
                try {
                    beforePrototypeCreation(beanName);
                    prototypeInstance = createBean(beanName, mbd, args);
                }
                finally {
                    afterPrototypeCreation(beanName);
                }
                //处理sharedInstance,获取真正想要的bean。
                bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
            }

            else {//如果既不是单例模式也不是原型模式,则根据具体的scope创建bean
                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);
                        }
                    });
                    //处理sharedInstance,获取真正想要的bean。
                    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;
        }
    }

    // 如果requireType参数不为null,根据需要进行类型转换
    if (requiredType != null && bean != null && !requiredType.isInstance(bean)) {
        try {
            return getTypeConverter().convertIfNecessary(bean, requiredType);
        }
        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());
        }
    }
    // For the nullability warning, see the elaboration in the comment above;
    // in short: This is never going to be null unless user-declared code enforces null.
    return (T) bean;
}

将bean的加载步骤总结如下:

  1. 获取bean的真正名字(name可能是别名或者FactoryBean)。
  2. 尝试从缓存中加载单例bean。
  3. 如果从缓存中加载到了单例bean,从bean实例中获取对象并返回。从缓存中加载到的bean并不一定是我们最终想要的bean。举个例子,从缓存中加载到bean可能是工厂bean的初始状态,但我们真正想要的是工厂bean中定义的factory-method方法中返回的bean,所以需要处理从缓存中加载到bean。
  4. 原型模式的依赖检查。prototype的Bean,Spring容器不进行缓存。如果是prototype的Bean处于正在创建的状态,直接抛出异常。
  5. 递归加载依赖的bean。如果此bean依赖了其他的bean,则需要递归加载依赖的bean。
  6. 因为在缓存中没有加载到单例bean。
    1. 递归检查ParentBeanFactory中是否包含了想要的bean。
    2. 如果在ParentBeanFactory中也没有找到想要的bean,则创建bean。
      1. 如果是单例模式,创建单例bean。
      2. 如果是原型模式,创建原型bean。
      3. 如果既不是单例模式也不是原型模式,则根据scope创建bean。
    3. 从bean实例中获取对象
  7. 类型装换。如果requireType参数不为null,根据需要进行类型转换。

以上步骤会在下面的几篇文章中进行详细的介绍。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值