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

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

在这里插入图片描述

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循环注入问题的解决

猜你喜欢

转载自blog.csdn.net/weixin_43811057/article/details/132888763