<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)依賴!

          共 2122字,需瀏覽 5分鐘

           ·

          2022-06-10 20:14

          往期熱門文章:

          1、BigDecimal使用不當,造成P0事故!

          2、Spring Boot 啟動時自動執(zhí)行代碼的幾種方式,還有誰不會??

          3、VS Code 殺死 IDEA?!

          4、服務(wù)器被惡意攻擊是什么體驗?

          5、麻了!Fastjson 再曝反序列化漏洞。。

          Spring的Bean管理,一直是整個體系中津津樂道的東西。尤其是Bean的循環(huán)依賴,更是很多面試官最喜歡考察的2B知識點之一。
          但事實上,項目中存在Bean的循環(huán)依賴,是代碼質(zhì)量低下的表現(xiàn)。多數(shù)人寄希望于框架層來給擦屁股,造成了整個代碼的設(shè)計越來越糟,最后用一些奇技淫巧來填補犯下的錯誤。
          還好,SpringBoot終于受不了這種濫用,默認把循環(huán)依賴給禁用了!
          從2.6版本開始,如果你的項目里還存在循環(huán)依賴,SpringBoot將拒絕啟動!

          驗證代碼小片段

          為了驗證這個功能,我們只需要兩段小代碼。
          CircularDependencyA.java
          @Component
          @RequiredArgsConstructor
          public?class?CircularDependencyA?{
          ????private?final?CircularDependencyB?circB;
          }
          CircularDependencyB.java
          @Component
          @RequiredArgsConstructor
          public?class?CircularDependencyB?{
          ????private?final?CircularDependencyA?circA;
          }
          RequiredArgsConstructor注解,是lombok包里面的,用來實現(xiàn)簡單的構(gòu)造器注入。不出所料,當我們啟動代碼的時候,報錯了~~
          報錯如下:
          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.
          當然,有些鳥人已經(jīng)玩大了,循環(huán)依賴到處都是,改代碼變的越來越不現(xiàn)實。那你還可以通過在yaml里配置參數(shù)來臨時開啟循環(huán)依賴。
          spring.main.allow-circular-references=true
          看來SpringBoot對惡勢力的容忍能力還是不夠堅決??!
          繞過SpringBoot這個攔截的方法不止一種,比如使用@Lazy注解進行延遲初始化。但這些都是治標不治本,辜負了SpringBoot的一片苦心。

          做對的事

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

          /**?Whether?to?automatically?try?to?resolve?circular?references?between?beans.?*/
          private?boolean?allowCircularReferences?=?true;
          很長一段時間,SpringBoot這個值都是默認為true的。但這種縱容造成了大批低質(zhì)量的代碼產(chǎn)生,以至于新員工一直在給老員工擦屁股。
          把這個值默認設(shè)置為false,是堅持做對的事情。起碼,在工程師編寫出質(zhì)量不高的代碼時,能夠知道他自己在做什么,而不是把隱患一步步的推遲,任代碼腐敗。
          不得不為SpringBoot點個贊。真棒!

          往期熱門文章:

          1、MySQL 暴跌!

          2、超越 Xshell!號稱下一代 Terminal 終端神器,用完愛不釋手!

          3、IDEA 官宣全新默認 UI,太震撼了?。?/span>

          4、讓你直呼「臥槽」的 GitHub 項目!

          5、Kafka又笨又重,為啥不選Redis?

          6、50多個高頻免費 API 接口分享

          7、IDEA公司再發(fā)新神器!超越 VS Code 騷操作!

          8、我懷疑這是 IDEA 的 BUG,但是我翻遍全網(wǎng)沒找到證據(jù)!

          9、Spring MVC 中的 Controller 是線程安全的嗎?

          10、Gitee 倒下了???

          瀏覽 49
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <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>
                  影音先锋成人影视 | 亚欧洲精品视频 | 人妻少妇91精品一区黑人 | 亚洲一逼 | 欧美,三级黄 |