Spring的循环注入&&三级缓存(一)

首先找到 bean的生命周期的起点。就是从BeanDefinitionMap中创建bean实例的开始的地方。

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
AnnotationConfigApplicationContext构造函数的refresh()方法。
-----》finishBeanFactoryInitialization(beanFactory);    //实例化所有非懒加载的单例。
-----》》beanFactory.preInstantiateSingletons();

※※※※※※※※※※※※※※※※※※※※※※※※※※※

从DefaultListableBeanFactory的beanDefinitionNames(链表:记录的是所有deandefinitionmap中的beanName)获取所有的beanName并且遍历。
判断是单例且是懒加载后 执行getBean()方法。
-----》》》getBean(beanName)-----》》》》doGetBean()   。。。。。。doCreateBean() 

 



 

 一个普通的bean的实例化(单例,没有循环依赖)。

1、Object sharedInstance = getSingleton(beanName)

protected Object getSingleton(String beanName, boolean allowEarlyReference) {
//从一级缓存的读取,显然没有		
Object singletonObject = this.singletonObjects.get(beanName);
//isSingletonCurrentlyInCreation(beanName)返回false,因为不在创建中。
//如果在创建中,会放入一个set中。这个只有在循环依赖时用到
		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();
						this.earlySingletonObjects.put(beanName, singletonObject);
						this.singletonFactories.remove(beanName);
					}
				}
			}
		}
//所以只会返回null;
		return singletonObject;
	}

2、满足是单例

if (mbd.isSingleton()) {
		sharedInstance = getSingleton(beanName, () -> {
			try {
				return createBean(beanName, mbd, args);
			}
			catch (BeansException ex) {
				// Explicitly remove instance from singleton cache: It might have been put there
				// eagerly by the creation process, to allow for circular reference resolution.
			    // Also remove any beans that received a temporary reference to the bean.
				destroySingleton(beanName);
				throw ex;
			}
		});
		bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
	}

执行getSingleton方法。参数是beanName和一个lambda表达式。这里的getSingleton和上文的不是同一个函数

public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
		Assert.notNull(beanName, "Bean name must not be null");
		synchronized (this.singletonObjects) {
//从一级缓存获取(单例池),返回null
			Object singletonObject = this.singletonObjects.get(beanName);
			if (singletonObject == null) {
				
                ........

                
                //放入一个set表示当前的beanName的bean正在实例化,循环依赖时用到,
				beforeSingletonCreation(beanName);
				boolean newSingleton = false;
				boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
				if (recordSuppressedExceptions) {
					this.suppressedExceptions = new LinkedHashSet<>();
				}
				try {

                    //执行labmda表达式中的createBean方法。
					singletonObject = singletonFactory.getObject();
					newSingleton = true;
				}
				

                .......

				if (newSingleton) {
                    //放入单例池
					addSingleton(beanName, singletonObject);
				}
			}
			return singletonObject;
		}
	}

我们找到这个方法 doCreateBean方法:

if (instanceWrapper == null) {
//这里的instanceWrapper是bean原型的包装。
	instanceWrapper = createBeanInstance(beanName, mbd, args);
}
//这里的bean是实例化好的bean原型,其中没有注入过属性和经过AOP
final Object bean = instanceWrapper.getWrappedInstance();

.......

//这里的earlySingletonExposure 是true
//满足单例 && 默认允许循环注入 && 且在创建过程中。上文加入了Set。
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && isSingletonCurrentlyInCreation(beanName));
if (earlySingletonExposure) {
	if (logger.isDebugEnabled()) {
		logger.debug("Eagerly caching bean '" + beanName +
				"' to allow for resolving potential circular references");
	}
	addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}

 追踪addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));

这里的addSingletonFactory  向三级缓存中放入了一个beanName和一个lambda表达式

protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
		Assert.notNull(singletonFactory, "Singleton factory must not be null");
		synchronized (this.singletonObjects) {
			if (!this.singletonObjects.containsKey(beanName)) {
//向三级缓存中放入了一个beanName和一个lambda表达式
				this.singletonFactories.put(beanName, singletonFactory);
				this.earlySingletonObjects.remove(beanName);
				this.registeredSingletons.add(beanName);
			}
		}
	}

 之后完成属性注入 和initializeBean()方法。https://blog.csdn.net/wuyuwei/article/details/88252969  (aware 前置后置处理器??)

1. 对实现Aware接口对bean的设置。
2. 依次回调bean后置处理器的postProcessBeforeInitialization方法。
3. 执行实现了InitializingBean或者用户自定义的init方法方法。
4. 依次回调bean后置处理器的postProcessAfterInitialization方法。

最后还会执行一次getSingleton()方法,从三级缓存中取出对象返回。





 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值