关于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 ...
随机推荐
- Prompt 指北:如何写好 Prompt,让 GPT 的回答更加精准
目录 1. 得亏 GPT 脾气好 2. 玩 GPT 得注意姿势 3. 指南指北指东指西 3.1 首先你得理解 GPT 是咋工作的 3.2 "Prompt 工程"走起 3.3 奇淫技 ...
- Java程序员学vue3最好的方式就是搭建后台管理模板
前言 作为Java程序员,vue3还是有必要学的,毕竟是国内最受欢迎的前端JS框架,你现在接手的项目,前端部分几乎都会和vue沾边,尤其是中小企业. vue3作为新的大版本,相较于vue2改动还是很多 ...
- Netty+WebSocket整合STOMP协议
1.STOMP协议简介 常用的WebSocket协议定义了两种传输信息类型:文本信息和二进制信息.类型虽然被确定,但是他们的传输体是没有规定的,也就是说传输体可以自定义成什么样的数据格式都行,只要客户 ...
- Centos7使用s3fs-fuse挂载minio对象存储实践
Centos7使用s3fs-fuse挂载minio对象存储实践 事前准备 主机可以访问到对象存储API.例如minio默认的9000端口 主机安装好s3fs软件 已在minio上创建存储桶 安装s3f ...
- 人工智能AI绘画全攻略(AI绘画教程分享)
在过去的三个月一直在研究人工智能生成绘画这个方向,3 月份的时候参加了小红书的小航海,也因为这个方向的选择正好对应到了趋势,小红书在一个半月做到了 1 万粉.我为什么看好这个方向? 主要是从三个方面: ...
- 19c上ADG主库sys密码修改会影响备库同步吗?
一套Oracle 19c的ADG集群要修改sys密码,由于之前遇见过11g上sys密码修改导致同步问题的情况,所以改之前特意查了下文档,发现其实12cR2开始,在主库修改密码就会自动同步到备库了,以下 ...
- SpringBoot整合XXLJob
目录 XXLJob简介 特性 模块 安装调度中心 初始化数据库 配置 启动 整合执行器 pom yml XxlJobConfig 启动执行器 实践 简单的定时任务 在执行器创建任务 在调度中心创建执行 ...
- P4899 [IOI2018] werewolf 狼人 题解
P4899 [IOI2018] werewolf 狼人 题解 题目描述 省流: \(n\) 个点,\(m\) 条边,\(q\) 次询问,对于每一次询问,给定一个起点 \(S\) 和终点 \(T\) , ...
- Windows下音视频对讲演示程序(声学回音消除、噪音抑制、语音活动检测、自动增益控制、自适应抖动缓冲)(2023年07月13日更新)
Windows下音视频对讲演示程序 必读说明 简介 本软件根据<道德经>为核心思想而设计,实现了两个设备之间进行音视频对讲,一般可用于楼宇对讲.智能门铃对讲.企业员工对讲.智能对讲机. ...
- 如何使用DALL-E 3
如何使用 DALL-E 3:OpenAI 图像生成指南 DALL-E 3 是 OpenAI 图像生成器的高级版本,它可以理解自然语言提示来创建详细图像. 它克服了以前版本的方形图像限制,现在支持各种宽 ...