背景
問題從spring boot 2.3.12升級(jí)到2.6.15版本后,項(xiàng)目啟動(dòng)后訪問報(bào)錯(cuò).
The dependencies of some of the beans in the application context form a cycle.
serviceCollectionIdCacheService
┌─────┐
| serviceProductInfoProviderImpl
↑ ↓
| serviceOfflineProviderImpl
↑ ↓
| serviceProductMappingProviderImpl
└─────┘
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.
原因
在2.6.0之前,spring會(huì)自動(dòng)處理循環(huán)依賴的問題,2.6.0 以后的版本默認(rèn)禁止 Bean 之間的循環(huán)引用,如果存在循環(huán)引用就會(huì)啟動(dòng)失敗報(bào)錯(cuò)。
解決
方案1
清理循環(huán)引用的Bean
1、在字段上使用@Autowired注解,讓Spring決定在合適的時(shí)機(jī)注入。
2、在@Autowired注解上方加上@Lazy注解(延遲加載)
(A—>B—>C—>D 一般在D引用A的@Autowired下加入@Lazy注解即可)
方案2
it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
也可以暫時(shí)跳過,在yml配置中加入
spring:
main:
allow-circular-references: true
方案3
在啟動(dòng)中加入也一樣文章來源:http://www.zghlxwxcb.cn/news/detail-608820.html
public static void main(String[] args) {
SpringApplication sa = new SpringApplication(xx.class);
sa.setAllowCircularReferences(Boolean.TRUE);//加入的參數(shù)
sa.run(args);
}
本人采用了方案3來解決問題。文章來源地址http://www.zghlxwxcb.cn/news/detail-608820.html
到了這里,關(guān)于springboot升級(jí)出現(xiàn)循環(huán)依賴問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!