您好,登录后才能下订单哦!
这篇文章运用简单易懂的例子给大家介绍spring如何解决循环依赖,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
概述
循环依赖就是依赖关系形成环,比如最简单的循环依赖:A对象依赖B,B对象依赖A
属性注入与循环依赖
spring属性注入处理循环依赖的方式
通过以下xml方式配置一个循环依赖的示例:
<bean id="person1" class="com.example.leetcode.spring.bean.Person"> <property name="parent" ref="person2"></property> <property name="name" value="tom"></property> </bean> <bean id="person2" class="com.example.leetcode.spring.bean.Person"> <property name="parent" ref="person1"></property> <property name="name" value="jack"></property> </bean>
spring循环依赖处理几个关键位置:
获取bean对象
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType, @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException { final String beanName = transformedBeanName(name); Object bean; // 这里会检查单例bean是否已经在注册表,并返回。 // Eagerly check singleton cache for manually registered singletons. Object sharedInstance = getSingleton(beanName); if (sharedInstance != null && args == null) { if (logger.isTraceEnabled()) { if (isSingletonCurrentlyInCreation(beanName)) { logger.trace("Returning eagerly cached instance of singleton bean '" + beanName + "' that is not fully initialized yet - a consequence of a circular reference"); } else { logger.trace("Returning cached instance of singleton bean '" + beanName + "'"); } } bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); } ... }
DefaultSingletonBeanRegistry(单例对象注册表)的几个关键属性。
// 用来存储已经创建好的单例对象 /** Cache of singleton objects: bean name to bean instance. */ private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256); // 用来存储单例beanname到ObjectFactory的映射 /** 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 HashMap<>(16);
DefaultSingletonBeanRegistry.getSingleton()的实现.
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; }
AbstractAutowireCapableBeanFactory.doCreateBean创建对象与注入属性
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args) throws BeanCreationException { ... instanceWrapper = createBeanInstance(beanName, mbd, args); ... // 检查是否提前将单例bean存入缓存 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"); } // 这里将beanname与工厂映射放入缓存注册表中(也就是上面的singletonFactories) addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean)); } ... // 注入依赖属性 populateBean(beanName, mbd, instanceWrapper); ... }
假设我们从beanfactory获取person1对象, 循环依赖处理流程如下:
1.通过AbstractBeanFactory.doGetBean("persion1")获取对象
2.因为一开始通过DefaultSingletonBeanRegistry.getSingleton()什么都没有,进入AbstractAutowireCapableBeanFactory.doCreateBean()进行创建
3.AutowireCapableBeanFactory.doCreateBean()里面执行完创建逻辑,因为是singleton将beanname与工厂的映射加入到addSingletonFactory()到缓存
4.开始处理person1对象的属性依赖populateBean()
5.当发现person1的parent属性是一个引用时,通过beanfactory.getBean("person2")获取依赖对象(org.springframework.beans.factory.support.BeanDefinitionValueResolver#resolveReference)
6.此时进入person2的创建流程, person2也没有缓存,开始实例化并加入到addSingletonFactory()到缓存
7.person2在通过populateBean()注入属性依赖发现依赖person1, 此时通过beanfactory.getBean("person1")获取依赖对象
8.此时AbstractBeanFactory.doGetBean("persion1")获取对象执行到getSingleton("person1")进行以下判断:
9.此时从getSingleton("person1")返回了一个仅实例化尚未注入的bean引用
10.person2在第7步获取到person1仅实例化未注入的对象引用。
11.person2完成属性注入并返回。
12.person2被addSingleton(beanName, singletonObject);中singletonObjects.put(beanName, singletonObject)缓存,并删除singletonFactories.remove(beanName);earlySingletonObjects.remove(beanName);
13.person1在5步获取到person2的对象并完成属性注入
14.person1对象返回(因为一开始person2获取的是person1的引用,此时person1完成注入是能看到注入后的对象)
15.person1被addSingleton(beanName, singletonObject);中singletonObjects.put(beanName, singletonObject)缓存,并删除singletonFactories.remove(beanName);earlySingletonObjects.remove(beanName);
16.返回最终的person1对象
关于三个map(三级缓存)
在出现循环依赖时,三个map之间的流程如下:
先从singletonFactories获取工厂,并通过getObject获取对象并移除缓存,将对象缓存到earlySingletonObjects
通过earlySingletonObjects获取提前曝光的对象
对象创建并初始化完成之后,对象信息保留在singletonObjects并移除过earlySingletonObjects中的缓存
earlySingletonObjects二级缓存是鸡肋吗?
earlySingletonObjects缓存的目的是,通过三级缓存在获取对象会执行一些列的后置处理器,通过earlySingletonObjects来缓存提升性能。
关于spring如何解决循环依赖就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。