关于SpringBoot中出现的循环依赖问题
- 环境:
SpringBoot2.7.8
- 背景:
在增加出库订单时需要对物品表的的数量进行修改
因此我在OutboundController中创建了几个公共方法,并将其注入到Spring中,结果给我报了这一串错误。
Description:
The dependencies of some of the beans in the application context form a cycle:
┌──->──┐
| getGoodsNumber defined in class path resource [com/QRproject/project/system/controller/GoodsController.class]
└──<-──┘
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.- 代码:
- 在OutboundController中创建了个公共方法,并将其注入到Spring中:(以下方法均未开启事务支持)
1 @RestController
2 @RequestMapping("/goods")
3 public class GoodsController {
4
5 @Autowired
6 private IGoodsService goodsService;
7 @Bean
8 public boolean deleteGoodsNum(Integer goodsId,Integer goodsNum){
9 Goods goods = goodsService.getById(goodsId);
10 goods.setGoodsNumber(goods.getGoodsNumber()-goodsNum);
11 goodsService.updateById(goods);
12 return true;
13 }
14 }- 接着我在OutboundController中注入GoodsController
1 public class OutboundController {
2 @Autowired
3 private IOutboundService outboundService;
4 @Autowired
5 private GoodsController goodsController;
6
7 @PostMapping
8 public Result<?> addOutbound(@RequestBody Outbound outbound){
9 goodsController.deleteGoodsNum(outbound.getGoodsId(),outbound.getGoodsNumber());
10 LocalDateTime localDateTime = LocalDateTime.now();
11 outbound.setOutTime(localDateTime);
12 outboundService.save(outbound);
13 return Result.success("增加出库订单成功");
14 }
15 }- 运行完便报错了
- 解决:
引发的原因是因为两个类进行了相互的调用,触发了spring的循环依赖检查
SpringBoot中2.6后开启了循环依赖检查,并默认关闭了循环依赖支持 可以在配置中开启
诚然造成循环依赖是一个bad habit 我们需要另外一种解决方法:
- 1.试着把goodsServices注入过来结果还是一样
- 2.使用@Lazy 懒加载注解 还是不行
1 @Lazy
2 @Autowired
3 private GoodsController goodsController;- 3.试了一下在配置中开启循环依赖支持结果也不行。。
1 spring:
2 main:
3 allow-circular-references: true- 4.最后只好老实敲代码进行解耦了
- goodsPublicService
1 @Service
2 public interface goodsPublicService extends IService<Goods>{
3 void deleteGoodsNum(Integer goodsId, Integer goodsNumber);
4 }- goodsPublicServiceImpl:
1 @Service
2 public class goodsPublicServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements goodsPublicService {
3 public void deleteGoodsNum(Integer goodsId,Integer goodsNum){
4 Goods goods = this.baseMapper.selectById(goodsId);
5 goods.setGoodsNumber(goods.getGoodsNumber()-goodsNum);
6 this.baseMapper.updateById(goods);
7 }
8 }- OutboundController:
1 @PostMapping
2 public Result<?> addOutbound(@RequestBody Outbound outbound){
3 goodsPublicService.deleteGoodsNum(outbound.getGoodsId(),outbound.getOutNum());
4 LocalDateTime localDateTime = LocalDateTime.now();
5 outbound.setOutNum(outbound.getOutNum());
6 outbound.setOutTime(localDateTime);
7 outboundService.save(outbound);
8 return Result.success("增加出库订单成功");
9
- 成功运行!
关于SpringBoot中出现的循环依赖问题的更多相关文章
- SpringBoot项目意外出现 循环依赖和注入的对象意外是Null的问题 Requested bean is currently in creation: Is there an unresolvable circular reference? 或 nested exception is java.lang.NullPointerException
1.Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ...
- 在.NET Core中遭遇循环依赖问题"A circular dependency was detected"
今天在将一个项目迁移至ASP.NET Core的过程中遭遇一个循环依赖问题,错误信息如下: A circular dependency was detected for the service of ...
- SpringBoot中实现依赖注入功能
本文转载自:https://blog.csdn.net/linzhiqiang0316/article/details/52639888 今天给大家介绍一下SpringBoot中是如何实现依赖注入的功 ...
- 面试必杀技,讲一讲Spring中的循环依赖
本系列文章: 听说你还没学Spring就被源码编译劝退了?30+张图带你玩转Spring编译 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configu ...
- 面试阿里,腾讯,字节跳动90%都会被问到的Spring中的循环依赖
前言 Spring中的循环依赖一直是Spring中一个很重要的话题,一方面是因为源码中为了解决循环依赖做了很多处理,另外一方面是因为面试的时候,如果问到Spring中比较高阶的问题,那么循环依赖必定逃 ...
- 【Spring】Spring中的循环依赖及解决
什么是循环依赖? 就是A对象依赖了B对象,B对象依赖了A对象. 比如: // A依赖了B class A{ public B b; } // B依赖了A class B{ public A a; } ...
- Spring基础系列-Spring事务不生效的问题与循环依赖问题
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9476550.html 一.提出问题 不知道你是否遇到过这样的情况,在ssm框架中开发we ...
- 从源码层面深度剖析Spring循环依赖
作者:郭艳红 以下举例皆针对单例模式讨论 图解参考 https://www.processon.com/view/link/60e3b0ae0e3e74200e2478ce 1.Spring 如何创建 ...
- spring循环依赖问题分析
新搞了一个单点登录的项目,用的cas,要把源码的cas-webapp改造成适合我们业务场景的项目,于是新加了一些spring的配置文件. 但是在项目启动时报错了,错误日志如下: 一月 , :: 下午 ...
- 解析spring循环依赖策略
循环依赖 所谓循环依赖就是多个Bean之间依赖关系形成一个闭环,例如A->B->C->...->A 这种情况,当然,最简单的循环依赖就是2个Bean之间互相依赖:A->B ...
随机推荐
- 一文解锁vue3中hooks的使用姿势
vue3 中的 hooks 是什么? 简单来说如果你的函数中用到了诸如 ref,reactive,onMounted 等 vue 提供的 api 的话,那么它就是一个 hooks 函数,如果没用到它就 ...
- js调起android安卓与ios苹果方法 vue3.0 + ts
let shareSelect = (ev :any) => { const u :any= navigator.userAgent; const win :any = window const ...
- web组态软件(BY组态)介绍
BY组态是什么? BY组态面向工业物联网系统复杂的功能要求,通过"搭积木"的方式,拖拽组件到画布上,实现工业物联网可视化的web开发系统. BY组态适用领域 能源电力.物联网.智能 ...
- Note -「Maths」Euler 筛筛积性函数
Part. 1 Preface 这个东西是我在做 JZPTAB 的时候 LYC 给我讲的. 然后发现这是个通法,就写一写. 本文除了例题所有代码均未经过编译,仅作为参考. Part. 2 Untitl ...
- 文心一言 VS 讯飞星火 VS chatgpt (105)-- 算法导论10.1 3题
三.用go语言,仿照图 10-2,画图表示依次执行操作 ENQUEUE(Q,4).ENQUEUE(Q,1).ENQUEUE(Q,3).DEQUEUE(Q).ENQUEUE(Q,8)和 DEQUEUE( ...
- 数据泵(impdb)导入Oracle分片的数据库dump文件
数据泵(impdb)导入Oracle数据库 一.sqlplus登录目标数据库,创建导入的目录路径 #该目录要在导入的数据库本机建立,如果是docker就在容器内部创建 create directory ...
- Blackmail
Blackmail Arthur Hailey The chief house officer, Ogilvie, who had declared he would appear at the Cr ...
- MAC Big Sur系统升级导致三星移动硬盘T7无法识别解决方案
一.问题 MAC系统升级后总是导致三星移动硬盘(加密)无法被识别,影响正常使用.问售后让去官网下载最新驱动,第一次升级有用,在升级就没用了. 升级系统版本MAC 15.5.1重新安装官网驱动仍然无法识 ...
- CentOS7调整分区大小
前言 部署CentOS7的时候分配的动态扩充虚拟磁盘,共1T大小,在安装Centos时默认分区,系统仅给/分配50G,而大量空间都挂载到/home下,最近CentOS7使用中发现空间已不足够,所以就想 ...
- Spring ---三种注入方式
循环依赖这个问题,按理说我们在日常的程序设计中应该避免,其实这个本来也是能够避免的.不过由于总总原因,我们可能还是会遇到一些循环依赖的问题,特别是在面试的过程中,面试考察循环依赖,主要是想考察候选人对 ...