写点什么

spring 循环依赖与三级缓存

用户头像
Rubble
关注
发布于: 2 小时前
spring 循环依赖与三级缓存

当有两个类 A、B 相互依赖时便会形成循环依赖,多个类之间相互依赖也会存在循环依赖。


@Servicepublic class ServiceA {  @Autowired  private ServiceB serviceB;
public String say(){ return "hello"; }}
@Servicepublic class ServiceB {
@Autowired private ServiceA serviceA;
}
复制代码


当构造方法注入依赖时循环无解,会有如下提示


The dependencies of some of the beans in the application context form a cycle:
cycleController (field com.paw.cycle.dependency.service.ServiceA com.paw.cycle.dependency.controller.CycleController.serviceA)┌─────┐| serviceA defined in file [/Users/.../target/classes/com/paw/cycle/dependency/service/ServiceA.class]↑ ↓| serviceB defined in file [/Users/.../target/classes/com/paw/cycle/dependency/service/ServiceB.class]└─────┘
复制代码


看下 spring 是如何解决循环依赖的


开启日志


logging:  level:    org.springframework: trace
复制代码


运行查看日志


main] o.s.b.f.s.DefaultListableBeanFactory     : Finished creating instance of bean 'cycleDependencyApplication'main] o.s.b.f.s.DefaultListableBeanFactory     : Returning cached instance of singleton bean 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory'main] o.s.b.f.s.DefaultListableBeanFactory     : Creating shared instance of singleton bean 'cycleController'main] o.s.b.f.s.DefaultListableBeanFactory     : Creating instance of bean 'cycleController'main] o.s.b.f.s.DefaultListableBeanFactory     : Eagerly caching bean 'cycleController' to allow for resolving potential circular referencesmain] o.s.b.f.s.DefaultListableBeanFactory     : Finished creating instance of bean 'cycleController'main] o.s.b.f.s.DefaultListableBeanFactory     : Creating shared instance of singleton bean 'serviceA'main] o.s.b.f.s.DefaultListableBeanFactory     : Creating instance of bean 'serviceA'main] o.s.b.f.annotation.InjectionMetadata     : Registered injected element on class [com.paw.cycle.dependency.service.ServiceA]: AutowiredFieldElement for private com.paw.cycle.dependency.service.ServiceB com.paw.cycle.dependency.service.ServiceA.serviceBmain] o.s.b.f.s.DefaultListableBeanFactory     : Eagerly caching bean 'serviceA' to allow for resolving potential circular referencesmain] o.s.b.f.annotation.InjectionMetadata     : Processing injected element of bean 'serviceA': AutowiredFieldElement for private com.paw.cycle.dependency.service.ServiceB com.paw.cycle.dependency.service.ServiceA.serviceBmain] o.s.b.f.s.DefaultListableBeanFactory     : Creating shared instance of singleton bean 'serviceB'main] o.s.b.f.s.DefaultListableBeanFactory     : Creating instance of bean 'serviceB'main] o.s.b.f.annotation.InjectionMetadata     : Registered injected element on class [com.paw.cycle.dependency.service.ServiceB]: AutowiredFieldElement for private com.paw.cycle.dependency.service.ServiceA com.paw.cycle.dependency.service.ServiceB.serviceAmain] o.s.b.f.s.DefaultListableBeanFactory     : Eagerly caching bean 'serviceB' to allow for resolving potential circular referencesmain] o.s.b.f.annotation.InjectionMetadata     : Processing injected element of bean 'serviceB': AutowiredFieldElement for private com.paw.cycle.dependency.service.ServiceA com.paw.cycle.dependency.service.ServiceB.serviceAmain] o.s.b.f.s.DefaultListableBeanFactory     : Returning eagerly cached instance of singleton bean 'serviceA' that is not fully initialized yet - a consequence of a circular referencemain] f.a.AutowiredAnnotationBeanPostProcessor : Autowiring by type from bean name 'serviceB' to bean named 'serviceA'main] o.s.b.f.s.DefaultListableBeanFactory     : Finished creating instance of bean 'serviceB'main] f.a.AutowiredAnnotationBeanPostProcessor : Autowiring by type from bean name 'serviceA' to bean named 'serviceB'main] o.s.b.f.s.DefaultListableBeanFactory     : Finished creating instance of bean 'serviceA'main] o.s.b.f.s.DefaultListableBeanFactory     : Returning cached instance of sinleton bean 'serviceB'    
复制代码


可以梳理到基本流程:


创建 ing serviceA==>需要注入 serviceB==>缓存半成品 serviceA ==>创建 ing serviceB==>需要注入 serviceA==>缓存半成品 serviceB==>注入缓存的半成品 serviceA==>完成创建 serviceB==>service 注入 serviceB ==>完成 serviceA 创建。


DefaultSingletonBeanRegistry 的三级缓存


// 一级实例化好的bean/**Cache of singleton objects: bean name to bean instance.*/private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
// 二级半成品 未进行属性注入/** Cache of early singleton objects: bean name to bean instance. */private final Map<String, Object> earlySingletonObjects = new HashMap<>(16);
// 三级 生成bean的ObjectFactory/** Cache of singleton factories: bean name to ObjectFactory. */private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);
复制代码


