圖解 Spring 循環(huán)依賴(lài),寫(xiě)得太好了!
關(guān)注我們,設(shè)為星標(biāo),每天7:30不見(jiàn)不散,架構(gòu)路上與您共享? 回復(fù)"架構(gòu)師"獲取資源
大家好,我是架構(gòu)君,一個(gè)會(huì)寫(xiě)代碼吟詩(shī)的架構(gòu)師。
前言
正文


if?(isPrototypeCurrentlyInCreation(beanName))?{
??throw?new?BeanCurrentlyInCreationException(beanName);
}

Spring解決循環(huán)依賴(lài)
singletonObjects 它是我們最熟悉的朋友,俗稱(chēng)“單例池”“容器”,緩存創(chuàng)建完成單例Bean的地方。 singletonFactories 映射創(chuàng)建Bean的原始工廠 earlySingletonObjects 映射Bean的早期引用,也就是說(shuō)在這個(gè)Map里的Bean不是完整的,甚至還不能稱(chēng)之為“Bean”,只是一個(gè)Instance.

循環(huán)依賴(lài)的本質(zhì)
將指定的一些類(lèi)實(shí)例為單例 類(lèi)中的字段也都實(shí)例為單例 支持循環(huán)依賴(lài)
public?class?A?{
????private?B?b;
}
//?類(lèi)B:
public?class?B?{
????private?A?a;
}
?/**
?????*?放置創(chuàng)建好的bean?Map
?????*/
????private?static?Map?cacheMap?=?new?HashMap<>(2);
????public?static?void?main(String[]?args)?{
????????//?假裝掃描出來(lái)的對(duì)象
????????Class[]?classes?=?{A.class,?B.class};
????????//?假裝項(xiàng)目初始化實(shí)例化所有bean
????????for?(Class?aClass?:?classes)?{
????????????getBean(aClass);
????????}
????????//?check
????????System.out.println(getBean(B.class).getA()?==?getBean(A.class));
????????System.out.println(getBean(A.class).getB()?==?getBean(B.class));
????}
????@SneakyThrows
????private?static??T?getBean(Class?beanClass) ?{
????????//?本文用類(lèi)名小寫(xiě)?簡(jiǎn)單代替bean的命名規(guī)則
????????String?beanName?=?beanClass.getSimpleName().toLowerCase();
????????//?如果已經(jīng)是一個(gè)bean,則直接返回
????????if?(cacheMap.containsKey(beanName))?{
????????????return?(T)?cacheMap.get(beanName);
????????}
????????//?將對(duì)象本身實(shí)例化
????????Object?object?=?beanClass.getDeclaredConstructor().newInstance();
????????//?放入緩存
????????cacheMap.put(beanName,?object);
????????//?把所有字段當(dāng)成需要注入的bean,創(chuàng)建并注入到當(dāng)前bean中
????????Field[]?fields?=?object.getClass().getDeclaredFields();
????????for?(Field?field?:?fields)?{
????????????field.setAccessible(true);
????????????//?獲取需要注入字段的class
????????????Class>?fieldClass?=?field.getType();
????????????String?fieldBeanName?=?fieldClass.getSimpleName().toLowerCase();
????????????//?如果需要注入的bean,已經(jīng)在緩存Map中,那么把緩存Map中的值注入到該field即可
????????????//?如果緩存沒(méi)有?繼續(xù)創(chuàng)建
????????????field.set(object,?cacheMap.containsKey(fieldBeanName)
??????????????????????cacheMap.get(fieldBeanName)?:?getBean(fieldClass));
????????}
????????//?屬性填充完成,返回
????????return?(T)?object;
????}

what?問(wèn)題的本質(zhì)居然是two sum!
class?Solution?{
????public?int[]?twoSum(int[]?nums,?int?target)?{
????????Map?map?=?new?HashMap<>();
????????for?(int?i?=?0;?i?????????????int?complement?=?target?-?nums[i];
????????????if?(map.containsKey(complement))?{
????????????????return?new?int[]?{?map.get(complement),?i?};
????????????}
????????????map.put(nums[i],?i);
????????}
????????throw?new?IllegalArgumentException("No?two?sum?solution");
????}
}
結(jié)尾
end
end
文章來(lái)源:https://juejin.im/post/5e927e27f265da47c8012ed9
這些年小編給你分享過(guò)的干貨
1.優(yōu)質(zhì)SpringBoot物流管理項(xiàng)目(附源碼)
2.優(yōu)質(zhì)ERP系統(tǒng)帶進(jìn)銷(xiāo)存財(cái)務(wù)生產(chǎn)功能(附源碼)
3.優(yōu)質(zhì)SpringBoot帶工作流管理項(xiàng)目(附源碼)
4.最好用的OA系統(tǒng),拿來(lái)即用(附源碼)
5.SBoot+Vue外賣(mài)系統(tǒng)前后端都有(附源碼)
6.SBoot+Vue可視化大屏拖拽項(xiàng)目(附源碼)

轉(zhuǎn)發(fā)在看就是最大的支持??
評(píng)論
圖片
表情
