環境: SpringBoot2.7.8 背景: 在增加出庫訂單時需要對物品表的的數量進行修改 因此我在OutboundController中創建了幾個公共方法,並將其註入到Spring中,結果給我報了這一串錯誤。 Description:The dependencies of some of the ...
- 環境:
SpringBoot2.7.8
- 背景:
-
在增加出庫訂單時需要對物品表的的數量進行修改
-
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的迴圈依賴檢查
誠然造成迴圈依賴是一個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
- 成功運行!
本文來自博客園,作者:Lollipop_pro,轉載請註明原文鏈接:https://www.cnblogs.com/CooperCode/p/17657952.html