您好,登錄后才能下訂單哦!
本篇內容介紹了“Spring Boot2.6.0默認禁止循環怎么引用”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
如下代碼,ComponentA類注入ComponentB類,ComponentB類注入ComponentA類,就會發生循環依賴的問題,在2.6.0之前,spring會自動處理循環依賴的問題
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; }
現在,2.6.0 這個版本已經默認禁止 Bean 之間的循環引用,如果存在循環引用就會啟動失敗報錯:
***************************
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.
整改業務,清理掉所有存在循環引用的 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); } }
“Spring Boot2.6.0默認禁止循環怎么引用”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。