偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

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

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

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

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

驗(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包里面的,用來(lái)實(shí)現(xiàn)簡(jiǎn)單的構(gòu)造器注入。不出所料,當(dāng)我們啟動(dòng)代碼的時(shí)候,報(bào)錯(cuò)了~~

報(bào)錯(cuò)如下:

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)然,有些鳥(niǎo)人已經(jīng)玩大了,循環(huán)依賴到處都是,改代碼變的越來(lái)越不現(xiàn)實(shí)。那你還可以通過(guò)在yaml里配置參數(shù)來(lái)臨時(shí)開(kāi)啟循環(huán)依賴。

spring.main.allow-circular-references=true

看來(lái)SpringBoot對(duì)惡勢(shì)力的容忍能力還是不夠堅(jiān)決啊!

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

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

AbstractAutowireCapableBeanFactory#allowCircularReferences

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

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

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

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

作者簡(jiǎn)介:小姐姐味道 (xjjdog),一個(gè)不允許程序員走彎路的公眾號(hào)。聚焦基礎(chǔ)架構(gòu)和Linux。十年架構(gòu),日百億流量,與你探討高并發(fā)世界,給你不一樣的味道。

責(zé)任編輯:武曉燕 來(lái)源: 小姐姐味道
相關(guān)推薦

2019-09-09 06:30:06

Springboot程序員開(kāi)發(fā)

2020-04-09 08:29:50

編程語(yǔ)言事件驅(qū)動(dòng)

2021-09-06 08:13:35

APM系統(tǒng)監(jiān)控

2014-12-01 11:20:28

Win8.1微軟

2020-04-02 14:07:30

微信QQ轉(zhuǎn)賬

2023-11-02 08:43:08

protocgo兼容

2023-05-04 08:06:27

Spring循環(huán)依賴

2012-02-03 09:25:39

Node.js

2025-03-17 00:21:00

2021-12-13 08:15:01

SpringSpring Boot開(kāi)發(fā)

2021-09-01 11:45:10

Spring循環(huán)依賴面試

2024-06-05 11:43:10

2022-01-26 10:29:24

微服務(wù)循環(huán)依賴代碼

2021-05-06 07:58:57

Spring BeanIOCAOP

2021-04-29 10:18:18

循環(huán)依賴數(shù)組

2022-05-20 15:15:56

開(kāi)發(fā)編程

2021-11-04 10:45:46

SpringBootExcelJava

2012-11-28 10:54:37

禁止追蹤DNT

2020-11-12 10:38:11

Windows 功能系統(tǒng)

2021-10-21 08:31:31

Spring循環(huán)依賴面試
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)