首页 >> 宠物资讯
宠物资讯

Spring源码:三级堆栈

发布时间:2024-01-07 12:18

wEarlyReference) { Object singletonObject = this.singletonObjects.get(beanName); if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) { singletonObject = this.earlySingletonObjects.get(beanName); 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) { // 三级CPU通过getObject()分析方法借助半成品bean singletonObject = singletonFactory.getObject(); // 存放二级CPU this.earlySingletonObjects.put(beanName, singletonObject); this.singletonFactories.remove(beanName); } } } } } } return singletonObject;}镜像预定义singletonFactories:三级CPU,在bean举例来说转化再继续来最后,类型可用以及初始转化之前,如果允许原定爆出,spring时会将举例来说转化后的bean原定爆出,也就是把该bean转换成beanFactory并转到到三级CPU。在只能提到原定爆出并不一定时再继续通过singletonFactory.getObject()借助。// 在举例来说转化bean最后,如果允许反向依赖于,将没创建者再继续来的bean转到到三级CPUif (earlySingletonExposure) { if (logger.isTraceEnabled()) { logger.trace("Eagerly caching bean '" + beanName + "' to allow for resolving potential circular references"); } // 拿取三级CPU addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));}protected void addSingletonFactory(String beanName, ObjectFactory singletonFactory) { Assert.notNull(singletonFactory, "Singleton factory must not be null"); synchronized (this.singletonObjects) { if (!this.singletonObjects.containsKey(beanName)) { // 存放三级CPU this.singletonFactories.put(beanName, singletonFactory); this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } }}镜像预定义三级CPU的用到

当A和B相互依存于时,若先创建者举例来说A,则整个命令行流程为:

其中都关乎CPU和依赖于相关主要流程为:

开始创建者举例来说A,标明A为打算创建者protected void beforeSingletonCreation(String beanName) { // 将举例来说A转到到singletonsCurrentlyInCreation中都 if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.add(beanName)) { throw new BeanCurrentlyInCreationException(beanName); }}镜像预定义举例来说转化A后,将没字符串和初始转化的举例来说A转到到三级CPUsingletonFactories中都addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));protected void addSingletonFactory(String beanName, ObjectFactory singletonFactory) { Assert.notNull(singletonFactory, "Singleton factory must not be null"); synchronized (this.singletonObjects) { if (!this.singletonObjects.containsKey(beanName)) { // 加到三级CPU this.singletonFactories.put(beanName, singletonFactory); // 二级CPU更正该bean this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } }}镜像预定义由于反向依赖于,举例来说A一连串举例来说B复制到,标明B为打算创建者,并在举例来说转化B后,将没字符串和初始转化的举例来说B转到到三级CPUsingletonFactories中都(同上)由于反向依赖于,举例来说B一连串举例来说A复制到,在CPU中都借助到没创建者好的Aprotected Object getSingleton(String beanName, boolean allowEarlyReference) { // 一级CPU中都不普遍存在A,返国null Object singletonObject = this.singletonObjects.get(beanName); // A因为早已标明为打算创建者,进入一个大的if语句 if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) { // 二级CPU不普遍存在A,返国null singletonObject = this.earlySingletonObjects.get(beanName); // 允许反向依赖于 if (singletonObject == null && allowEarlyReference) { // 锁定一级CPU和二级CPU,并最后化验 synchronized (this.singletonObjects) { singletonObject = this.singletonObjects.get(beanName); if (singletonObject == null) { singletonObject = this.earlySingletonObjects.get(beanName); if (singletonObject == null) { // 三级CPU中都普遍存在没创建者好的举例来说A,借助到ObjectFactory并不一定 ObjectFactory singletonFactory = this.singletonFactories.get(beanName); if (singletonFactory != null) { // 命令行getObject()分析方法,借助到为创建者好的举例来说A singletonObject = singletonFactory.getObject(); // 将举例来说A加到到二级CPU中都 this.earlySingletonObjects.put(beanName, singletonObject); // 三级CPU中都更正举例来说A this.singletonFactories.remove(beanName); } } } } } } return singletonObject;}镜像预定义在B创建者好最后,则标明举例来说B创建者顺利再继续来成,并将B转到到一级CPU中都protected void afterSingletonCreation(String beanName) { // 将举例来说B从singletonsCurrentlyInCreation中都更正 if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.remove(beanName)) { throw new IllegalStateException("Singleton '" + beanName + "' isn't currently in creation"); }}protected void addSingleton(String beanName, Object singletonObject) { synchronized (this.singletonObjects) { // 将举例来说B加到一级CPU this.singletonObjects.put(beanName, singletonObject); // 三级CPU中都更正B this.singletonFactories.remove(beanName); // 二级CPU中都更正B this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); }}镜像预定义在A创建者好最后,则标明举例来说A创建者顺利再继续来成,并将A转到到一级CPU中都AOP和三级CPU不关乎AOP

举例来说转到到三级CPU,并从三级CPU中都借助举例来说流程如下:

// 附加到三级CPUaddSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));// 用到三级CPUObjectFactory singletonFactory = this.singletonFactories.get(beanName);if (singletonFactory != null) { singletonObject = singletonFactory.getObject(); this.earlySingletonObjects.put(beanName, singletonObject); this.singletonFactories.remove(beanName);}镜像预定义

从三级CPU借助到的bean,是经过getEarlyBeanReference()作用最后返国的bean,在不关乎AOP时,很难类构成充分利用getEarlyBeanReference()分析方法,用到getEarlyBeanReference()的默认充分利用,因此返国的bean就是举例来说转化后的早期bean。

default Object getEarlyBeanReference(Object bean, String beanName) throws BeansException { return bean;}镜像预定义关乎AOP

当关乎到AOP时,AbstractAutoProxyCreator充分利用了getEarlyBeanReference()分析方法,返国的bean是经过AOP指派的bean。

public Object getEarlyBeanReference(Object bean, String beanName) { Object cacheKey = getCacheKey(bean.getClass(), beanName); this.earlyProxyReferences.put(cacheKey, bean); // 创建者AOP指派 return wrapIfNecessary(bean, beanName, cacheKey);}镜像预定义

不关乎反向依赖于时,bean的AOP指派是在举例来说转化,字符串,初始转化最后创建者的。而关乎反向依赖于时,三级CPU中都保存的是举例来说转化但没字符串,初始转化最后的bean,当通过三级CPU借助举例来说时,则时会原定创建者AOP指派:

非反向依赖于,在举例来说转化,字符串,初始转化最后创建者AOP指派反向依赖于,在举例来说转化,没字符串,初始转化最后创建者AOP指派(原定创建者指派)

对于在CPU中都原定创建者好指派的举例来说,当字符串,初始转化最后,运行至bean的四轮驱动处理事件逻辑,即AbstractAutoProxyCreator的postProcessAfterInitialization分析方法(正常创建者AOP指派逻辑)时,则不时会最后创建者AOP指派,返国的是可不指派的bean。

// AbstractAutoProxyCreator的四轮驱动处理事件逻辑public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) { if (bean != null) { // 由于原定创建者了指派并且有CPU,则不再继续返国指派 Object cacheKey = getCacheKey(bean.getClass(), beanName); if (this.earlyProxyReferences.remove(cacheKey) != bean) { return wrapIfNecessary(bean, beanName, cacheKey); } } return bean;}镜像预定义

再继续返国字符串和初始转化bean的逻辑中都,exposedObject作为再继续度的返国结果,在用到到AOP时,再继续度返国的都是指派bean

// 早期beanObject exposedObject = bean;try { // 举例来说字符串 populateBean(beanName, mbd, instanceWrapper); // 举例来说初始转化(初始转化后命令行bean的四轮驱动处理事件逻辑,即创建者AOP指派) // (1)非反向依赖于,没原定创建者指派,initializeBean返国的是指派bean // (2)反向依赖于,原定创建者指派,initializeBean返国的是早期bean exposedObject = initializeBean(beanName, exposedObject, mbd);}if (earlySingletonExposure) { // 当allowEarlyReference=false时,只能从一级二级CPU中都借助举例来说 // (1)没原定创建者指派时,举例来说在三级CPU中都,getSingleton返国指派null // (2)原定创建者指派时,举例来说在二级CPU中都,返国指派bean Object earlySingletonReference = getSingleton(beanName, false); if (earlySingletonReference != null) { // 原定创建者指派,指派bean在CPU中都,所以一个大相等 if (exposedObject == bean) { // 将指派bean字符串给exposedObject exposedObject = earlySingletonReference; } // ... }}

作者:重新做人链接:

佐米曲普坦片有效果吗
有哪些感冒可以吃阿莫西林
眼睛干涩视力模糊怎么解决
新冠特效药有哪些
阳了以后出现黄痰能用阿莫西林治疗吗
友情链接