《Spring源码深度解析》学习笔记——bean的加载(一)

bean的加载

对于加载bean的功能,在Spring中的调用方式为:

MyTestBean bean = (MyTestBean)bf.getBean("myTestBean")

这行代码在Spring中是这样实现的

public  T getBean(String name, Class requiredType, Object... args) throws BeansException {
    return this.doGetBean(name, requiredType, args, false);
}

protected  T doGetBean(String name, Class requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException {
    //提取对应的beanName
    final String beanName = this.transformedBeanName(name);
    /*
     *检查缓存中或者实例工厂中是否有对应的实例
     *为什么首先会使用这段代码呢,因为在创建单例bean的时候会存在依赖注入的情况,而在创建依赖的时候为了避免循环依赖,
     *Spring创建bean的原则是不等bean创建完成就会将创建bean的ObjectFactory提早曝光,
     *也就是将ObjectFactory加入到缓存中,一旦下个bean创建时候需要依赖上个bean则直接使用ObjectFactory
     */
    //直接尝试从缓存获取或者singletonFactories中的ObjectFactory中获取
    Object sharedInstance = this.getSingleton(beanName);
    Object bean;
    if(sharedInstance != null && args == null) {
        if(this.logger.isDebugEnabled()) {
            if(this.isSingletonCurrentlyInCreation(beanName)) {
                this.logger.debug("Returning eagerly cached instance of singleton bean \'" + beanName + "\' that is not fully initialized yet - a consequence of a circular reference");
            } else {
                this.logger.debug("Returning cached instance of singleton bean \'" + beanName + "\'");
            }
        }

        //返回对应的实例,有时候存在诸如BeanFactory的情况并不是直接返回实例本身而是返回指定方法返回的实例
        bean = this.getObjectForBeanInstance(sharedInstance, name, beanName, (RootBeanDefinition)null);
    } else {
        //只有在单例情况下才会尝试解决循环依赖,原型模式情况下,如果存在
        //A中有B的属性,B中有A的属性,那么当依赖注入的时候,就会产生当A还未创建完的时候因为
        //对于B的创建再次返回创建A,造成循环依赖,也就是下面的情况
        //isPrototypeCurrentlyInCreation(beanName)为true
        if(this.isPrototypeCurrentlyInCreation(beanName)) {
            throw new BeanCurrentlyInCreationException(beanName);
        }

        BeanFactory parentBeanFactory = this.getParentBeanFactory();
        //如果BeanDefinitionMap中也就是在所有已经加载的类中不包括beanName则尝试从parentBeanFactory中检测
        if(parentBeanFactory != null && !this.containsBeanDefinition(beanName)) {
            String var19 = this.originalBeanName(name);
            //递归到BeanFactory中寻找
            if(args != null) {
                return parentBeanFactory.getBean(var19, args);
            }

            return parentBeanFactory.getBean(var19, requiredType);
        }

        //如果不是仅仅做类型检查则是创建bean,这里要进行记录
        if(!typeCheckOnly) {
            this.markBeanAsCreated(beanName);
        }

        //将存储XML配置文件的GernericBeanDefinition转换为RootBeanDefinition,如果指定BeanName是子Bean的话同时会合并父类的相关属性
        final RootBeanDefinition mbd = this.getMergedLocalBeanDefinition(beanName);
        this.checkMergedBeanDefinition(mbd, beanName, args);
        String[] dependsOn = mbd.getDependsOn();
        String scopeName;
        //若存在依赖则需要递归实例化依赖的bean
        if(dependsOn != null) {
            String[] var14 = dependsOn;
            int ex = dependsOn.length;

            for(int scope = 0; scope < ex; ++scope) {
                scopeName = var14[scope];
                this.getBean(scopeName);
                //缓存依赖调用
                 this.registerDependentBean(scopeName, beanName);
            }
        }

        //实例化依赖的bean后便可以实例化mbd本身了
        //singleton模式的创建
        if(mbd.isSingleton()) {
            sharedInstance = this.getSingleton(beanName, new ObjectFactory() {
                public Object getObject() throws BeansException {
                    try {
                        return AbstractBeanFactory.this.createBean(beanName, mbd, args);
                    } catch (BeansException var2) {
                        AbstractBeanFactory.this.destroySingleton(beanName);
                        throw var2;
                    }
                }
            });
            bean = this.getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
        } else if(mbd.isPrototype()) {
            //prototype模式的创建(new)
            scopeName = null;

            Object var20;
            try {
                this.beforePrototypeCreation(beanName);
                var20 = this.createBean(beanName, mbd, args);
            } finally {
                this.afterPrototypeCreation(beanName);
            }

            bean = this.getObjectForBeanInstance(var20, name, beanName, mbd);
        } else {
            //指定的scope上实例化bean
            scopeName = mbd.getScope();
            Scope var21 = (Scope)this.scopes.get(scopeName);
            if(var21 == null) {
                throw new IllegalStateException("No Scope registered for scope \'" + scopeName + "\'");
            }

            try {
                Object var22 = var21.get(beanName, new ObjectFactory() {
                    public Object getObject() throws BeansException {
                        AbstractBeanFactory.this.beforePrototypeCreation(beanName);

                        Object var2;
                        try {
                            var2 = AbstractBeanFactory.this.createBean(beanName, mbd, args);
                        } finally {
                            AbstractBeanFactory.this.afterPrototypeCreation(beanName);
                        }

                        return var2;
                    }
                });
                bean = this.getObjectForBeanInstance(var22, name, beanName, mbd);
            } catch (IllegalStateException var18) {
                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", var18);
            }
        }
    }

    //检查需要的类型是否符合bean的实际类型
    if(requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass())) {
        throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
    } else {
        return bean;
    }
}

