springboot 使用已經非常廣泛了,它的版本迭代速度也比較快,過一段時間版本差異就會比較大。
由于低版本依賴的 spring 版本有漏洞問題,這次我們是從 2.2.6 版本直接升級到 2.7.16,升級 3.0 的話擔心差異更大。
這時直接修改依賴啟動就會報錯,主要是提示循環依賴的問題,這里就不貼代碼了,大致是這樣的錯誤
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.
如果仔細看可以發現有修改建議。這里再啰嗦一下,spring 一直不建議循環依賴,但是沒辦法還得兼容,這次 springboot2.6 這個版本直接給禁用了,你要使用要么去掉循環依賴,要么使用兼容配置。想必大家要在老項目去掉循環依賴都覺得頭大吧。那就這樣來搞:
通過在 application.properties 或 application.yaml 配置打開允許循環依賴
application.properties
spring.main.allow-circular-references=true
application.yaml
spring:main:allow-circular-references: true
如果你使用了 springcloud 時,在沒有以上的配置文件時,也可以在
resource/bootstrap.yaml 中增加配置:
spring:main:allow-circular-references: true
下面是參考資料
禁用循環依賴:https://www.springcloud.io/post/2022-02/spring-cyclic-dependencies/#gsc.tab=0
其他解決方案:https://medium.com/@karthik.jeyapal/circular-dependency-in-spring-boot-how-to-detect-and-fix-it-2a6e64bb488f
核心就是要對升級的內容做到心中有數,盲目升級就會出現各種搜索解決方案,所以建議大家多了解官方文檔,比如遷移建議、更新內容等