SpringBoot2.6.x默認(rèn)禁用循環(huán)依賴后的應(yīng)對策略

一、序言

SpringBoot 2.6.x不推薦使用循環(huán)依賴,這是一個好消息,SpringBoot從底層逐漸引導(dǎo)開發(fā)者書寫規(guī)范的代碼,同時也是個憂傷的消息,循環(huán)依賴的應(yīng)用場景實在是太廣泛了。

如果從低版本升級到2.6.x,那么很大概率遇到的第一個問題便是循環(huán)依賴問題。

二、問題復(fù)原

1、代碼說明

下面風(fēng)格的代碼比較普遍:兩個類都有調(diào)用對方方法的需求,因此很容易寫成循環(huán)引用。

@Service
public class TbDeptServiceImpl extends ServiceImpl<TbDeptMapper, TbDept> implements ITbDeptService {
    
    @Autowired
    private ITbStaffService staffService;
}
@Service
public class TbStaffServiceImpl extends ServiceImpl<TbStaffMapper, TbStaff> implements ITbStaffService {
    @Autowired
    private ITbDeptService deptService;
}
2、錯誤示例
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.
Despite circular references being allowed, the dependency cycle between beans could not be broken. Update your application to remove the dependency cycle.

三、問題解決

1、粗暴解決

最簡單的方式是在全局配置文件中允許循環(huán)引用存在,此屬性默認(rèn)值為false,顯示聲明為true,可回避項目啟動時控制臺循環(huán)引用異常。

spring:
  main:
    allow-circular-references: true
2、優(yōu)雅解決

Spring官方默認(rèn)禁止使用循環(huán)依賴,盡管留有可選配置,允許開發(fā)者繼續(xù)使用循環(huán)依賴。

Spring官方的初心是不希望開發(fā)者編寫循環(huán)依賴的代碼,也就是說未來的某個版本可能強制不得使用循環(huán)依賴,因此逐漸在新項目中消除循環(huán)依賴是不得不面對的問題。

使用方法的返回值獲取實例對象,替換通過成員變量注入實例對象。

@Service
public class TbDeptServiceImpl extends ServiceImpl<TbDeptMapper, TbDept> implements ITbDeptService {
    /**
     * 使用方法返回實例對象,替換成員變量注入
     * @return ITbStaffService
     */
    public ITbStaffService getStaffService(){
        return SpringUtils.getBean(ITbStaffService.class);
    }
}
@Service
public class TbStaffServiceImpl extends ServiceImpl<TbStaffMapper, TbStaff> implements ITbStaffService {
    /**
     * 使用方法返回實例對象,替換成員變量注入
     * @return ITbStaffService
     */
    public ITbDeptService getDeptService(){
        return SpringUtils.getBean(ITbDeptService.class);
    }
}

其中需要使用如下依賴,此依賴是筆者抽離出來的公共依賴,可跨項目使用。

<dependency>
    <groupId>xin.altitude.cms.common</groupId>
    <artifactId>ucode-cms-common</artifactId>
    <version>1.3.4</version>
</dependency>

如果找不到此依賴,很大可能是阿里云Maven倉庫尚未同步,在項目中強制使用Maven中央倉庫即可。

<repositories>
    <repository>
        <id>public</id>
        <name>maven nexus</name>
        <url>https://repo1.maven.org/maven2/</url>
        <snapshots>
            <updatePolicy>always</updatePolicy>
        </snapshots>
    </repository>
</repositories>

四、小結(jié)

Spring生態(tài)作為廣泛使用的框架,儼然成為Java企業(yè)級應(yīng)用主流標(biāo)準(zhǔn),其微小的變化對整合生態(tài)帶來不可估量的影響。從跟隨者轉(zhuǎn)化為引導(dǎo)者,果斷禁止循環(huán)依賴問題,體現(xiàn)的是作為引導(dǎo)者的擔(dān)當(dāng)。

循環(huán)引用使用習(xí)慣了,初步看起來代碼沒毛病,仔細(xì)想想是不合理的設(shè)計。循環(huán)依賴的直接表現(xiàn)是你中有我,我中有你,從對象的設(shè)計上令人費解。

最為開發(fā)者時刻關(guān)注底層框架的變動,將會在應(yīng)用層收益。這里所說的底層框架是指JDK、Spring生態(tài)、Apache、知名大廠開源并廣泛被應(yīng)用的框架,比如guava等。

喜歡本文點個??贊??支持一下,關(guān)注我下期再見,相關(guān)源碼在GitHub,視頻講解在B站,本文收藏在專題博客

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容