4.spring ioc源码解析之解决循环依赖-司马
spring-循环依赖 uml图
@startuml
'https://plantuml.com/sequence-diagram
autonumber
autonumber
autoactivate on
[-> AbstractBeanFactory : doGetBean()
AbstractBeanFactory -> AbstractBeanFactory : transformedBeanName : 处理beanName,返回一个真正的名字
return String : beanName
AbstractBeanFactory -> DefaultSingletonBeanRegistry : getSingleton(beanName) : 试图从缓存中获取
return 如果有,则直接返回;如果没有,则去创建bean对象
AbstractBeanFactory -> DefaultSingletonBeanRegistry : getSingleton(beanName,singletonFactory) : 试图从缓存中获取
DefaultSingletonBeanRegistry -> xx : beforeSingletonCreation : 加标识,表示当前bean正在被创建
return void
DefaultSingletonBeanRegistry -> ObjectFactory : getObject() 调用 FactoryBean去创建bean对象 :
ObjectFactory -> AbstractAutowireCapableBeanFactory : createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
AbstractAutowireCapableBeanFactory -> AbstractAutowireCapableBeanFactory : doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
AbstractAutowireCapableBeanFactory -> AbstractAutowireCapableBeanFactory :createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
return a BeanWrapper for the new instance : 此时对象的属性还没有赋值
AbstractAutowireCapableBeanFactory -> DefaultSingletonBeanRegistry : addSingletonFactory(String beanName,ObjectFactory<?> singletonFactory) : Add the given singleton factory for building the specified singleton if necessary.
return void
AbstractAutowireCapableBeanFactory -> AbstractAutowireCapableBeanFactory : populateBean(beanName,mbd,instanceWrapper): Populate the bean instance in the given BeanWrapper with the property values from the bean definition.
AbstractAutowireCapableBeanFactory -> AbstractAutowireCapableBeanFactory : applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) :
AbstractAutowireCapableBeanFactory -> BeanDefinitionValueResolver : valueResolver.resolveValueIfNecessary(pv, originalValue);
BeanDefinitionValueResolver -> BeanDefinitionValueResolver : resolveReference(Object argName, RuntimeBeanReference ref)
BeanDefinitionValueResolver -> BeanDefinitionValueResolver : this.beanFactory.getBean(refName); :去创建被引用的对象,重新走一遍上面的流程
return Object
return Object
return the resolved object
return void
return void
return a new instance of the bean
return a new instance of the bean
return the resulting instance
return the registered singleton obejct
<--> AbstractBeanFactory : return an instance of the bean
@enduml