五、Spring循环依赖解决方案

本文详细解析了Spring框架中如何通过三级缓存结构解决单例Bean的循环依赖问题,涉及singletonObjects、earlySingletonObjects和singletonFactories的作用以及实例化过程中的策略。
摘要由CSDN通过智能技术生成

一、spring 概述
二、XML配置spring容器
三、Spring依赖注入类型
四、Spring依赖注入原理分析
五、Spring循环依赖解决方案


Spring循环依赖解决方案

Spring IocBean处于单例模式且setter注入方法下可以解决循环依赖依赖。对于单例作用域来说,Spring容器在整个生命周期内,有且只有一个Bean对象,所以很容易想到这个对象应该存在于缓存中。Spring为了解决单例Bean的循环依赖问题,使用了三级缓存。

1.1 三级缓存结构

所谓的三级缓存,在Spring中表现为三个Map对象,如代码清单1-17所示。这三个Map对象定义在DefaultSingletonBeanRegistry类中,该类是DefaultListableBeanFactory的父类。

代码清单1-16 DefaultSingletonBeanRegistry 中三级缓存

org.springframework.beans.factory.support.DefaultSingletonBeanRegistry

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

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

	/** Cache of early singleton objects: bean name to bean instance. */
	private final Map<String, Object> earlySingletonObjects = new ConcurrentHashMap<>(16);

代码清单1-16singletonObjects变量是第一级缓存,用来持有完整的Bean实例。而earlySingletonObjects中存放的是那些提前暴露的对象,也就是已经创建但还没有完成属性注入的对象,属于第二级缓存。最后的singletonFactories存放用来创建earlySingletonObjects的工厂对象,属于第三级缓存。

三级缓存如何协作解决循环依赖问题的呢?首先我们看一下获取Bean的代码流程。

代码清单1-17 DefaultSingletonBeanRegistry 中 getSingleton

org.springframework.beans.factory.support.DefaultSingletonBeanRegistry

	/**
	 * 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
	 */
	@Nullable
	protected Object getSingleton(String beanName, boolean allowEarlyReference) {
         // 首先从一级缓存 singletonObjects 中获取
		// Quick check for existing instance without full singleton lock
		Object singletonObject = this.singletonObjects.get(beanName);
         // 如果获取不到,则从二级缓存中 earlySingletonObjects 中获取
		if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
			singletonObject = this.earlySingletonObjects.get(beanName);
             // 如果还获取不到,就从三级缓存 singletonFactory 中获取
			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) {
								singletonObject = singletonFactory.getObject();
                                   // 从三级缓存中获取成功,就把对象从第三级缓存移动到第二级缓存
								this.earlySingletonObjects.put(beanName, singletonObject);
								this.singletonFactories.remove(beanName);
							}
						}
					}
				}
			}
		}
		return singletonObject;
	}

如代码清单1-17所示,我们不难理解对三级缓存的依次访问过程,但也可能还不是很理解Spring为什么要这样设计。事实上,解决循环依赖的关键还是要围绕Bean的生命周期。在实例化Bean的过程中,

代码清单1-18 Bean 实例化过程中的 addSingletonFactory 方法代码

org.springframework.beansfactory.support.AbstractAutowireCapableBeanFactory

	
    /**
	 * Actually create the specified bean. Pre-creation processing has already happened
	 * at this point, e.g. checking {@code postProcessBeforeInstantiation} callbacks.
	 * <p>Differentiates between default bean instantiation, use of a
	 * factory method, and autowiring a constructor.
	 * @param beanName the name of the bean
	 * @param mbd the merged bean definition for the bean
	 * @param args explicit arguments to use for constructor or factory method invocation
	 * @return a new instance of the bean
	 * @throws BeanCreationException if the bean could not be created
	 * @see #instantiateBean
	 * @see #instantiateUsingFactoryMethod
	 * @see #autowireConstructor
	 */
	protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
			throws BeanCreationException {

		// Instantiate the bean.
		......
		if (instanceWrapper == null) {
			instanceWrapper = createBeanInstance(beanName, mbd, args); // 创建 Bean 实例
		}
		Object bean = instanceWrapper.getWrappedInstance();
		......
		// 针对循环依赖问题,暴露单例工厂类
		// 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));
		}

		// Initialize the bean instance.
		Object exposedObject = bean;
		try {
			populateBean(beanName, mbd, instanceWrapper); // 初始化 Bean 实例,注入属性值
			exposedObject = initializeBean(beanName, exposedObject, mbd); // 初始化 Bean 实例,执行工厂回调,比如 init 和 bean post processors 
		}
		catch (Throwable ex) {
			if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
				throw (BeanCreationException) ex;
			}
			else {
				throw new BeanCreationException(
						mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
			}
		}

		......

		return exposedObject;
	}

Spring解决循环依赖的诀窍就在于singletonFactories这个第三级缓存,上述addSingletonFactory方法用于初始化这个第三级缓存中的数据,如代码清单1-19所示:

代码清单1-19 Bean 实例化过程中的 addSingletonFactory 方法代码

org.springframework.beansfactory.support.AbstractAutowireCapableBeanFactory

	/**
	 * Add the given singleton factory for building the specified singleton
	 * if necessary.
	 * <p>To be called for eager registration of singletons, e.g. to be able to
	 * resolve circular references.
	 * @param beanName the name of the bean
	 * @param singletonFactory the factory for the singleton object
	 */
	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);
			}
		}
	}

这段代码的执行时机是在已经通过构造函数创建Bean,但还没有完成对Bean中完整属性的注入的时候。换句话说,Bean已经可以被暴露出来进行识别了,但还不能正常使用。那为什么使用这种机制就可以解决循环依赖问题呢?

1.2 循环依赖解决方案

看下面使用setter循环依赖注入代码:

代码清单1-20 基于Setter方法注入的循环依赖代码
public class ClassA {
    private ClassB classB;
    @Autowired
    public void setClassB(ClassB classB) {
        this.classB = classB;
    }
}
public class ClassB {
    private ClassA classA;
    @Autowired
    public void setClassA(ClassA classA) {
        this.classA = classA;
    }
}

现在假设我们先初始化ClassA,步骤如下,详细步骤如下图所示。

  1. ClassA首先通过createBeanInstance方法创建了实例,并且将这个实例暴露到第三级缓存(singletonFactories)中,对应图中步骤1-7
  2. 然后,ClassA尝试通过populateBean方法注入属性,发现自己依赖ClassB这个属性,就会尝试去获取ClassB的实例,对应图中步骤8-15
  3. 显然,这时候ClassB还没有被创建,所以要走创建流程,对应图中步骤16-28
  4. ClassB在初始化的时候发现自己依赖了ClassA,就会尝试从第一级缓存(singletonObjects)去获取ClassA的实例。因为ClassA这时候还没有被创建完毕,所以它在第一级缓存和第二级缓存中都不存在。当尝试访问第三级缓存时,因为ClassA已经提前暴露了,所以ClassB能够通过singletonFactories拿到ClassA对象并顺利完成所有初始化流程,对应图中步骤29-40
  5. ClassB对象创建完成之后会被放到第一级缓存中,对应步骤41-42
  6. 随后返回创建的ClassB,回到ClassA populate流程,随后完成ClassA的创建流程。

spring循环依赖流程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值