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

拼搏现实的明天。 2023-10-15 22:54 260阅读 0赞

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

在这里插入图片描述

1、排查了构造器注入

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

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

  1. spring:
  2. main:
  3. allow-circular-references: true #允许循环引用

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

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

  1. Error creating bean with name 'ticketInterceptor': Unsatisfied dependency expressed through field 'appService';
  2. nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'appServiceImpl': Unsatisfied dependency expressed through field 'bizSysUserApi';
  3. 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循环注入问题的解决

发表评论

表情:
评论列表 (有 0 条评论,260人围观)

还没有评论,来说两句吧...

相关阅读