关于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 ...
随机推荐
- hihocoder 1290 DP
题目利用DP思想,dp[i][j][k]表示robot跑到i行j列目前移动方向为k时,所需要的最小的flip.其中0 <= i <= N,0 <= j <= M,k = rig ...
- 算法2:寻找吸血鬼数(JS)
任务二:寻找吸血鬼数 打印所有4位吸血鬼数和它们的獠牙 提示:一共有7个: 吸血鬼数: -该鬼的位数为偶数: -该数的所有位中.是0的位少一半: -该数每一位上的数字重新组合为两个位数相等的数,乘 ...
- gestureRecognition
这段代码定义了一个名为 gestureRecognition 的函数,它用于识别手势并显示在摄像头或指定图像上.以下是对代码的详细注释:1. 初始化一个空字符串 ges,用于存储手势结果.如果 sel ...
- CSP-J 2022 游记
10.9 早上睡到 7:00. 上午继续学习 Vim,学习哈希表. 10.11 白天线段树,区间加从六参改成四参就过了 晚上模拟赛,感觉良好 10.16 膜你赛,std变量命名毒瘤. 想用 geogb ...
- 每个后端都应该了解的OpenResty入门以及网关安全实战
简介 在官网上对 OpenResty 是这样介绍的(http://openresty.org): "OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成 ...
- JDK21的虚拟线程是什么?和平台线程什么关系?
虚拟线程(Virtual Thread)是 JDK 而不是 OS 实现的轻量级线程(Lightweight Process,LWP),由 JVM 调度.许多虚拟线程共享同一个操作系统线程,虚拟线程的数 ...
- 当scroll-view水平滚动,内容溢出时,文本会自动竖向排列问题
当scroll-view水平滚动,内容溢出时,文本会自动竖向排列 解决方法:thite-space:nowrap:规定段落中的文本不进行换行
- 实验1 C语言输入输出和简单程序编写
1.试验任务1 task1.c //打印一个字符小人 #include <stdio.h> int main() { printf(" o \n"); printf(& ...
- Istio 入门(七):出入口网关 - 负载均衡和熔断等一系列功能
本教程已加入 Istio 系列:https://istio.whuanle.cn 目录 5,出入口网关 istio-ingressgateway 部署服务 配置 Gateway 子版本 istio-e ...
- [vue]精宏技术部试用期学习笔记 III
精宏技术部试用期学习笔记(vue) 父子通信 什么是通信 / 为什么要通信 通信即在不同组件之间传输数据 当在 复用组件 时,需要传递不同数据达成不同的表现效果 能够根据其他组件的行动,响应式 的做出 ...