Spring杂记BeanFactory之getBean方法

Posted 颓废的悠然

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring杂记BeanFactory之getBean方法相关的知识,希望对你有一定的参考价值。

1.(BeanFactory) getBean(beanName)
  2.(AbstractBeanFactory) doGetBean
    3.(AbstractBeanFactory) transformedBeanName //如果是&beanName工厂类形式去掉第一个&
    4.(AbstractBeanFactory) getSingleton
      5.(DefaultSingletonBeanRegistry) getSingleton //从Map<String, Object> singletonObjects中获取实例如果存在的话(第一次不存在),有就返回
    6.(AbstractBeanFactory) getParentBeanFactory //看父类中是否包含了该beanName对应的实例,有就返回
    7.(AbstractBeanFactory) markBeanAsCreated(beanName) //this.alreadyCreated.add(beanName)
    8.(AbstractBeanFactory) getMergedLocalBeanDefinition(beanName)
      9.(AbstractBeanFactory) getMergedBeanDefinition(beanName, getBeanDefinition(beanName), null) //查看传入的bd是否有parentName,有的话需要合并。
      10.(AbstractBeanFactory) mbd.setScope(RootBeanDefinition.SCOPE_SINGLETON); //!StringUtils.hasLength(mbd.getScope()),如果该bean内部包含了一个非单例bean就将该bean的scope修正为内部的bean的scope
      11.(AbstractBeanFactory) this.mergedBeanDefinitions.put(beanName, mbd); //cache关系
    12.(AbstractBeanFactory) checkMergedBeanDefinition(mbd, beanName, args) //查看mbd.isAbstract(),是就抛出异常
    13.(AbstractBeanFactory) String[] dependsOn = mbd.getDependsOn(); //将依赖初始化
    14.(AbstractBeanFactory) sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() //如果是单例bean,就调用该方法
      15.(DefaultSingletonBeanRegistry) Object singletonObject = this.singletonObjects.get(beanName); //有就返回
        16.(AbstractBeanFactory) createBean(beanName, mbd, args)
          17.(AbstractAutowireCapableBeanFactory) createBean
            18.(AbstractAutowireCapableBeanFactory) mbd.prepareMethodOverrides
            19.(AbstractAutowireCapableBeanFactory) resolveBeforeInstantiation(beanName, mbd) // Give BeanPostProcessors a chance to return a proxy instead of the target bean 如果这步有bean就返回
            20.(AbstractAutowireCapableBeanFactory) doCreateBean(beanName, mbd, args)
              21.(AbstractAutowireCapableBeanFactory) createBeanInstance
                22.如果RootBeanDefinition mbd.getFactoryMethodName() != null就使用工厂方法初始化->instantiateUsingFactoryMethod(beanName, mbd, args)
                23.如果发现是构造器模式走23.1,否则走23.2
                  23.1.1.(AbstractAutowireCapableBeanFactory) autowireConstructor(beanName, mbd, ctors, args);
                    23.1.2.(ConstructorResolver) autowireConstructor
                    23.1.3.(ConstructorResolver) this.beanFactory.getInstantiationStrategy().instantiate(mbd, beanName, this.beanFactory, constructorToUse, argsToUse);
                      23.1.4.(SimpleInstantiationStrategy) instantiate
                  23.2.1.(AbstractAutowireCapableBeanFactory) instantiateBean(beanName, mbd)
                    23.2.2.(AbstractAutowireCapableBeanFactory) beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);
              24.(AbstractAutowireCapableBeanFactory) populateBean(beanName, mbd, instanceWrapper);//填充属性
              25.(AbstractAutowireCapableBeanFactory) initializeBean(beanName, exposedObject, mbd)
                26.(AbstractAutowireCapableBeanFactory) invokeAwareMethods(beanName, bean)
                27.(AbstractAutowireCapableBeanFactory) applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName)
                28.(AbstractAutowireCapableBeanFactory) invokeInitMethods(beanName, wrappedBean, mbd)
                29.(AbstractAutowireCapableBeanFactory) applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName)
              30.(AbstractAutowireCapableBeanFactory) registerDisposableBeanIfNecessary(beanName, bean, mbd);
      31.(DefaultSingletonBeanRegistry) addSingleton //this.singletonObjects.put,this.registeredSingletons.add
    32.(AbstractAutowireCapableBeanFactory) getObjectForBeanInstance(sharedInstance, name, beanName, mbd) //如果beanName是&开头的就返回factoryBean
    33.//如果传入的requiredType不为空,则调用 getTypeConverter().convertIfNecessary(bean, requiredType)
  34. return bean
35. return bean









































以上是关于Spring杂记BeanFactory之getBean方法的主要内容,如果未能解决你的问题,请参考以下文章

Spring AMQP杂记之Spring实现简述

Spring之BeanFactory详解

Spring之BeanFactory和FactoryBean的区别

转:Spring系列之beanFactory与ApplicationContext

Spring之BeanFactory和FactoryBean接口的区别

Spring系列之beanFactory与ApplicationContext