示例代码
public class A {
private B b;
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
public class B {
private A a;
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="a" class="com.self.cycle.A">
<property name="b" ref="b"/>
</bean>
<bean id="b" class="com.self.cycle.B">
<property name="a" ref="a"/>
</bean>
</beans>
执行流程图
-
循环依赖产生原因:发生时机在填充属性。
1.1: A实例化完成,开始填充B属性,发现B属性没有被创建, 开始创建B。
1.2: B实例化完成,开始填充A属性,发现A属性没有被创建, 开始创建A。
1.3: 由此行成闭环,造成循环依赖。 -
spring解决方案:
2.1:所有由doGetBean方法创建的bean,都放入三级缓存。
// 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.isTraceEnabled()) {
logger.trace("Eagerly caching bean '" + beanName +
"' to allow for resolving potential circular references");
}
//放入三级缓存
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}
//三级缓存也就是 singletonFactories
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);
}
}
}
//通过beanName 从三级缓存拿出来的也就是bean实例
protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) {
Object exposedObject = bean;
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
exposedObject = ibp.getEarlyBeanReference(exposedObject, beanName);
}
}
}
return exposedObject;
}
2.2:doGetBean 判断是创建bean还是获取bean在getSingleton。
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
// Quick check for existing instance without full singleton lock
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference) {
synchronized (this.singletonObjects) {
// Consistent creation of early reference within full singleton lock
singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null) {
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
// 这句也就是执行放入三级缓存的getEarlyBeanReference方法
// 也就是获取只被实例化的bean
singletonObject = singletonFactory.getObject();
// 使用中被放入二级缓存
this.earlySingletonObjects.put(beanName, singletonObject);
// 从三级缓存除去
this.singletonFactories.remove(beanName);
}
}
}
}
}
}
return singletonObject;
}
这段核心是,先从一级缓存里找,找不到,再由二级缓存里找,找不到,执行三级缓存里的lambda,拿出只被实例化的bean。
- 示例代码的解决流程
1.1: A实例化完成,放入三级缓存,开始填充B属性,发现B属性没有被创建, 开始创建B;
1.2: B实例化完成,放入三级缓存,开始填充A属性,发现A属性没有被创建, 开始创建A;
1.3: 调用doGetBean下的getSingleton发现A位于三级缓存(A此时还没有被填充属性),取出A将B填充属性完成;
1.4:将B填充到A中, A填充属性完成;
1.5:循环依赖问题解决。