标签:执行流程 pop proc 对象 start dep 判断 存在 过滤
@Component class A { @Autowired B b; } @Component class B { @Autowired A a; }
二、源码分析
DefaultListableBeanFactory(DefaultSingletonBeanRegistry).getSingleton DefaultListableBeanFactory(DefaultSingletonBeanRegistry).getSingleton 从singletonFactories拿到了这个A的bean DefaultListableBeanFactory(AbstractBeanFactory).doGetBean DefaultListableBeanFactory(AbstractBeanFactory).getBean 6.获取属性A的bean DependencyDescriptor.resolveCandidate DefaultListableBeanFactory.doResolveDependency DefaultListableBeanFactory.resolveDependency AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject 5.对B中的带注解Autowired属性A注入 InjectionMetadata.inject AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).populateBean DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).doCreateBean 4.创建B的bean(先实例化) DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBean AbstractBeanFactory$1.getObject() DefaultListableBeanFactory(DefaultSingletonBeanRegistry).getSingleton(String, ObjectFactory<?>) DefaultListableBeanFactory(AbstractBeanFactory).doGetBean 3.获取属性B的bean DefaultListableBeanFactory(AbstractBeanFactory).getBean DependencyDescriptor.resolveCandidate DefaultListableBeanFactory.doResolveDependency DefaultListableBeanFactory.resolveDependency AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject 2.对A中的带注解Autowired属性B注入 InjectionMetadata.inject AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).populateBean DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).doCreateBean 1.创建A的bean(先实例化) DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBean AbstractBeanFactory$1.getObject() DefaultListableBeanFactory(DefaultSingletonBeanRegistry).getSingleton(String, ObjectFactory<?>) DefaultListableBeanFactory(AbstractBeanFactory).doGetBean 断点调试 首次获取A的bean
private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<String, ObjectFactory<?>>(16); protected Object getSingleton(String beanName, boolean allowEarlyReference) { Object singletonObject = this.singletonObjects.get(beanName); // 关键点1,isSingletonCurrentlyInCreation方法 if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) { synchronized (this.singletonObjects) { singletonObject = this.earlySingletonObjects.get(beanName); if (singletonObject == null && allowEarlyReference) { // 关键点2,singletonFactories单例工厂对象 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); }
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) throws BeanCreationException { // Instantiate the bean. BeanWrapper instanceWrapper = null; if (mbd.isSingleton()) { instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) { instanceWrapper = createBeanInstance(beanName, mbd, args); } final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null); Class<?> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null); mbd.resolvedTargetType = beanType; // Allow post-processors to modify the merged bean definition. synchronized (mbd.postProcessingLock) { if (!mbd.postProcessed) { try { applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName); } catch (Throwable ex) { // 抛出异常... } mbd.postProcessed = true; } } // Eagerly cache singletons to be able to resolve circular references // even when triggered by lifecycle interfaces like BeanFactoryAware. // ********************** singletonFactories 设值的关键部分 START ********************** boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && isSingletonCurrentlyInCreation(beanName)); // isSingletonCurrentlyInCreation也是一个关键方法,与getSingleton方法相呼应 if (earlySingletonExposure) { addSingletonFactory(beanName, new ObjectFactory<Object>() { @Override public Object getObject() throws BeansException { return getEarlyBeanReference(beanName, mbd, bean); } }); } // ********************** singletonFactories 设值的关键部分 END ********************** // Initialize the bean instance. Object exposedObject = bean; try { populateBean(beanName, mbd, instanceWrapper); if (exposedObject != null) { exposedObject = initializeBean(beanName, exposedObject, mbd); } } catch (Throwable ex) { // 抛出异常。。。 } if (earlySingletonExposure) { Object earlySingletonReference = getSingleton(beanName, false); if (earlySingletonReference != null) { if (exposedObject == bean) { exposedObject = earlySingletonReference; } else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) { String[] dependentBeans = getDependentBeans(beanName); Set<String> actualDependentBeans = new LinkedHashSet<String>(dependentBeans.length); for (String dependentBean : dependentBeans) { if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) { actualDependentBeans.add(dependentBean); } } if (!actualDependentBeans.isEmpty()) { // 抛出异常。。。 } } } } // Register bean as disposable. try { registerDisposableBeanIfNecessary(beanName, bean, mbd); } catch (BeanDefinitionValidationException ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); } return exposedObject; }
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); } } }
总结:当循环依赖存在的时候,会一直循环调用doGetBean方法(ABABAB)...,Spring为我们解决了这一问题,使得doGetBean方法为ABA就结束了。
标签:执行流程 pop proc 对象 start dep 判断 存在 过滤
原文地址:https://www.cnblogs.com/caoxb/p/12862940.html