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è)務,清理掉所有存在循環(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)文章
基于java下載中g(shù)etContentLength()一直為-1的一些思路
下面小編就為大家?guī)硪黄趈ava下載中g(shù)etContentLength()一直為-1的一些思路。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06Spring事件發(fā)布監(jiān)聽,順序監(jiān)聽,異步監(jiān)聽方式
這篇文章主要介紹了Spring事件發(fā)布監(jiān)聽,順序監(jiān)聽,異步監(jiān)聽方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12JSP頁面pageEncoding和contentType屬性
有關(guān)于JSP頁面中pageEncoding和contentType屬性。2013-04-04springboot控制層傳遞參數(shù)為非必填值的操作
這篇文章主要介紹了springboot控制層傳遞參數(shù)為非必填值的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10multi-catch和try-catch異常處理知識點詳解
在本篇文章里我們給大家分享了一篇關(guān)于multi-catch和try-catch異常處理知識點內(nèi)容,有需要的朋友們可以參考學習下。2019-11-11