【报错解决】The dependencies of some of the beans in the application context form a cycle:

【报错解决】The dependencies of some of the beans in the application context form a cycle:,第1张

【报错解决】The dependencies of some of the beans in the application context form a cycle:

文章目录
  • 一、报错信息
  • 二、背景说明
  • 报错原因
  • 报错解决

我是一名立志把细节都说清楚的博主,欢迎关注 ~
原创不易,如果有帮助,还请鼓励个【赞】哦~ ❥(^_-)~

一、报错信息
The dependencies of some of the beans in the application context form a cycle:

   securityConfig (field com.XXX.security.UserDetailServiceImpl com.XXX.config.SecurityConfig.userDetailService)
      ↓
   userDetailServiceImpl (field com.XXX.service.SysUserService com.XXX.security.UserDetailServiceImpl.sysUserService)
┌─────┐
|  sysUserServiceImpl (field com.XXX.service.SysMenuService com.XXX.service.impl.SysUserServiceImpl.sysMenuService)
↑     ↓
|  sysMenuServiceImpl (field com.XXX.service.SysUserService com.XXX.service.impl.SysMenuServiceImpl.sysUserService)
└─────┘
二、背景说明
  • Springboot
  • Mybatis Puls
报错原因
  • 两个类相互引用对方,导致Spring在初始化bean的时候不知道先初始化哪个,从而形成循环依赖注入。
报错解决
  • 其中一个不要引用对方,避免循环依赖,代码解耦肯定是最优解。
  • 任选其中一个使用@Lazy 注解。
    • 延迟互相依赖的其中一个bean的加载,从而解决Spring在初始化bean的时候不知道先初始化哪个的问题。
// 任选一个加上@Lazy注解即可。
@Autowired
@Lazy 
XXXService xxxService;
  • 如果是SSM项目,可以在配置文件中加上lazy-init="true"属性。

 
 
   
 

我是一名立志把细节都说清楚的博主,欢迎关注 ~
原创不易,如果有帮助,还请鼓励个【赞】哦~ ❥(^_-)~

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5637713.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存