spring中循环依赖(循环引用)的解决
循环依赖就是循环引用,两个或以上的bean互相之间引用,比如A引用B,B又引用A,最终变成了一个环。
Spring中如何解决循环依赖
spring容器循环依赖包括构造器循环依赖和setter循环依赖。
源码分析:
查看源码DefaultSingletonBean

此方法是创建bean的整个过程,其中第一步的方法操作是将当前要创建的bean放入”当前创建bean池“中。如下方法。
/** Names of beans that are currently in creation */
private final Set<String> singletonsCurrentlyInCreation = Collections.synchronizedSet(new HashSet<String>());
/**
* Callback before singleton creation.
* <p>Default implementation register the singleton as currently in creation.
* @param beanName the name of the singleton about to be created
* @see #isSingletonCurrentlyInCreation
*/
protected void beforeSingletonCreation(String beanName) {
if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.add(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
}
第二步:代表真正的创建bean过程以及构造函数、属性填充、beanPostProcesser等特殊操作。
也正是因为这一步,可能出现属性依赖另一个bean,或者构造函数对bean的依赖,所以,需要对依赖的bean首先加载。也是bean的循环依赖的事故发生点。如果在singletonsCurrentlyInCreation的当前创建池中已经存在了bean,则就抛出异常
第三步:这个时候当前创建的bean才算基本创建完成,从singletonsCurrentlyInCreation的当前正在创建池中remve掉。
/**
* Callback after singleton creation.
* <p>Default implementation marks the singleton as not in creation anymore.
* @param beanName the name of the singleton that has been created
* @see #isSingletonCurrentlyInCreation
*/
protected void afterSingletonCreation(String beanName) {
if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.remove(beanName)) {
throw new IllegalStateException("Singleton '" + beanName + "' isn't currently in creation");
}
}
第四步:将原始创建的bean放入内部缓存中。供以后直接从缓存中拿。也是便于有依赖bean的情况下的提前暴露bean作基础
/**
* Add the given singleton object to the singleton cache of this factory.
* <p>To be called for eager registration of singletons.
* @param beanName the name of the bean
* @param singletonObject the singleton object
*/
protected void addSingleton(String beanName, Object singletonObject) {
synchronized (this.singletonObjects) {
this.singletonObjects.put(beanName, (singletonObject != null ? singletonObject : NULL_OBJECT));
this.singletonFactories.remove(beanName);
this.earlySingletonObjects.remove(beanName);
this.registeredSingletons.add(beanName);
}
}
1.构造器循环依赖。
此依赖是无法解决的,只能抛出BeanCurrentlyInCreationException异常表示循环依赖。
例如:

当在创建demoBean的时候,添加到了当前创建bean池中,,然后去创建,当需要构造器参数”b“,则又去先去加载b的bean(此时 demoBean还未实例化,因为实例化必须调用构造函数,所以demoBean还未提前暴露出,还在当前创建池中)
创建b的时候,查看当前创建池中是否存在,不存在则继续准备其需要的构造器参数demoBean,并将b的标识符放入当前创建bean池中。
创建demoBean的时候,查看池中存在了该bean,则抛出BeanCurrentlyInCreationException异常,因为循环依赖。
2、setter循环依赖
通过setter方式来注入构成的循环依赖。对此,spring容器是通过提前暴露刚完成的构造器注入但未完成其他步骤(如setter注入)的bean来完成的。而且只能解决scope作用域为单例sigleton的bean循环依赖。通过提前暴露一个单例工厂方法。从而使其他bean能引用到该bean。
查看源码AbstractAutowireCapableBeanFactory的doCreateBean

提前暴露ObjectFactory的前提是当前bean成功被实例化后,在初始化bean之前,也就是填充属性之前,刚成功调用构造器之后。
根据 scope是否是单例、是否允许循环引用并且当前创建池中存在当前bean 这三个条件则调用addSingletonFactory来提前暴露一个ObjectFactory到缓存中,供单例循环使用。
可以通过setAllowCircullarReference(false)来禁用循环引用。
3、prototype范围的依赖处理。
对于原型作用域来说,spring容器也无法解决循环依赖的问题。因为Spring容器不进行bean的缓存。因此无法提前暴露一个正在创建中的bean。



被折叠的 条评论
为什么被折叠?



