1.什么是循环依赖
假设Spring容器中有两个Bean:A和B
依赖关系如下:
A->B->A
@Component
public class CircularA {
@Autowired
private CircularB b;
public CircularA() {
}
public void setB(CircularB b) {
this.b = b;
}
}
@Component
public class CircularB {
@Autowired
private CircularA a;
public CircularB() {
}
public void setA(CircularA a) {
this.a = a;
}
}
Spring容器在创建BeanA的时候,发现需要依赖BeanB,那么在创建BeanB的时候,发现需要依赖BeanA,如此就形成循环依赖。
2. Spring怎么解决循环依赖
在网上有很多相关的博客解释Spring如何解决循环依赖Spring解决循环依赖。
简而言之就是Spring通过三级缓存来解决循环依赖。在Spring容器初始化过程中,通过beanName获取Bean的优先级依次是:一级缓存->二级缓存->三级缓存
/** 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);
当BeanFactory实例化BeanA后,BeanFactory会把刚刚实例化还没有依赖注入的Bean包装成一个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);
}
}
}
接下来进行依赖注入,如果存在循环依赖,例如A->B->A的情况,A实例化完毕,注入A.b的时候,要实例化B,发现依赖a,这个时候就要从BeanFactory中获取a实例,这个时候,缓存升级了。下面方法的第二个参数是true
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();
/** 将获取到的Bean从三级缓存中移除,并且升级到二级缓存中 */
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
return (singletonObject != NULL_OBJECT ? singletonObject : null);
}
b实例自己先完成实例化和依赖注入(这个时候a实例只是刚刚实例化,但是已经可以满足beanB的需求了)以及初始化等声明周期,最后在返回到a的创建流程中,a实例就可以注入已经成熟的b实例,a实例自身也顺利完成创建,由于b实例持有了a实例的引用,所以在后续的使用中是完全没有问题的。
如果Spring中不存在Bean的循环依赖,应该是不存在从三级缓存升级到二级缓存的场景,因为Spring是单线程初始化的。
这样Spring解决Bean循环依赖的问题!!!
3. BeanPostProcessor
BeanPostProcessor接口是用来对bean进行后置处理的,这个时候bean已经完成实例化和依赖注入了,属于bean初始化生命周期的一部分。
protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
invokeAwareMethods(beanName, bean);
return null;
}
}, getAccessControlContext());
}
else {
invokeAwareMethods(beanName, bean);
}
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
如果在BeanPostProcessor的接口中,对传入的bean进行了处理导致返回的bean和传入的bean不是同一个bean,这个正常情况是没有问题,很多中间件都是这么做的
但是!!!当Spring 循环依赖遇上BeanProcessor返回一个不一致对象的时候,就会发生问题了!!!
4. 异常情况
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'xxx': Bean with name 'xxx' has been injected into other beans [a,b,c] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
问题的描述就是这个样子了,大致的意思就是xxx这个bean已经注入到很多bean中了,只不过呢依赖xxx的bean中引用的不是它的最终版本,因为他们之间存在循环依赖的问题,在解决循环依赖中使用的是二级缓存中的early bean,而解决完循环依赖后,bean的引用发生了变化,导致了early bean和 expose bean不相等,所以抛出异常了!!!
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()) {
throw new BeanCurrentlyInCreationException(beanName,
"Bean with name '" + beanName + "' has been injected into other beans [" +
StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
"] in its raw version as part of a circular reference, but has eventually been " +
"wrapped. This means that said other beans do not use the final version of the " +
"bean. This is often the result of over-eager type matching - consider using " +
"'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
}
}
}
5. 解决方案
找到问题原因了,那么问题的解决通常就有了
项目中尽量避免Spring的循环引用,这本来就是不合理的。
使用@Lazy加载机制来解决