public class A{ private B b; } public class B{ private A a; }
public class A{ public A(B b){} } public class B{ private B(A a){} }
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
类中,如下: private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256); private final Map<String, Object> earlySingletonObjects = new HashMap<>(16); private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);
@Component public class Husband{ private String name; private Integer age; //丈夫依赖妻子 @Autowired private Wife wife; public String getName(){ return name; } public void setName(String name){ this.name = name; } public Integer getAge(){ return age; } public void setAge(Integer age){ this.age = age; } public Wife getWife(){ return wife; } public void setWife(Wife wife){ this.wife = wife; } } @Component public class Wife{ private String name; private Integer age; //妻子依赖丈夫 @Autowired private Husband husband; public String getName(){ return name; } public void setName(String name){ this.name = name; } public Integer getAge(){ return age; } public void setAge(Integer age){ this.age = age; } public Husband getHusband(){ return husband; } public void setHusband(Husband husband){ this.husband = husband; } }
1、finishBeanFactoryInitialization(beanFactory):实例化所有饿加载的单例Bean 2、org.springframework.beans.factory.config.ConfigurableListableBeanFactory#preInstantiateSingletons:真正的实现的方法 3、org.springframework.beans.factory.support.AbstractBeanFactory#getBean:调用方法获取Bean 4、org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean:真正getBean的方法 4.1 Object sharedInstance = getSingleton(beanName):先尝试从三级缓存中获取Bean,第一次执行缓存中肯定是没有 4.2 BeanFactory parentBeanFactory = getParentBeanFactory():检查父工厂中是否存在当前Bean 4.3 markBeanAsCreated(beanName):将Bean放入alreadyCreated中,表示当前Bean已经创建了 4.4 String[] dependsOn = mbd.getDependsOn():检查dependOn 4.5 Object bean = resolveBeforeInstantiation(beanName, mbdToUse):执行后置处理器,在初始化之前执行 4.6 sharedInstance = getSingleton(beanName, () -> {:调用getSingleton方法,尝试从单例缓存池中获取Bean 4.6.1 Object singletonObject = this.singletonObjects.get(beanName):从单例缓存池中获取 4.6.2 if (singletonObject == null) {:如果缓存池中没有,那么执行后续的逻辑,如果有直接返回即可 4.6.3 单例池中没有的逻辑:singletonObject = singletonFactory.getObject():调用createBean方法创建Bean 4.6.4 addSingleton(beanName, singletonObject):创建Bean成功之后将其添加到单例缓存池中,并且将其从二三级缓存中移除(singletonFactories,earlySingletonObjects) 4.7 return createBean(beanName, mbd, args):三级缓存和父工厂中都没有当前的这个Bean,此时进入创建Bean的过程 5、org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean:真正执行创建Bean的逻辑 5.1 instanceWrapper = createBeanInstance(beanName, mbd, args):调用构造器创建Bean,此时的Bean称为早期Bean,还未进行属性赋值 5.2 applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName):执行MergedBeanDefinitionPostProcessor,也是一种后置处理器 5.3 addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean)):将早期Bean放入三级缓存singletonFactories中,并且从早期对象缓存earlySingletonObjects中删除,用来解决循环依赖。 if (!this.singletonObjects.containsKey(beanName)) { this.singletonFactories.put(beanName, singletonFactory); this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } 5.4 populateBean(beanName, mbd, instanceWrapper):此时执行当前Bean的属性赋值 6、org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean:进行属性的赋值 6.1 if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) :执行实例化之后的后置处理器 6.2 for (BeanPostProcessor bp : getBeanPostProcessors()) :执行属性赋值之前的后置处理器,返回PropertyVlaue,我们知道@Autowired注解就是后置处理器实现的,主要的逻辑就是在postProcessProperties方法中,并且我们的例子中是自动注入Wife和Husband的,所以逻辑一定是在这里实现的,进入 7、org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#postProcessProperties:@Autowired注解解析,为标注该注解的属性赋值 7.1 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.AutowiredFieldElement#inject:真正执行赋值的逻辑 7.1.1 value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter):解析依赖 8、org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveDependency:解析标注@Autowired的依赖 8.1 org.springframework.beans.factory.support.DefaultListableBeanFactory#doResolveDependency:真正解析逻辑 8.2 instanceCandidate = descriptor.resolveCandidate(autowiredBeanName, type, this):从容器中获取指定的Bean,内部调用的又是getBean方法 9、org.springframework.beans.factory.config.DependencyDescriptor#resolveCandidate:至此,又回到了getBean这个方法,从ioc容器中获取指定的Bean
1、Husband创建Bean,先判断缓存池中是否存在,存在直接返回,不存在进入createBean创建的流程,调用构造方法创建一个早期的Bean【未进行属性赋值】,创建成功将其放入二级缓存 earlySingletonObjects
中,之后又调用 addSingletonFactory
方法将其放入三级缓存中并且将二级缓存中的移除,之后调用 populateBean
为属性赋值,在@Autowired的后置处理器中查找需要注入的依赖,发现Husband中的一个属性 Wife
,因此调用 getBean
方法从容器中获取,但是此时的Wife还未创建,因此又进入了doGetBean的流程,但是此时Wife并没有创建,因此在一二三级缓存中不能获取,又执行createBean方法创建Wife,同样调用构造方法创建一个早期Bean放入二级缓存中,调用 addSingletonFactory
放入三级缓存并移除二级缓存,然后调用 populateBean
方法为Wife属性赋值,在@Autowired的后置处理器中查找需要注入的依赖,发现Wife类中有一个属性是Husband,因此调用getBean方法,再次调用doGetBean获取Husband,但是此时的Husband已经创建成功【未赋值】,存放在三级缓存中,因此直接从三级缓存中取出Husband赋值给Wife属性,至此Wife属性已经赋值成功,直接添加到一级缓存( singletonObjects
)中并且移除三级缓存,直接返回给Husband赋值,因此Husband中的属性也持有了Wife的引用,都创建并且赋值成功了。