Requested bean is currently in creation: Is there an unresolvable circular reference? (circular dependency error)

What is a circular dependency?

For example, you call Xiaoming, and Xiaoming is also calling you. At this time, the phone will say that the other party is on the phone, and the above error report is this "in progress". The meaning of the code is: if b is injected into a, and a is injected into b, the circular dependency problem will be triggered;

Not much to say about the code:

public class A{
    @Autowired
    private B b;
}
public class B{
    @Autowired
    private A a;
}

Solution:

  1. Add the @Lazy annotation to a certain class. The function of this annotation is to delay the loading of one of the beans that depend on each other, so as to solve the problem that Spring does not know which one to initialize first when initializing the bean.

public class A{
    @Autowired
    @Lazy
    private B b;
}
  1. Solve the circular dependency by modifying the yml configuration file

Spring:
  main:
    allow-circular-references:true  

Summarize:

Circular dependent references are discouraged and disabled by default. Don’t use it if you can~~~

おすすめ

転載: blog.csdn.net/qq_61544409/article/details/128603136