Spring IOC循环依赖解决方案分析

Spring IOC循环依赖解决方案分析

这里Spring主要用了三层缓存来完成对循环依赖的实现。

下面的属性来源于DefaultSingletonBeanRegistry类

	/** Cache of singleton objects: bean name --> bean instance */
	private final Map singletonObjects = new ConcurrentHashMap(64);

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

	/** Cache of early singleton objects: bean name --> bean instance */
	private final Map earlySingletonObjects = new HashMap(16);

当获取一个类对象时,会调用这个方法

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);
	}


这里用到了上面的三层缓存。其中第一层是singletonObjects,首先会去看singletonObjects是否已经创建了一个对象。如果没有,那么从第二层缓存earlySingletonObjects提前曝光对象的缓存中获取;如果没有,那么最后从第三层缓存singletonFactories单实例工厂缓存中获取。当获取成功后,会把第三层缓存singletonFactories的bean去掉,加入到第二层缓存中。


下面就用一个例子来具体分析循环依赖的问题。

比如一个类A中有一个属性是B类,B类中有一个属性是A类,这时看Spring是怎么解决他们的相互依赖的。

Spring注入一个类的大体步骤分为两部分,一是先完成对类的构造工作,二是会对类的属性进行设置和填充。

首先Spring构造A类,通过AbstractAutowireCapableBeanFactory的doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)方法中调用addSingletonFactory方法将A类曝光到singletonFactories中。

addSingletonFactory(beanName, new ObjectFactory() {
				public Object getObject() throws BeansException {
					return getEarlyBeanReference(beanName, mbd, bean);
				}
			}); 
  

这时完成A的构造后,需要填充B属性,继续第二步,发现B还没有构造,于是开始B流程的构造过程,构造的时候发现需要填充A,从第三层缓存singletonFactories中找到A(此时的A还没有完全构造完成,但是可以拿到A的一个引用),B拿到A的引用后,完成B自己的填充属性工作,完成初始化工作,把自己放到第一层缓存singletonObjects中。这时回到A的这边,在拿到B对象后,完成自己的填充属性工作。

这就是Spring处理循环依赖基本的一些思路,有兴趣的同学可以自行研究一下相关的代码,还是比较有意思的,可以看到spring设计比较好的一些思路。


你可能感兴趣的:(Spring)