今天在做一个项目得时候,当时碰到这个错误得一脸懵逼,好像是第一次碰到这个问题,然后查找相关资料,才发现这个问题出在哪里了。
报错信息:
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
第二种方式
他的报错信息既然是我两个service方法得项目依赖了,那我既可以把某个service得方法降级为dao
得方式来解决循环依赖