Spring源码分析-bean创建(循环依赖)
2021-01-25 01:12
标签:执行流程 pop proc 对象 start dep 判断 存在 过滤 二、源码分析 总结:当循环依赖存在的时候,会一直循环调用doGetBean方法(ABABAB)...,Spring为我们解决了这一问题,使得doGetBean方法为ABA就结束了。 Spring源码分析-bean创建(循环依赖) 标签:执行流程 pop proc 对象 start dep 判断 存在 过滤 原文地址:https://www.cnblogs.com/caoxb/p/12862940.html@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
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
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);
}
}
}
文章标题:Spring源码分析-bean创建(循环依赖)
文章链接:http://soscw.com/index.php/essay/46562.html