The dependencies of some of the beans in the application context form a cycle异常分析及解决

文章讲述了项目中遇到的循环依赖问题,通过调整构造器注入、YML配置允许循环引用,以及使用@Lazy注解延迟加载来解决SpringIoC中的循环注入问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中出现了循环依赖问题:

在这里插入图片描述

1、排查了构造器注入

在这里插入图片描述
改成
在这里插入图片描述

2、yml中添加了允许循环依赖。

spring:
  main:
      allow-circular-references: true  #允许循环引用

3、上面修改完之后还是不行

出现这个问题。
Requested bean is currently in creation: Is there an unresolvable circular reference?

Error creating bean with name 'ticketInterceptor': Unsatisfied dependency expressed through field 'appService'; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'appServiceImpl': Unsatisfied dependency expressed through field 'bizSysUserApi'; 
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'mvcResourceUrlProvider': Requested bean is currently in creation: Is there an unresolvable circular reference?

找到TicketInterceptor中AppService添加上@Lazy

在这里插入图片描述

@Lazy注解注解的作用主要是减少springIOC容器启动的加载时间。
Spring IoC (ApplicationContext) 容器一般都会在启动的时候实例化所有单实例 bean 。如果我们想要 Spring 在启动的时候延迟加载 bean,即在调用某个 bean 的时候再去初始化,那么就可以使用 @Lazy 注解。当出现循环依赖时,也可以添加@Lazy

Is there an unresolvable circular reference循环注入问题的解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值