Spring?Boot2.6.0新特性之默認禁止循環(huán)引用
前言
如下代碼,ComponentA類注入ComponentB類,ComponentB類注入ComponentA類,就會發(fā)生循環(huán)依賴的問題,在2.6.0之前,spring會自動處理循環(huán)依賴的問題
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class ComponentA {
@Resource
private ComponentB componentB;
}import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class ComponentB {
@Resource
private ComponentA componentA;
}現(xiàn)在,2.6.0 這個版本已經(jīng)默認禁止 Bean 之間的循環(huán)引用,如果存在循環(huán)引用就會啟動失敗報錯:
***************************
APPLICATION FAILED TO START
***************************Description:
The dependencies of some of the beans in the application context form a cycle:
┌─────┐
| componentA
↑ ↓
| componentB
└─────┘
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.
處理方案一:
整改業(yè)務(wù),清理掉所有存在循環(huán)引用的 Bean
處理方案二:
spring:
main:
allow-circular-references: true
處理方案三:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringDemoApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(SpringDemoApplication.class);
application.setAllowCircularReferences(Boolean.TRUE);
application.run(args);
}
}總結(jié)
到此這篇關(guān)于Spring Boot2.6.0新特性之默認禁止循環(huán)引用的文章就介紹到這了,更多相關(guān)SpringBoot2.6.0默認禁止循環(huán)引用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Tomcat使用IDEA遠程Debug調(diào)試的講解
今天小編就為大家分享一篇關(guān)于Tomcat使用IDEA遠程Debug調(diào)試的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
springboot application無法使用$獲取pom變量的問題及解決
這篇文章主要介紹了springboot application無法使用$獲取pom變量的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
MybatisPlus調(diào)用原生SQL的三種方法實例詳解
這篇文章主要介紹了MybatisPlus調(diào)用原生SQL的三種方法,在有些情況下需要用到MybatisPlus查詢原生SQL,MybatisPlus其實帶有運行原生SQL的方法,我這里列舉三種,需要的朋友可以參考下2022-09-09
Java面向?qū)ο蠡A(chǔ)知識之封裝,繼承,多態(tài)和抽象
這篇文章主要介紹了Java面向?qū)ο蟮姆庋b,繼承,多態(tài)和抽象,文中有非常詳細的代碼示例,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很好的幫助,需要的朋友可以參考下2021-11-11

