6.2.3-AbstractBeanFactory
AbstractBeanFactory体系:
这个抽象类中很重要的实现了BeanFactory中得一个方法,doGetBean();
@SuppressWarnings("unchecked") protected <T> T doGetBean( final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException { // 获取原始的bean name,去除&,解决alias问题 final String beanName = transformedBeanName(name); Object bean; // 尝试从缓存中获取bean 调用的是接口中得方法,其实就是Map中存在的那个注册时候注册进去bean // Eagerly check singleton cache for manually registered singletons. 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 + "'"); } } //// 如果从缓存中或得bean,还需要判断是否是FactoryBean,并调用getObejct bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); } //缓存中不存在bean else { // Fail if we're already creating this bean instance: // We're assumably within a circular reference. // // 如果是原型scope,这边又是正在创建,说明有循环依赖,而原型的循环依赖Spring是不解决的 if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } //如果当前容器没有配置bean,那么去父容器查找 // Check if bean definition exists in this factory. BeanFactory parentBeanFactory = getParentBeanFactory(); if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { // Not found -> check parent. String nameToLookup = originalBeanName(name); 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信息,这边需要将之前的GenericBeanDefinition转化为RootBeanDefinition // 同时如果父bean的话,需要合并到子bean // Guarantee initialization of beans that the current bean depends on. String[] dependsOn = mbd.getDependsOn(); if (dependsOn != null) { for (String dependsOnBean : dependsOn) { if (isDependent(beanName, dependsOnBean)) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Circular depends-on relationship between '" + beanName + "' and '" + dependsOnBean + "'"); } registerDependentBean(dependsOnBean, beanName); getBean(dependsOnBean); } } //创建单例bean // Create bean instance. if (mbd.isSingleton()) { // 单例情况下,为解决循环依赖,在实例化之前,先新建一个ObjectFactory实例 sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() { @Override public Object getObject() throws BeansException { try { //创建Bean return createBean(beanName, mbd, args); } catch (BeansException ex) { // Explicitly remove instance from singleton cache: It might have been put there // eagerly by the creation process, to allow for circular reference resolution. // Also remove any beans that received a temporary reference to the bean. //创建异常直接销毁 destroySingleton(beanName); throw ex; } } }); bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } //创建多例bean else if (mbd.isPrototype()) { // It's a prototype -> create a new instance. Object prototypeInstance = null; try { //创建线程进行绑定说明这是一个多例bean beforePrototypeCreation(beanName); prototypeInstance = createBean(beanName, mbd, args); } finally { //移除线程上绑定的多例bean信息 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, new ObjectFactory<Object>() { @Override public Object getObject() throws BeansException { 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; } } //这边需要对实例进行类型校验,如果与requiredType不一致,需要委托TypeConverter尝试类型转换 // Check if required type matches the type of the actual bean instance. if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass())) { 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()); } } return (T) bean; }
其中主要调用这个方法的有:
//--------------------------------------------------------------------- // Implementation of BeanFactory interface //--------------------------------------------------------------------- @Override public Object getBean(String name) throws BeansException { return doGetBean(name, null, null, false); } @Override public <T> T getBean(String name, Class<T> requiredType) throws BeansException { return doGetBean(name, requiredType, null, false); } @Override public Object getBean(String name, Object... args) throws BeansException { return doGetBean(name, null, args, false); }
则这个是获取bean的几种方式,对应了doGetBean()中的几种判断。
具体如下
- 获取原始bean name
- 根据alia获取原始bean name
- 去除FactoryBean时的& [如果是需要获取FactoryBean自省,配置时需要在bean name前添加&]
- 尝试从缓存中获取实例
- 如果获取到实例,还要委托getObjectForBeanInstance解决FactoryBean的场景,就是调用getObject
- 判断原型场景的循环依赖问题,如果是原型同时bean又正在创建,说明是循环依赖,那直接抛异常,spring不尝试解决原型的循环依赖
- 如果在本容器中没有定义该bean,需要去父容器查找
- 如果有参数,结合参数初始化
- 如果没有参数,需要结合类型初始化,这边的调用是这个分支(当然这边一样没有类型)
- 如果不是类型检查,这边需要标记bean正在实例化
- bean实例化的准备工作
- 合并父bean的定义,并转化GenericBeanDefinition为RootBeanDefinition
- 校验BeanDefinition,如果是抽象类或者非原型带参数抛异常[这边注释说的是只有原型才可以配置构造方法的参数]
- 解决bean的依赖
- 注册依赖的bean
- 递归调用getBean实例化依赖bean
- 创建单例的实例
- 为解决循环依赖问题,这边使用ObjectFactory在实例化前先暴露bean
- 老规矩,需要委托getObejctForBeanInstance解决FactoryBean的问题
- 创建原型实例
- 创建前的准备工作,使用prototypesCurrentlyInCreation标记bean正在实例化
- 委托createBean实例化bean
- 创建后的善后工作,从prototypesCurrentlyInCreation中删除标记
- 老规矩,委托getObjectForBeanInstance解决工厂方法的问题
- 创建其他scope的实例,这边的逻辑结合了单例跟原型的处理逻辑,即使用解决循环依赖的ObjectFactory也使用prototypeCreation的标记
- 获取作用域scope,并校验是否已配置
- 使用ObjectFactory提早暴露实例
- 标记bean正在创建并委托createBean实例化
- 又是委托getObjectForBeanInstance解决工厂方法问题
- 最后需要对创建的实例进行类型校验,如果不一致,这边还需要委托TypeConverter进行类型装换
关于从父类DefaultSingletonBeanRegistry中继承过来的四种缓存的集合,
/** Cache of singleton objects: bean name --> bean instance */ private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(256); /** Cache of singleton factories: bean name --> ObjectFactory */ private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<String, ObjectFactory<?>>(16); /** Cache of early singleton objects: bean name --> bean instance */ private final Map<String, Object> earlySingletonObjects = new HashMap<String, Object>(16); /** Set of registered singletons, containing the bean names in registration order */ private final Set<String> registeredSingletons = new LinkedHashSet<String>(256);
四种集合的关系:
protected void addSingleton(String beanName, Object singletonObject) { synchronized (this.singletonObjects) { this.singletonObjects.put(beanName, (singletonObject != null ? singletonObject : NULL_OBJECT)); this.singletonFactories.remove(beanName); this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } }
protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) { Assert.notNull(singletonFactory, "Singleton factory must not be null"); synchronized (this.singletonObjects) { if (!this.singletonObjects.containsKey(beanName)) { this.singletonFactories.put(beanName, singletonFactory); this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } } }
protected Object getSingleton(String beanName, boolean allowEarlyReference) { Object singletonObject = this.singletonObjects.get(beanName); if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) { synchronized (this.singletonObjects) { singletonObject = this.earlySingletonObjects.get(beanName); if (singletonObject == null && allowEarlyReference) { ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName); if (singletonFactory != null) { singletonObject = singletonFactory.getObject(); this.earlySingletonObjects.put(beanName, singletonObject); this.singletonFactories.remove(beanName); } } } } return (singletonObject != NULL_OBJECT ? singletonObject : null); }
4个变量的关系如下:
- singletonObjects与singletonFactories,earlySingletonObjects,是互斥的.就是一个bean如果在其中任意一个变量中就,不会存在在另一变量中.这三个变量用于记录一个bean的不同状态.
- 如果bean已经添加到singletonObjects中,那么singltonFactories和earlySinletonObjects都不会考虑
- singltonFactories中的bean 通过 ObjectFactory的getObject实例化后,添加到earlySingletonObjects
梦想一定要有,万一实现了!