Spring解决循环依赖的个人理解

首先将Spring如何解决Bean的循环依赖之前得先搞明白,SpringBean的生命周期是怎样的

bean的生命周期简单可以理解为三大步

第一步是进行bean的实例化,要实现bean的实例化,大前提是要先启动上下文,去加载注解orXML或者其他地方写的配置,将配置加载进beanfactory中,那么就需要去初始化beanfactory了,这就引申到了abstractApplicationContext中的refresh

方法了。在这个方法里由12个关键的方法,其实这些个关键的方法也是分为几个大类,第一大类,为创建beanFactory服务,第二大类,为创建Bean服务,第三大类,为注销或清除bean服务

第一大类;

prepareRefresh,此处设置容器的部分启动数据,如启动时间,初始化容器监听器,增强器的空集合,获取Environment对象等

ObtainFreshBeanFactory(),获取核心beanfactory对象--》DefaultListablebeanFactory

prepareBeanFactory(),加载beanFactory的相关属性,比如classLoader,beanPostProcessor等

第二大类的核心就是为创建bean服务,最核心方法就是finishBeanFactoryIntialication

在这个方法里可以粗略的将bean的创建分为三个过程

createBeanInstance

创建bean的引用,实例化bean,在堆中为Bean开辟存储位置

populateBean

初始化bean的第一步,就是将bean的属性赋值(基本类型直接赋值,对象类型,由于在前期doGetBean中读取以来创建对象时,已经将依赖提前创建好了,)

initializeBean

此处就有四个动作,

第一步加载aware,这部分aware接口主要负责的是,beanNameaware,beanclassLoaderaware,BeanFactoryaware

第二步是执行beanPostProcessorBeforeBeanIntialization,此处回去进行applicationContextAware,EnvironmentAware,resourceLoaderAware的注入

第三步为初始化的方法,invokeInitMethod执行初始化方法

第四步为,执行beanpostProcessorAfterBeanintialization方法

最终完成对象的创建

springbean的初始化说完了,那么循环依赖发生在什么地方呢?

简单举个例子

A中有B的属性,所以A的创建依赖与B的创建,但是这个B呢,内部也存在者A对象的引用,那么他就会去创建A对象,这就是典型的循环依赖

如何解决呢?

首先上面说了,创建一个Bean对象实际是一个三步走战略,所以每一步都会去做对应的处理,不绕弯子了,其实解决循环依赖靠的是三级缓存

DefaultSingletonBeanRegistry	

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

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

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

第一级缓存为;singletonObjects,当对象创建完成,beanfactory会将他放到一级缓存中以供使用

第三级缓存是当实例化完成之后会将提前暴露自己的引用,此时还未完成初始化,

那么循环依赖是怎么依靠三级缓存去解决呢?

由于B的创建需要依赖A,所以他会主动去创建A对象,创建A对象的时候已经将完整的依赖关系存储到缓存中了,

sharedInstance = getSingleton(beanName, () -> {
						try {
							return createBean(beanName, mbd, args);
						}
						catch (BeansException ex) {
							// Explicitly remove instance from singleton cache: It might have been put there
							// eagerly by the creation process, to allow for circular reference resolution.
							// Also remove any beans that received a temporary reference to the bean.
							destroySingleton(beanName);
							throw ex;
						}
					});

这里有一点需要注意一下就是函数式编程

public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
		Assert.notNull(beanName, "Bean name must not be null");
		synchronized (this.singletonObjects) {
			Object singletonObject = this.singletonObjects.get(beanName);
			if (singletonObject == null) {
				if (this.singletonsCurrentlyInDestruction) {
					throw new BeanCreationNotAllowedException(beanName,
							"Singleton bean creation not allowed while singletons of this factory are in destruction " +
							"(Do not request a bean from a BeanFactory in a destroy method implementation!)");
				}
				if (logger.isDebugEnabled()) {
					logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
				}
				//标识自己正在创建
				beforeSingletonCreation(beanName);
				boolean newSingleton = false;
				boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
				if (recordSuppressedExceptions) {
					this.suppressedExceptions = new LinkedHashSet<>();
				}
				try {
					//创建对象执行createBean方法
					singletonObject = singletonFactory.getObject();
					newSingleton = true;
				}
				catch (IllegalStateException ex) {
					// Has the singleton object implicitly appeared in the meantime ->
					// if yes, proceed with it since the exception indicates that state.
					singletonObject = this.singletonObjects.get(beanName);
					if (singletonObject == null) {
						throw ex;
					}
				}
				catch (BeanCreationException ex) {
					if (recordSuppressedExceptions) {
						for (Exception suppressedException : this.suppressedExceptions) {
							ex.addRelatedCause(suppressedException);
						}
					}
					throw ex;
				}
				finally {
					if (recordSuppressedExceptions) {
						this.suppressedExceptions = null;
					}
					//将正在创建的状态移除,标识自己已经创建完成
					afterSingletonCreation(beanName);
				}
				if (newSingleton) {
					addSingleton(beanName, singletonObject);
				}
			}
			return singletonObject;
		}
	}

上面标识自己正在创建的状态,在下面的判断中使用,接下来A就会在

Object beanInstance = doCreateBean(beanName, mbdToUse, args);中去进行实例化,在populateBean是遇到了B未创建,就回去创建B,而A为创建完成但是已经标识自己正在创建中,如果B创建需要A就会执行以下代码
@Nullable
	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;
	}

在populateBean中有这样一段代码,解释了何处去创建B的对象

if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
			MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
			// Add property values based on autowire by name if applicable.
			if (resolvedAutowireMode == AUTOWIRE_BY_NAME) {
				autowireByName(beanName, mbd, bw, newPvs);
			}
			// Add property values based on autowire by type if applicable.
			if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
				autowireByType(beanName, mbd, bw, newPvs);
			}
			pvs = newPvs;
		}





protected void autowireByName(
			String beanName, AbstractBeanDefinition mbd, BeanWrapper bw, MutablePropertyValues pvs) {

		String[] propertyNames = unsatisfiedNonSimpleProperties(mbd, bw);
		for (String propertyName : propertyNames) {
			if (containsBean(propertyName)) {
				Object bean = getBean(propertyName);
				pvs.add(propertyName, bean);
				registerDependentBean(propertyName, beanName);
				if (logger.isDebugEnabled()) {
					logger.debug("Added autowiring by name from bean name '" + beanName +
							"' via property '" + propertyName + "' to bean named '" + propertyName + "'");
				}
			}
			else {
				if (logger.isTraceEnabled()) {
					logger.trace("Not autowiring property '" + propertyName + "' of bean '" + beanName +
							"' by name: no matching bean found");
				}
			}
		}
	}

这两处代码展示了,当在给A属性赋值前,会把AutoWired注解的属性进行创建,这样在回想一下之前所说的,创建bean对象的过程,是否清晰了呢

因为此处创建B对象,但B对象依赖A对象,但是一级缓存中并未有A对象,所以判断A对象有没有在创建,发现A对象在创建,那么就会去二级缓存中找,此时一般情况下,由于是先执行createBeanInstance,那么二级缓存中必然存在A对象提前暴露的引用,这样B对象就可以完成创建,然后对象A也就可以完成创建

此篇文章仅对AutoWired注入的属性循环依赖进行解释(我还没看懂构造器中的是咋解决的,所以我先不解释,哈哈)

整个spring解决循环依赖的过程就是这样,是不是很简单。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值