Relying upon circular references is discouraged and they are prohibited by default. springboot启动报错

文章讲述了作者在项目中遇到SpringBoot2.6版本后关于循环依赖的报错,介绍了两种解决方法:一是设置`spring.main.allow-circular-references`为true,二是调整服务方法依赖,将其降级为DAO方式以消除循环。
摘要由CSDN通过智能技术生成

今天在做一个项目得时候,当时碰到这个错误得一脸懵逼,好像是第一次碰到这个问题,然后查找相关资料,才发现这个问题出在哪里了。

报错信息:

Description:

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

   attrController (field private com.muguang.product.service.AttrService com.muguang.product.controller.AttrController.attrService)
      ↓
   attrService (field private com.muguang.product.service.CategoryService com.muguang.product.service.impl.AttrServiceImpl.categoryService)
      ↓
   categoryService (field private com.muguang.product.service.CategoryBrandRelationService com.muguang.product.service.impl.CategoryServiceImpl.categoryBrandRelationService)
┌─────┐
|  categoryBrandRelationService (field private com.muguang.product.service.BrandService com.muguang.product.service.impl.CategoryBrandRelationServiceImpl.brandService)
↑     ↓
|  brandService (field private com.muguang.product.service.CategoryBrandRelationService com.muguang.product.service.impl.BrandServiceImpl.categoryBrandRelationService)
└─────┘

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.min.allow-circular-references设置为true来自动打破循环。

查看我自己得报错信息就是因为:这两个类相互依赖了
错误描述

开始解决问题 ,开搞

第一种方式

出现这个问题得原因是,在springboot2.6以后得版本升级,默认是不支持循环引用了,需要我们去配置文件中设置springboot.main来允许这样使用

在配置文件中添加一下依赖

spring.main.allow-circular-references=true

参数yml格式描述

第二种方式

他的报错信息既然是我两个service方法得项目依赖了,那我既可以把某个service得方法降级为dao
得方式来解决循环依赖
解决方式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值