获取 bean 实例对象


先从一级取 》再从二级取 》最后从三级取


@Nullableprotected 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;}
复制代码


加入条件断点,开始顺腾摸瓜


beanName.equals("serviceA")



类的继承关系


SimpleAliasRegistry (org.springframework.core)


|-DefaultSingletonBeanRegistry (org.springframework.beans.factory.support)


|-|-FactoryBeanRegistrySupport (org.springframework.beans.factory.support)


|-|-|-AbstractBeanFactory (org.springframework.beans.factory.support)


|-|-|-|-AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)


|-|-|-|-|-DefaultListableBeanFactory (org.springframework.beans.factory.support)


|-|-|-|-|-XmlBeanFactory (org.springframework.beans.factory.xml)


三级缓存 objectFactory 通过匿名内部类实现 (匿名类 调用了 AbstractAutowireCapableBeanFactory#getEarlyBeanReference 方法) getEarlyBeanReference 获取早期的半成品对象。


AbstractAutowireCapableBeanFactoryprotected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)  throws BeanCreationException {      ...    addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));    ...}
复制代码


singletonFactory AbstractAutowireCapableBeanFactory


// 加入三级缓存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);  } }}
复制代码


// 由 ObjectFactory 创建 bean


AbstractBeanFactory#doGetBean// 创建bean实例sharedInstance = getSingleton(beanName, () -> { try {  return createBean(beanName, mbd, args); } ... }
==> DefaultSingletonBeanRegistry#getSingletonpublic 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 + "'"); } // 创建bean之前的处理 beforeSingletonCreation(beanName); boolean newSingleton = false; boolean recordSuppressedExceptions = (this.suppressedExceptions == null); if (recordSuppressedExceptions) { this.suppressedExceptions = new LinkedHashSet<>(); } try { // 通过三级缓存的objectFactory获取bean对象实例 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; }
复制代码


AbstractAutowireCapableBeanFactory 创建 bean


添加到三级缓存==>创建 Eagerly caching bean ===>注入 bean 属性实例化 bean


protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)  throws BeanCreationException {
// Instantiate the bean. BeanWrapper instanceWrapper = null; if (mbd.isSingleton()) { instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) { instanceWrapper = createBeanInstance(beanName, mbd, args); } final Object bean = instanceWrapper.getWrappedInstance(); Class<?> beanType = instanceWrapper.getWrappedClass(); if (beanType != NullBean.class) { mbd.resolvedTargetType = beanType; }
// Allow post-processors to modify the merged bean definition. synchronized (mbd.postProcessingLock) { if (!mbd.postProcessed) { try { applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName); } catch (Throwable ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Post-processing of merged bean definition failed", ex); } mbd.postProcessed = true; } }
// 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"); } // 添加三级缓存 beanFactory addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean)); }
// Initialize the bean instance. Object exposedObject = bean; try { // 填充bean属性 即注入@Autowired字段 populateBean(beanName, mbd, instanceWrapper); exposedObject = initializeBean(beanName, exposedObject, mbd); } 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); } }
if (earlySingletonExposure) { 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<>(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."); } } } }
// Register bean as disposable. try { registerDisposableBeanIfNecessary(beanName, bean, mbd); } catch (BeanDefinitionValidationException ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); }
return exposedObject;}
复制代码


属性注入 serviceB


填充属性


//AbstractAutowireCapableBeanFactoryprotected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) 
复制代码


AbstractAutowireCapableBeanFactory#postProcessProperties


InjectionMetadata 类进行属性注入


public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) { InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass(), pvs); try {  metadata.inject(bean, beanName, pvs); } catch (BeanCreationException ex) {  throw ex; } catch (Throwable ex) {  throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", ex); } return pvs;}
复制代码


存在 AOP 的情况 singletonObject = singletonFactory.getObject(); 多次调用会生成新的代理对象,(不同代理对象代理的还是同一个 bean 对象 serviceA)。spring 将 singletonObject 代理对象放入到二级缓存,注入到 serviceB 中的也是代理对象 singletonObject。


如果没有 AOP 的话确实可以两级缓存就可以解决循环依赖的问题,如果加上 AOP,两级缓存是无法解决的,不可能每次执行 singleFactory.getObject()方法都给我产生一个新的代理对象,所以还要借助另外一个缓存来保存产生的代理对象。

总结:

spring 中三级缓存存放 ObjectFactory(匿名类 调用了 AbstractAutowireCapableBeanFactory #getEarlyBeanReference 方法),通过 getObect 获取半成品对象放入二级缓存,完成属性注入及 bean 初始化后放入一级缓存。对存在 AOP 代理的情况,三级缓存每次会生成一个新的代理对象,因此将 getObject 生成的代理对象 singletonObject 放入到二级缓存,完成属性注入后,将代理对象添加到一级缓存。

用户头像

Rubble

关注

还未添加个人签名 2021.06.01 加入

还未添加个人简介

评论

发布
暂无评论
spring 循环依赖与三级缓存