spring三级缓存--循环依赖

文章详细阐述了Spring框架中的一级、二级和三级缓存在处理bean生命周期和解决循环依赖问题中的作用。一级缓存保存完全初始化的bean,二级缓存用于存储正在初始化的bean,而三级缓存则涉及AOP代理的循环依赖解决。在处理ABean和BBean的循环依赖示例中,解释了每个级别的缓存如何协同工作以确保正确初始化。
摘要由CSDN通过智能技术生成

1、一级缓存:经历完整的springbean生命周期,保存已完成初始化的bean

2、二级缓存:保存正在初始化的bean(实例化完成没有注入属性),解决springbean的循环依赖

3、三级缓存:保存的时初始化完成的bean的工厂,解决springaop代理的循环依赖

工作过程:

1、当ABean实例化完成后,加入三级缓存,进入ABean初始化阶段,发现依赖BBean

2、当BBean实例化完成后,加入三级缓存,进入BBean初始化阶段,发现依赖ABean

3、BBean发现在ABean在三级缓存中刚好有,利用工厂缓存完成ABean的创建(ABean实例化完成但未赋值),存入二级缓存,删除三级缓存。

4、BBean完成了ABean的依赖注入(ABean就是个半成品,初始化了工厂缓存,实例化但没有赋值),证明BBean初始化完成,BBean放入到一级缓存。

5、ABean发现在BBean在一级缓存中,继续对BBean的赋值,也完成了初始化。

二级缓存就可以解决循环依赖了,

DefaultSingletonBeanRegistry#getSingleton

	/** Cache of singleton objects: bean name --> bean instance */
	private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(256);

	/** Cache of singleton factories: bean name --> ObjectFactory */
	private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<String, ObjectFactory<?>>(16);

	/** Cache of early singleton objects: bean name --> bean instance */
	private final Map<String, Object> earlySingletonObjects = new HashMap<String, Object>(16);
	/**
	 * Return the (raw) singleton object registered under the given name.
	 * <p>Checks already instantiated singletons and also allows for an early
	 * reference to a currently created singleton (resolving a circular reference).
	 * @param beanName the name of the bean to look for
	 * @param allowEarlyReference whether early references should be created or not
	 * @return the registered singleton object, or {@code null} if none found
	 */
	protected Object getSingleton(String beanName, boolean allowEarlyReference) {
		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();
						this.earlySingletonObjects.put(beanName, singletonObject);
						this.singletonFactories.remove(beanName);
					}
				}
			}
		}
		return (singletonObject != NULL_OBJECT ? singletonObject : null);
	}

三级缓存因为springaop特性,代理也会发生循环依赖的场景

proxy提前存入工厂缓存,实例化之前早早暴露出来。

AbstractAutowireCapableBeanFactory#doCreateBean

		// Eagerly cache singletons to be able to resolve circular references
		// even when triggered by lifecycle interfaces like BeanFactoryAware.
		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, new ObjectFactory<Object>() {
				@Override
				public Object getObject() throws BeansException {
					return getEarlyBeanReference(beanName, mbd, bean);
				}
			});
		}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值