關(guān)于SpringBoot禁止循環(huán)依賴解說
前言:
Spring的Bean管理,一直是整個(gè)體系中津津樂道的東西。尤其是Bean的循環(huán)依賴,更是很多面試官最喜歡考察的2B知識(shí)點(diǎn)之一。
但事實(shí)上,項(xiàng)目中存在Bean的循環(huán)依賴,是代碼質(zhì)量低下的表現(xiàn)。多數(shù)人寄希望于框架層來給擦屁股,造成了整個(gè)代碼的設(shè)計(jì)越來越糟,最后用一些奇技淫巧來填補(bǔ)犯下的錯(cuò)誤。
還好,SpringBoot終于受不了這種濫用,默認(rèn)把循環(huán)依賴給禁用了!
從2.6版本開始,如果你的項(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包里面的,用來實(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)然,有些鳥人已經(jīng)玩大了,循環(huán)依賴到處都是,改代碼變的越來越不現(xiàn)實(shí)。那你還可以通過在yaml里配置參數(shù)來臨時(shí)開啟循環(huán)依賴。
spring.main.allow-circular-references=true
看來SpringBoot對(duì)惡勢(shì)力的容忍能力還是不夠堅(jiān)決??!
繞過SpringBoot這個(gè)攔截的方法不止一種,比如使用@Lazy注解進(jìn)行延遲初始化。但這些都是治標(biāo)不治本,辜負(fù)了SpringBoot的一片苦心。
做對(duì)的事:
其實(shí),我們一直把代碼往下找下去,會(huì)發(fā)現(xiàn)這個(gè)開關(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ì)的事情。起碼,在工程師編寫出質(zhì)量不高的代碼時(shí),能夠知道他自己在做什么,而不是把隱患一步步的推遲,任代碼腐敗。
不得不為SpringBoot點(diǎn)個(gè)贊。真棒!
到此這篇關(guān)于關(guān)于SpringBoot禁止循環(huán)依賴解說的文章就介紹到這了,更多相關(guān)SpringBoot循環(huán)依賴內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實(shí)現(xiàn)基于URL和IP的訪問頻率限制
在現(xiàn)代?Web?應(yīng)用中,接口被惡意刷新或暴力請(qǐng)求是一種常見的攻擊手段,為了保護(hù)系統(tǒng)資源,需要對(duì)接口的訪問頻率進(jìn)行限制,下面我們就來看看如何使用?Spring?Boot?實(shí)現(xiàn)基于?URL?和?IP?的訪問頻率限制吧2025-01-01
使用Springboot整合GridFS實(shí)現(xiàn)文件操作
這篇文章主要介紹了使用Springboot整合GridFS實(shí)現(xiàn)文件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
springboot中rabbitmq實(shí)現(xiàn)消息可靠性機(jī)制詳解
這篇文章主要介紹了springboot中rabbitmq實(shí)現(xiàn)消息可靠性機(jī)制詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
springboot項(xiàng)目或其他項(xiàng)目使用@Test測(cè)試項(xiàng)目接口配置
這篇文章主要介紹了springboot項(xiàng)目或其他項(xiàng)目使用@Test測(cè)試項(xiàng)目接口配置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07

