<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          痛快!SpringBoot終于禁掉了循環(huán)依賴!

          共 1935字,需瀏覽 4分鐘

           ·

          2022-06-12 11:11

          點(diǎn)擊關(guān)注公眾號,Java干貨及時(shí)送達(dá)

          Spring的Bean管理,一直是整個(gè)體系中津津樂道的東西。尤其是Bean的循環(huán)依賴,更是很多面試官最喜歡考察的2B知識點(diǎn)之一。

          但事實(shí)上,項(xiàng)目中存在Bean的循環(huán)依賴,是代碼質(zhì)量低下的表現(xiàn)。多數(shù)人寄希望于框架層來給擦屁股,造成了整個(gè)代碼的設(shè)計(jì)越來越糟,最后用一些奇技淫巧來填補(bǔ)犯下的錯誤。

          還好,SpringBoot終于受不了這種濫用,默認(rèn)把循環(huán)依賴給禁用了!

          從2.6版本開始,如果你的項(xiàng)目里還存在循環(huán)依賴,SpringBoot將拒絕啟動!

          驗(yàn)證代碼小片段

          為了驗(yàn)證這個(gè)功能,我們只需要兩段小代碼。

          CircularDependencyA.java

          @Component
          @RequiredArgsConstructor
          public?class?CircularDependencyA?{
          ????private?final?CircularDependencyB?circB;
          }

          CircularDependencyB.java

          @Component
          @RequiredArgsConstructor
          public?class?CircularDependencyB?{
          ????private?final?CircularDependencyA?circA;
          }

          RequiredArgsConstructor注解,是lombok包里面的,用來實(shí)現(xiàn)簡單的構(gòu)造器注入。不出所料,當(dāng)我們啟動代碼的時(shí)候,報(bào)錯了~~

          報(bào)錯如下:

          The?dependencies?of?some?of?the?beans?in?the?application?context?form?a?cycle:
          ┌─────┐
          |??circularDependencyA?defined?in?file?[cir/CircularDependencyA.class]
          ↑?????↓
          |??circularDependencyB?defined?in?file?[cir/CircularDependencyB.class]
          └─────┘

          Action:

          Relying?upon?circular?references?is?discouraged?and?they?are?prohibited?by?default.?Update?your?application?to?remove?the?dependency?cycle?between?beans.?As?a?last?resort,?it?may?be?possible?to?break?the?cycle?automatically?by?setting?spring.main.allow-circular-references?to?true.

          當(dāng)然,有些鳥人已經(jīng)玩大了,循環(huán)依賴到處都是,改代碼變的越來越不現(xiàn)實(shí)。那你還可以通過在yaml里配置參數(shù)來臨時(shí)開啟循環(huán)依賴。

          spring.main.allow-circular-references=true

          看來SpringBoot對惡勢力的容忍能力還是不夠堅(jiān)決??!

          繞過SpringBoot這個(gè)攔截的方法不止一種,比如使用@Lazy注解進(jìn)行延遲初始化。但這些都是治標(biāo)不治本,辜負(fù)了SpringBoot的一片苦心。

          做對的事

          其實(shí),我們一直把代碼往下找下去,會發(fā)現(xiàn)這個(gè)開關(guān),其實(shí)是Spring的功能。

          AbstractAutowireCapableBeanFactory#allowCircularReferences

          /**?Whether?to?automatically?try?to?resolve?circular?references?between?beans.?*/
          private?boolean?allowCircularReferences?=?true;

          很長一段時(shí)間,SpringBoot這個(gè)值都是默認(rèn)為true的。但這種縱容造成了大批低質(zhì)量的代碼產(chǎn)生,以至于新員工一直在給老員工擦屁股。

          把這個(gè)值默認(rèn)設(shè)置為false,是堅(jiān)持做對的事情。起碼,在工程師編寫出質(zhì)量不高的代碼時(shí),能夠知道他自己在做什么,而不是把隱患一步步的推遲,任代碼腐敗。

          不得不為SpringBoot點(diǎn)個(gè)贊。真棒!

          ????

          1、拖動文件就能觸發(fā)7-Zip安全漏洞,波及所有版本

          2、進(jìn)程切換的本質(zhì)是什么?

          3、一次 SQL 查詢優(yōu)化原理分析:900W+ 數(shù)據(jù),從 17s 到 300ms

          4、Redis數(shù)據(jù)結(jié)構(gòu)為什么既省內(nèi)存又高效?

          5、IntelliJ IDEA快捷鍵大全 + 動圖演示

          6、全球第三瀏覽器,封殺中國用戶這種操作?。ㄎ哪┧蜁?/a>

          點(diǎn)

          點(diǎn)

          點(diǎn)點(diǎn)

          點(diǎn)

          瀏覽 67
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  草逼小视频| 日本黄色电影网扯 | 天天干天天爽视频 | 在线免费看AV | 在线男人天堂 |