在 Spring Boot 应用中,循环依赖指的是两个或多个 Bean 互相依赖,形成一个闭环。这会导致 Spring 容器无法正常启动。以下是详细的排查和解决步骤:
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'beanA': Requested bean is currently in creation: Is there an unresolvable circular reference?
使用 IDE 工具:
Spring
工具窗口来查看 Bean 的依赖关系。Spring Tool Suite
插件来查看 Bean 依赖图。手动检查:
@Component
、@Service
、@Repository
和 @Configuration
注解的类,检查它们的依赖关系。构造函数注入:
@Component
public class A {
private final B b;
@Autowired
public A(B b) {
this.b = b;
}
}
@Component
public class B {
private final A a;
@Autowired
public B(A a) {
this.a = a;
}
}
Setter 注入:
@Component
public class A {
private B b;
@Autowired
public void setB(B b) {
this.b = b;
}
}
@Component
public class B {
private A a;
@Autowired
public void setA(A a) {
this.a = a;
}
}
@Lazy
注解@Lazy
注解来延迟 Bean 的初始化。示例如下:@Component
public class A {
private final B b;
@Autowired
public A(@Lazy B b) {
this.b = b;
}
}
@Component
public class B {
private final A a;
@Autowired
public B(@Lazy A a) {
this.a = a;
}
}
@Component
public class A {
private B b;
@Autowired
public void setB(B b) {
this.b = b;
}
}
@Component
public class B {
private A a;
@Autowired
public void setA(A a) {
this.a = a;
}
}
@Component
public class A {
private final C c;
@Autowired
public A(C c) {
this.c = c;
}
}
@Component
public class B {
private final C c;
@Autowired
public B(C c) {
this.c = c;
}
}
@Component
public class C {
private final A a;
private final B b;
@Autowired
public C(A a, B b) {
this.a = a;
this.b = b;
}
}
@Configuration
类@Configuration
类中定义 Bean:
@Configuration
类中手动创建 Bean,可以更灵活地控制 Bean 的创建顺序。例如:@Configuration
public class AppConfig {
@Bean
public A a(B b) {
return new A(b);
}
@Bean
public B b(A a) {
return new B(a);
}
}
假设你有两个类 A
和 B
,它们之间存在循环依赖。以下是如何使用 @Lazy
注解来解决问题的示例:
@Component
public class A {
private final B b;
@Autowired
public A(@Lazy B b) {
this.b = b;
}
}
@Component
public class B {
private final A a;
@Autowired
public B(@Lazy A a) {
this.a = a;
}
}
@Lazy
注解、改用 Setter 注入、重构 Bean 设计、在 @Configuration
类中定义 Bean。通过这些步骤,你可以有效地解决 Spring Boot 应用中的循环依赖问题,确保应用能够顺利启动。
一般使用 @Lazy
注解就可以解决。
仅为个人知识分享及开发中遇到的问题总结,
希望对你有所帮助,若有问题欢迎指正~