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张图片

1、排查了构造器注入

The dependencies of some of the beans in the application context form a cycle异常分析及解决_第2张图片
改成
The dependencies of some of the beans in the application context form a cycle异常分析及解决_第3张图片

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

The dependencies of some of the beans in the application context form a cycle异常分析及解决_第4张图片

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

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

你可能感兴趣的:(#,异常,java)