java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring Boot循环依赖

Spring Boot循环依赖原理、解决方案与最佳实践(全解析)

作者:weixin_52318532

循环依赖指两个或多个Bean相互直接或间接引用,形成闭环依赖关系,这篇文章主要介绍了Spring Boot循环依赖原理、解决方案与最佳实践(全解析),需要的朋友可以参考下

🚨 Spring Boot循环依赖全解析:原理、解决方案与最佳实践

#SpringBoot核心 #依赖注入 #设计模式 #性能优化

一、循环依赖的本质与危害

1.1 什么是循环依赖?

循环依赖指两个或多个Bean相互直接或间接引用,形成闭环依赖关系。
典型场景

@Service  
public class ServiceA {  
    @Autowired  
    private ServiceB serviceB;  
}  
@Service  
public class ServiceB {  
    @Autowired  
    private ServiceA serviceA;  
}  

Spring启动时抛出异常:

BeanCurrentlyInCreationException: Error creating bean with name 'serviceA':  
Requested bean is currently in creation: Is there an unresolvable circular reference?  

1.2 核心危害

pring无法完成Bean初始化设计缺陷信号:模块职责不清,耦合度过高潜在性能问题:即使解决循环依赖,可能引发隐藏的初始化顺序问题

二、Spring的三级缓存机制

Spring通过三级缓存解决Setter/Field注入的循环依赖,但无法解决构造器注入的循环依赖。

2.1 三级缓存结构

缓存级别存储内容
一级缓存(singletonObjects)完全初始化的Bean
二级缓存(earlySingletonObjects)提前暴露的早期Bean(未完成属性填充)
三级缓存(singletonFactories)Bean工厂对象(用于生成早期引用)

 2.2 解决流程(以ServiceA和ServiceB为例)

1. 创建ServiceA → 将原始对象工厂放入三级缓存  
2. 填充ServiceA属性 → 发现需要ServiceB  
3. 创建ServiceB → 将原始对象工厂放入三级缓存  
4. 填充ServiceB属性 → 从三级缓存获取ServiceA的工厂 → 生成代理对象  
5. ServiceB初始化完成 → 移入一级缓存  
6. ServiceA继续填充ServiceB → 从一级缓存获取ServiceB → 完成初始化  

三、解决方案与代码实战

3.1 避免构造器注入循环

构造器注入循环依赖无法解决(Spring 5.3+默认禁止):

// 错误示例:启动直接失败  
@Service  
public class ServiceA {  
    private final ServiceB serviceB;  
    public ServiceA(ServiceB serviceB) {  
        this.serviceB = serviceB;  
    }  
}  
@Service  
public class ServiceB {  
    private final ServiceA serviceA;  
    public ServiceB(ServiceA serviceA) {  
        this.serviceA = serviceA;  
    }  
}  

强制允许构造器循环依赖(不推荐)

# application.properties  
spring.main.allow-circular-references=true  

3.2 使用Setter/Field注入

将构造器注入改为Setter注入:

@Service  
public class ServiceA {  
    private ServiceB serviceB;  
    @Autowired  
    public void setServiceB(ServiceB serviceB) {  
        this.serviceB = serviceB;  
    }  
}  
@Service  
public class ServiceB {  
    private ServiceA serviceA;  
    @Autowired  
    public void setServiceA(ServiceA serviceA) {  
        this.serviceA = serviceA;  
    }  
}  

3.3 @Lazy延迟加载

强制延迟其中一个Bean的初始化:

@Service  
public class ServiceA {  
    @Lazy  
    @Autowired  
    private ServiceB serviceB;  
}  

原理:ServiceB被代理,首次调用时才会真实初始化。

3.4 接口抽象解耦

通过接口隔离实现类依赖

public interface IServiceA {  
    void doSomething();  
}  
public interface IServiceB {  
    void doAnother();  
}  
@Service  
public class ServiceAImpl implements IServiceA {  
    @Autowired  
    private IServiceB serviceB;  
}  
@Service  
public class ServiceBImpl implements IServiceB {  
    @Autowired  
    private IServiceA serviceA;  
}  

四、深度优化:设计模式应用

4.1 依赖倒置原则(DIP)

高层模块不应依赖低层模块,二者都应依赖抽象

// 定义数据访问接口  
public interface UserRepository {  
    User findById(Long id);  
}  
// 高层服务依赖接口  
@Service  
public class UserService {  
    private final UserRepository repository;  
    public UserService(UserRepository repository) {  
        this.repository = repository;  
    }  
}  
// 低层实现  
@Repository  
public class JpaUserRepository implements UserRepository {  
    // 实现细节  
}  

4.2 事件驱动模型

通过ApplicationEvent解耦强依赖

// ServiceA发布事件  
@Service  
public class ServiceA {  
    @Autowired  
    private ApplicationEventPublisher publisher;  
    public void triggerEvent() {  
        publisher.publishEvent(new CustomEvent(this));  
    }  
}  
// ServiceB监听事件  
@Service  
public class ServiceB {  
    @EventListener  
    public void handleEvent(CustomEvent event) {  
        // 处理逻辑  
    }  
}  

五、检测与预防工具

5.1 IDE检测

5.2 Maven插件分析

<plugin>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-maven-plugin</artifactId>  
    <configuration>  
        <excludes>  
            <exclude>  
                <groupId>org.projectlombok</groupId>  
                <artifactId>lombok</artifactId>  
            </exclude>  
        </excludes>  
    </configuration>  
</plugin>  

运行命令:

mvn spring-boot:run -Dspring-boot.run.profiles=dev  

5.3 架构规范

六、常见问题解答

Q1:允许循环依赖对性能有影响吗?

Q2:@Lazy注解可以随便用吗?

Q3:Spring为什么能解决Setter注入循环依赖?

七、总结与最佳实践

黄金法则

紧急修复流程

发现循环依赖 → 使用@Lazy临时解决 → 标记为技术债务 → 排期重构  

工具推荐

通过合理设计+规范约束,可有效避免循环依赖,构建高可维护的Spring Boot应用! 🚀

到此这篇关于Spring Boot循环依赖全解析:原理、解决方案与最佳实践的文章就介绍到这了,更多相关Spring Boot循环依赖内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文