对于Spring加载bean的过程,大致分为以下几步:

  • 转换对应beanName

    这里传入的参数name不一定就是beanName,有可能是别名或FactoryBean,所以需要进行一系列的解析,这些解析内容包括如下内容

    • 去除FactoryBean的修饰符,也就是如果name=”&aa”,那么会首先去除&而使name=”aa”

    • 取指定alias所表示的最终beanName,例如别名A指向名称为B的bean则返回B;若别名A指向别名B,别名B又指向名称为C的bean则返回C

  • 尝试从缓存中加载单例

    单例在Spring的同一个容器内只会被创建一次,后续再获取bean,就直接从单例缓存中获取了。这里只是尝试加载,首先尝试从缓存中加载,如果加载不成功则再次尝试从singletonFactories中加载,因为在创建单例bean的时候会存在依赖注入的情况,而在创建依赖的时候为了避免循环依赖,在Spring中创建bean的原则是不等bean创建完成就会将创建bean的ObjectFactory提早曝光加入到缓存中,一旦下一个bean创建时候需要依赖上一个bean则直接使用ObjectFactory

  • bean的实例化

    如果从缓存中得到了bean的原始状态,则需要对bean进行实例化,这里有必要强调一下,在缓存中记录的只是最原始的bean状态,并不一定是我们最终想要的bean

  • 原型模式的依赖检查

    只有在单例情况下才会尝试解决循环依赖,如果存在A中有B的属性,B中有A的属性,那么当依赖注入的时候,就会产生当A还未创建完的时候因为对于B的创建再次返回创建A,造成循环依赖,也就是情况:isPrototypeCurrentlyInCreation(beanName)判断true

  • 检测parentBeanFactory

    从代码上来看,如果缓存没有数据的话直接转到父类工厂上去加载,!this.containsBeanDefinition(beanName检测如果当前加载的XML配置文件中不包含beanName所对应的配置,就只能到parentBeanFactory去尝试,然后再去递归的调用getBean方法

  • 将存储XML配置文件的GernericBeanDefinition转换为RootBeanDefinition

    因为从XML配置文件中读取到的Bean信息是存储在GernericBeanDefinition中的,但是所有的Bean后续处理都是针对于RootBeanDefinition的,所以这里需要进行一个转换,转换的同时如果父类bean不为空的话,则会一并合并父类属性

  • 寻找依赖

    因为bean的初始化过程很可能会用到某些属性,而某些属性很可能是动态配置的,并且配置成依赖于其他的bean,那么这个时候就有必要先加载依赖的bean,所以,在Spring的加载顺寻中,在初始化某一个bean的时候首先会初始化这个bean所对应的依赖

  • 针对不同的scope进行bean的创建

    在Spring中存在着不同的scope,其中默认的是singleton,但是还有些其他的配置诸如prototype、request之类的,在这个步骤中,Spring会根据不同的配置进行不同的初始化策略

  • 类型转换

    程序到这里返回bean后已经基本结束了,通常对该方法的调用参数requiredType是为空的,但是可能会存在这样的情况,返回的bean其实是个Spring,但是requiredType却传入Integer类型,那么这时候本步骤就会起作用了,它的功能是将返回的bean转换为requiredType所指定的类型,当然,Spring转换为Integer是最简单的一种转换,在Spring中提供了各种各样的转换器,用户也可以自己扩展转换器来满足需求

FactoryBean的使用

一般情况下, Spring通过反射机制利用bean的class属性指定实现类来实例化bean,在某些情况下,实例化bean过程比较复杂,如果按照传统的方式,则需要在中提供大量的配置信息,配置方式的灵活性是受限的,这时采用编码的方式可能会得到一个简单的方案

Spring为此提供了一个org.Springframework.bean.factory.FactoryBean的工厂类接口,用户可以通过实现该接口定制实例化bean的逻辑

FactoryBean接口对于Spring框架来说占有重要的地位,Spring自身就提供了70多个FactoryBean的实现,它们隐藏了实例化一些复杂bean的细节,给上层应用带来了便利,从Spring 3.0开始,FactoryBean开始支持泛型,即接口声明改为FactoryBean的形式:

package org.springframework.beans.factory;

public interface FactoryBean {
    T getObject() throws Exception;

    Class getObjectType();

    boolean isSingleton();
}

在该接口中还定义了以下3个方法:

  • T getObject():返回由FactoryBean创建的bean实例,如果isSingleton()返回true,则该实例会放到Spring容器中单实例缓存池中
  • boolean isSingleton():返回由FactoryBean创建的bean实例的作用域是singleton还是prototype
  • Class getObjectType():返回FactoryBean创建的bean类型

当配置文件中的class属性配置的实现类是FactoryBean时,通过getBean()方法返回的不是FactoryBean本身,而是FactoryBean#getObject()方法所返回的对象,相当于FactoryBean#getObject()代理了getBean()方法。例如:如果使用传统方式配置下面Car的时,Car的每个属性分别对应一个元素标签

public class Car{
    private int maxSpeed;
    private String brand;
    private double price;
    //get/set方法
}

如果用FactoryBean的方式实现就会灵活一些,下例通过逗号分隔符的方式一次性地为Car的所有属性指定配置值

public class CarFactoryBean implements FactoryBean{
    private String carInfo;
    public Car getObject() throws Exceptino {
        Car car = new Car();
        String[] infos = carInfo.split(",");
        car.setBrand(infos[0]);
        car.setMaxSpeed(Integer.valueOf(infos[1]));
        car.setPrice(Double.valueOf(infos[2]));
        return car; 
    }

    public class getObjectType(){
        return Car.class;
    }

    public boolean isSingleton(){
        return false;
    }

    public String getCarInfo(){
        return this.carInfo;
    }

    //接受逗号分割符设置属性信息
    public void setCarInfo(String carInfo){
        this.carInfo = carInfo;
    }
}

有了这个CarFactoryBean后,就可以在配置文件中使用下面这种自定义的配置方式配置CarBean了


当调用getBean(“car”)时,Spring通过反射机制发现CarFactoryBean实现了FactoryBean的接口, 这时Spring容器就调用接口方法CarFactoryBean#getObject()方法返回,如果希望获取CarFactoryBean的实例,则需要在使用getBean(beanName)方法时在beanName前显示的加上”&”前缀,例如getBean(“&car”)

缓存中获取单例bean

 public Object getSingleton(String beanName) {
    //参数true设置标识允许早期依赖
    return this.getSingleton(beanName, true);
}

protected Object getSingleton(String beanName, boolean allowEarlyReference) {
    //检查缓存中是否存在实例
    Object singletonObject = this.singletonObjects.get(beanName);
    if(singletonObject == null) {
        //如果为空,则锁定全局变量并进行处理
        Map var4 = this.singletonObjects;
        synchronized(this.singletonObjects) {
            //如果此bean正在加载则不处理
            singletonObject = this.earlySingletonObjects.get(beanName);
            if(singletonObject == null && allowEarlyReference) {
                //当某些方法需要提前初始化的时候则会调用addSingletonFactory方法将对应的ObjectFactory初始化策略存储在singletonFactories
                ObjectFactory singletonFactory = (ObjectFactory)this.singletonFactories.get(beanName);
                if(singletonFactory != null) {
                    //调用预先设定的getObject方法
                    singletonObject = singletonFactory.getObject();
                    //记录在缓存中,earlySingletonObjects和singletonFactories互斥
                    this.earlySingletonObjects.put(beanName, singletonObject);
                    this.singletonFactories.remove(beanName);
                }
            }
        }
    }

    return singletonObject != NULL_OBJECT?singletonObject:null;
}

这个方法首先尝试从singletonObjects里面获取实例,如果获取不到再从earlySingletonObjects里面获取,如果还获取不到,再尝试从singletonFactories里面获取beanName对应的ObjectFactory,然后调用这个ObjectFactory的getObject来创建bean,并放到earlySingletonObjects里面去,并且从singletonFactories里面remove掉这个ObjectFactory,而对于后续的所有内存操作都只为了循环依赖检测时候使用,也就是在allowEarlyReference为true的情况下才会使用

这里涉及用于存在bean的不同map,说明如下:

  • singletonObjects:用于保存BeanName和创建bean实例之间的关系,bean name->bean instance
  • singletonFactories:用于保存BeanName和创建bean的工厂之间的关系,bean name->ObjectFactory
  • earlySingletonObjects:也是保存BeanName和创建bean实例之间的关系,与singletonObjects的不同之处在于,当一个单例bean被放到这里面后,那么当bean还在创建过程中,就可以通过getBean方法获取到了,其目的是用来检测循环引用
  • registeredSingletons:用来保存当前所有已注册的bean

从bean的实例中获取对象

在getBean方法中,getObjectForBeanInstance是个高频率使用的方法,无论是从缓存中获得bean还是根据不同的scope策略加载bean。总之,得到bean的实例后要做的第一步就是调用这个方法来检测一下正确性,其实就是用于检测当前bean是否是FactoryBean类型的bean,如果是,那么需要调用该bean对应的FactoryBean实例中的getObject()作为返回值

无论是从缓存中获取到的bean还是通过不同的scope策略加载的bean都只是最原始的bean状态,并不一定是最终想要的bean,如需要对工厂bean进行处理,那么这里得到的其实是工厂bean的初始状态, 但是我们真正需要的是工厂bean中定义的factory-method方法中返回的bean,而getObjectForBeanInstance方法就是完成这个工作的

protected Object getObjectForBeanInstance(Object beanInstance, String name, String beanName, RootBeanDefinition mbd) {
    //如果指定的name是工厂相关(以&为前缀)且beanInstance又不是FactoryBean类型则验证不通过
    if(BeanFactoryUtils.isFactoryDereference(name) && !(beanInstance instanceof FactoryBean)) {
        throw new BeanIsNotAFactoryException(this.transformedBeanName(name), beanInstance.getClass());
    } else if(beanInstance instanceof FactoryBean && !BeanFactoryUtils.isFactoryDereference(name)) {
        //加载FactoryBean
        Object object = null;
        if(mbd == null) {
            //尝试从缓存中加载bean
            object = this.getCachedObjectForFactoryBean(beanName);
        }

        //到这里已经明确知道beanInstance一定是FactoryBean类型
        if(object == null) {
            FactoryBean factory = (FactoryBean)beanInstance;
            //containsBeanDefinition检测BeanDefinitionMap中也就是在所有已经加载的类中检测是否定义beanName
            if(mbd == null && this.containsBeanDefinition(beanName)) {
                //将存储XML配置文件的GernericBeanDefinition转换为RootBeanDefinition,如果指定BeanName是子Bean的话同时会合并父类的相关属性
                mbd = this.getMergedLocalBeanDefinition(beanName);
            }

            //是否是用户定义的而不是应用程序本身定义的
            boolean synthetic = mbd != null && mbd.isSynthetic();
            object = this.getObjectFromFactoryBean(factory, beanName, !synthetic);
        }

        return object;
    } else {
        return beanInstance;
    }
}

getObjectForBeanInstance中所做的工作如下:

  • 对FactoryBean正确性的验证
  • 对非FactoryBean不做任何处理
  • 对bean进行转换
  • 将从Factory中解析bean的工作委托给getObjectFromFactoryBean

    protected Object getObjectFromFactoryBean(FactoryBean factory, String beanName, boolean shouldPostProcess) {
        if(factory.isSingleton() && this.containsSingleton(beanName)) {
            //如果是单例模式
            synchronized(this.getSingletonMutex()) {
                Object object = this.factoryBeanObjectCache.get(beanName);
                if(object == null) {
                    object = this.doGetObjectFromFactoryBean(factory, beanName, shouldPostProcess);
                    this.factoryBeanObjectCache.put(beanName, object != null?object:NULL_OBJECT);
                }
    
                return object != NULL_OBJECT?object:null;
            }
        } else {
            return this.doGetObjectFromFactoryBean(factory, beanName, shouldPostProcess);
        }
    }
    

这个方法中只做了一件事情,就是返回的bean如果是单例的,那就必须保证全局唯一,同时,也因为是单例的,所以不必重复创建,可以使用缓存来提高性能,也就是说已经加载过就要记录下来以便于下次复用, 否则的话就直接获取了

doGetObjectFromFactoryBean方法的代码如下:

private Object doGetObjectFromFactoryBean(final FactoryBean factory, String beanName, boolean shouldPostProcess) throws BeanCreationException {
    Object object;
    try {
        //需要权限验证
        if(System.getSecurityManager() != null) {
            AccessControlContext ex = this.getAccessControlContext();

            try {
                object = AccessController.doPrivileged(new PrivilegedExceptionAction() {
                    public Object run() throws Exception {
                        return factory.getObject();
                    }
                }, ex);
            } catch (PrivilegedActionException var8) {
                throw var8.getException();
            }
        } else {
            //直接调用getObject方法
            object = factory.getObject();
        }
    } catch (FactoryBeanNotInitializedException var9) {
        throw new BeanCurrentlyInCreationException(beanName, var9.toString());
    } catch (Throwable var10) {
        throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation", var10);
    }

    if(object == null && this.isSingletonCurrentlyInCreation(beanName)) {
        throw new BeanCurrentlyInCreationException(beanName, "FactoryBean which is currently in creation returned null from getObject");
    } else {
        if(object != null && shouldPostProcess) {
            try {
                //调用ObjectFactory的后处理器
                object = this.postProcessObjectFromFactoryBean(object, beanName);
            } catch (Throwable var7) {
                throw new BeanCreationException(beanName, "Post-processing of the FactoryBean\'s object failed", var7);
            }
        }

        return object;
    }
}

doGetObjectFromFactoryBean实现了从FactoryBean中对应的getObject方法得到bean,但是得到后并没有立即返回,而是做了些后处理的操作

protected Object postProcessObjectFromFactoryBean(Object object, String beanName) {
    return this.applyBeanPostProcessorsAfterInitialization(object, beanName);
}

public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException {
    Object result = existingBean;
    Iterator var5 = this.getBeanPostProcessors().iterator();

    while(var5.hasNext()) {
        BeanPostProcessor beanProcessor = (BeanPostProcessor)var5.next();
        result = beanProcessor.postProcessAfterInitialization(result, beanName);
        if(result == null) {
            return result;
        }
    }

    return result;
}

在Spring获取bean的规则中有这样一条:尽可能保证所有bean初始化后都会调用注册的BeanPostProcessor的postProcessAfterInitialization方法进行处理,在实际开发过程中大可以针对此特性设计自己的业务逻辑

获取单例

如果缓存中不存在已经加载的单例bean就需要从头开始bean的加载过程了,而Spring中使用getSingleton的重载方法实现bean的加载过程

public Object getSingleton(String beanName, ObjectFactory singletonFactory) {
    Assert.notNull(beanName, "\'beanName\' must not be null");
    Map var3 = this.singletonObjects;
    //全局变量需要同步
    synchronized(this.singletonObjects) {
        //首先检查对应的bean是否已经加载过,因为singleton模式其实就是复用以前创建的bean,所以这一步是必须的
        Object singletonObject = this.singletonObjects.get(beanName);
        //如果为null才可以进行singleton的bean的初始化
        if(singletonObject == null) {
            if(this.singletonsCurrentlyInDestruction) {
                throw new BeanCreationNotAllowedException(beanName, "Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)");
            }

            if(this.logger.isDebugEnabled()) {
                this.logger.debug("Creating shared instance of singleton bean \'" + beanName + "\'");
            }

            this.beforeSingletonCreation(beanName);
            boolean recordSuppressedExceptions = this.suppressedExceptions == null;
            if(recordSuppressedExceptions) {
                this.suppressedExceptions = new LinkedHashSet();
            }

            try {
                //初始化bean
                singletonObject = singletonFactory.getObject();
            } catch (BeanCreationException var13) {
                BeanCreationException ex = var13;
                if(recordSuppressedExceptions) {
                    Iterator var8 = this.suppressedExceptions.iterator();

                    while(var8.hasNext()) {
                        Exception suppressedException = (Exception)var8.next();
                        ex.addRelatedCause(suppressedException);
                    }
                }

                throw ex;
            } finally {
                if(recordSuppressedExceptions) {
                    this.suppressedExceptions = null;
                }

                this.afterSingletonCreation(beanName);
            }

            //加入缓存
            this.addSingleton(beanName, singletonObject);
        }

        return singletonObject != NULL_OBJECT?singletonObject:null;
    }
}

上述代码中其实是使用了回调方法,使得程序可以在单例创建的前后做一些准备及处理操作,而真正获取单例bean的方法其实并不是再此方法中实现的,其实现逻辑是在ObjectFactory类型的实例singletonFactory中实现的,而这些准备及处理操作包括如下内容

  • 检查缓存是否已经加载过
  • 若没有加载,则记录beanName的正在加载状态
  • 加载单例前记录加载状态

    beforeSingletonCreation方法是个空实现,其目的是:记录加载状态,也就是通过this.singletonsCurrentlyInCreation.add(beanName)将当前正在创建的bean记录在缓存中,这样便可以对循环依赖进行检测

     protected void beforeSingletonCreation(String beanName) {
        if(!this.singletonsCurrentlyInCreation.add(beanName)) {
            throw new BeanCurrentlyInCreationException(beanName);
        }
    }
    
  • 通过调用参数传入的ObjectFactory的个体Object方法实例化bean

  • 加载单例后的处理方法调用

    当bean加载结束后需要移除缓存中对该bean的正在加载状态的记录

    protected void afterSingletonCreation(String beanName) {
        if(!this.singletonsCurrentlyInCreation.remove(beanName)) {
            throw new IllegalStateException("Singleton \'" + beanName + "\' isn\'t currently in creation");
        }
    }
    
  • 将结果记录至缓存并删除加载bean过程中所记录的各种辅助状态

    protected void addSingleton(String beanName, Object singletonObject) {
        Map var3 = this.singletonObjects;
        synchronized(this.singletonObjects) {
            this.singletonObjects.put(beanName, singletonObject != null?singletonObject:NULL_OBJECT);
            this.singletonFactories.remove(beanName);
            this.earlySingletonObjects.remove(beanName);
            this.registeredSingletons.add(beanName);
        }
    }
    
  • 返回处理结果

    bean的加载逻辑其实是在传入的ObjectFactory类型的参数singletonFactory中定义的,反推参数的获取,得到如下代码:

    sharedInstance = getSingleton(beanName, new ObjectFactory(){
        public Object getObject() throws BeansException {
            try {
                return createBean(beanName, mbd, args);
            } catch(BeansException ex) {
                destroySingleton(beanName);
                throw ex;
            }
        }
    });
     

    ObjectFactory的核心部分其实只是调用了createBean的方法

    你可能感兴趣的:(Spring源码学习)