栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

The dependencies of some of the beans in the application context form a cycle,SpringBoot中循环依赖如何解决。

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

The dependencies of some of the beans in the application context form a cycle,SpringBoot中循环依赖如何解决。

这句话的意思是:应用程序上下文中某些bean的依赖关系形成了一个循环

有时候在写项目时会忘记类的依赖关系,很容易导致两个对象互相注入,形成了一个循环,但是官方是不鼓励依赖循环,默认情况下禁止它们。

解决办法1
Update your application to remove the dependency cycle between beans

更新应用程序以删除bean之间的依赖关系循环。一般控制台会打出来告诉你是哪里的类导致的互相依赖。比如以下日志:

The dependencies of some of the beans in the application context form a cycle:

┌─────┐
|  springLoopService1 (field com.jiefei.jdbc.service.impl.SpringLoopService2 com.jiefei.jdbc.service.impl.SpringLoopService1.springLoopService2)
↑     ↓
|  springLoopService2 (field com.jiefei.jdbc.service.impl.SpringLoopService1 com.jiefei.jdbc.service.impl.SpringLoopService2.springLoopService1)
└─────┘

直接根据自己需要,让他们不要互相依赖就行了。

解决办法2

在application.yml 中使用以下配置,好像是SpringBoot2.6.0之后默认禁止的。

spring:
    main:
        allow-circular-references: true  #允许循环引用
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/872633.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号