• 环境:

    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中出现的循环依赖问题的更多相关文章

  1. 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 ...

  2. 在.NET Core中遭遇循环依赖问题"A circular dependency was detected"

    今天在将一个项目迁移至ASP.NET Core的过程中遭遇一个循环依赖问题,错误信息如下: A circular dependency was detected for the service of ...

  3. SpringBoot中实现依赖注入功能

    本文转载自:https://blog.csdn.net/linzhiqiang0316/article/details/52639888 今天给大家介绍一下SpringBoot中是如何实现依赖注入的功 ...

  4. 面试必杀技,讲一讲Spring中的循环依赖

    本系列文章: 听说你还没学Spring就被源码编译劝退了?30+张图带你玩转Spring编译 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configu ...

  5. 面试阿里,腾讯,字节跳动90%都会被问到的Spring中的循环依赖

    前言 Spring中的循环依赖一直是Spring中一个很重要的话题,一方面是因为源码中为了解决循环依赖做了很多处理,另外一方面是因为面试的时候,如果问到Spring中比较高阶的问题,那么循环依赖必定逃 ...

  6. 【Spring】Spring中的循环依赖及解决

    什么是循环依赖? 就是A对象依赖了B对象,B对象依赖了A对象. 比如: // A依赖了B class A{ public B b; } // B依赖了A class B{ public A a; } ...

  7. Spring基础系列-Spring事务不生效的问题与循环依赖问题

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9476550.html 一.提出问题 不知道你是否遇到过这样的情况,在ssm框架中开发we ...

  8. 从源码层面深度剖析Spring循环依赖

    作者:郭艳红 以下举例皆针对单例模式讨论 图解参考 https://www.processon.com/view/link/60e3b0ae0e3e74200e2478ce 1.Spring 如何创建 ...

  9. spring循环依赖问题分析

    新搞了一个单点登录的项目,用的cas,要把源码的cas-webapp改造成适合我们业务场景的项目,于是新加了一些spring的配置文件. 但是在项目启动时报错了,错误日志如下: 一月 , :: 下午 ...

  10. 解析spring循环依赖策略

    循环依赖 所谓循环依赖就是多个Bean之间依赖关系形成一个闭环,例如A->B->C->...->A 这种情况,当然,最简单的循环依赖就是2个Bean之间互相依赖:A->B ...

随机推荐

  1. 关于初次new springboot项目

    如果是新手初学,然后做springboot项目报各种错,改来改去最终都无法出现successful字样. 请先检查,maven环境是否配好. maven环境决定你下载依赖的速度,以及能否下载成功. m ...

  2. 拦截|篡改|伪造.NET类库中不限于public的类和方法

    大家好,我是沙漠尽头的狼. 本文首发于Dotnet9,介绍使用Lib.Harmony库拦截第三方.NET库方法,达到不修改其源码并能实现修改方法逻辑.预期行为的效果,并且不限于只拦截public访问修 ...

  3. 低代码引擎 TinyEngine 正式发布!

    在当今数字化飞速发展的时代,企业对高效.敏捷的应用程序需求日益旺盛.为了满足这一需求,越来越多的低代码开发平台开始涌现.这些平台通过提供简单易用的开发工具和优化后的开发流程,帮助开发者快速构建高质量. ...

  4. FX3U-3A-ADP模拟量和数字量之间转换

    简单的例子: 0-10V对应0-8,4-20mA对应0-30 以下是对上面例子的详解: 电压: 电压(0-10V) 0-10V对应着数字量0-4000 数字量与变频器HZ量之间的关系是(4000-0) ...

  5. 爬虫系列——Scrapy

    文章目录 一 介绍 二 安装 三 命令行工具 四 项目结构以及爬虫应用简介 五 Spiders 六 Selectors 七 Items 八 Item Pipeline 九 Dowloader Midd ...

  6. Redis系列之——Redis介绍安装配置

    文章目录 第一章 redis初识 1.1 Redis是什么 1.2 Redis特性(8个) 1.3 Redis单机安装 1.3.1下载安装 1.3.2三种启动方式 1.3.2.1 最简启动 1.3.2 ...

  7. 【最佳实践】MongoDB导入数据时重建索引

    MongoDB一个广为诟病的问题是,大量数据resotore时索引重建非常缓慢,实测5000万的集合如果有3个以上的索引需要恢复,几乎没法成功,而且resotore时如果选择创建索引也会存在索引不生效 ...

  8. QT中级(1)QTableView自定义委托(一)实现QSpinBox、QDoubleSpinBox委托

    1 写在前面的话 我们在之前写的<QT(7)-初识委托>文章末尾提到,"使用一个类继承QStyledItemDelegate实现常用的控件委托,在使用时可以直接调用接口,灵活实现 ...

  9. t分布及t分布表

    http://baike.baidu.com/view/1419652.htm   下表列出了自由度为1-30以及80.100.120等t-分布的单侧和双侧区间值.例如,当样本数量n=5时,则自由度v ...

  10. UML学习入门就这一篇文章(转)

    1.1 UML基础知识扫盲 UML这三个字母的全称是Unified Modeling Language,直接翻译就是统一建模语言,简单地说就是一种有特殊用途的语言. 你可能会问:这明明是一种图形,为什 ...