Spring---循环依赖-@Autowired
/** * 【循环依赖---@Autowired】 * * BeanPostProcessor : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor * * org.springframework.beans.factory.config.BeanPostProcessor implementation that autowires annotated fields, setter methods, and arbitrary config methods. * BeanPostProcessor实现,用来自动织入 注解字段...; * Such members to be injected are detected through annotations: by default, Spring's @Autowired and @Value annotations. * 一些成员将被检测到:Spring的@Autowired、@Value; * * public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware { * * public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {} * } * * 1、 * DefaultSingletonBeanRegistry#getSingleton(String beanName){ * 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(); === 2、 * this.earlySingletonObjects.put(beanName, singletonObject); * this.singletonFactories.remove(beanName); * } * } * } * } * return singletonObject; * } * * 2、 * AbstractAutowireCapableBeanFactory#getEarlyBeanReference{ * Object exposedObject = bean; * if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { * for (BeanPostProcessor bp : getBeanPostProcessors()) { * if (bp instanceof SmartInstantiationAwareBeanPostProcessor) { * SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp; * exposedObject = ibp.getEarlyBeanReference(exposedObject, beanName); * } * } * } * return exposedObject; * } * * 3、 * DefaultSingletonBeanRegistry#getSingleton(String, ObjectFactory){ * ... * singletonObject = singletonFactory.getObject(); * ... * if (newSingleton) { * addSingleton(beanName, singletonObject); === 4、 * } * ... * } * * 4、DefaultSingletonBeanRegistry#addSingleton{ * synchronized (this.singletonObjects) { * this.singletonObjects.put(beanName, singletonObject); * this.singletonFactories.remove(beanName); * this.earlySingletonObjects.remove(beanName); * this.registeredSingletons.add(beanName); * } * } * * 5、 * DefaultSingletonBeanRegistry#addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory){ * synchronized (this.singletonObjects) { * if (!this.singletonObjects.containsKey(beanName)) { * this.singletonFactories.put(beanName, singletonFactory); * this.earlySingletonObjects.remove(beanName); * this.registeredSingletons.add(beanName); * } * } * } * * * 实现思路: * * AbstractBeanFactory#getBean * -> * AbstractBeanFactory#doGetBean{ * ... * Object sharedInstance = getSingleton(beanName); === 1、 * if (sharedInstance != null && args == null) { * ... * } * else{ * ... * if (mbd.isSingleton()) { * sharedInstance = getSingleton(beanName, () -> { === 3、 * return createBean(beanName, mbd, args); * } * ... * } * ... * } * ... * } * -> * DefaultSingletonBeanRegistry#getSingleton(String, ObjectFactory) -> singletonFactory.getObject() * AbstractBeanFactory#createBean <- * -> * AbstractAutowireCapableBeanFactory#createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args){ * ... * Object beanInstance = doCreateBean(beanName, mbdToUse, args); * ... * } * -> * AbstractAutowireCapableBeanFactory#doCreateBean(java.lang.String, RootBeanDefinition, java.lang.Object[]){ * ... * instanceWrapper = createBeanInstance(beanName, mbd, args); * ... * boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && isSingletonCurrentlyInCreation(beanName)); * if (earlySingletonExposure) { * addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean)); === 5、 * } * ... * populateBean(beanName, mbd, instanceWrapper); * ... * } * -> * AbstractAutowireCapableBeanFactory#populateBean{ * ... * if (hasInstAwareBpps) { * for (BeanPostProcessor bp : getBeanPostProcessors()) { * ... * PropertyValues pvsToUse = ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName); * ... * } * } * ... * } * -> * AutowiredAnnotationBeanPostProcessor#postProcessProperties{ * ... * metadata.inject(bean, beanName, pvs); * ... * } * -> * InjectionMetadata#inject{ * ... * element.inject(target, beanName, pvs); * ... * } * -> * AutowiredAnnotationBeanPostProcessor.AutowiredFieldElement#inject{ * ... * value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter); * ... * } * -> * DefaultListableBeanFactory#resolveDependency{ * ... * result = doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter); * ... * } * -> * DefaultListableBeanFactory#doResolveDependency{ * ... * instanceCandidate = descriptor.resolveCandidate(autowiredBeanName, type, this); * ... * } * DependencyDescriptor#resolveCandidate{ * return beanFactory.getBean(beanName); * } * -> * 递归 * * */