|
|
@ -1,24 +1,24 @@ |
|
|
|
# Spring Bean 循环依赖修复报告 |
|
|
|
# Spring Bean 循环依赖修复报告 (完整版) |
|
|
|
|
|
|
|
## 问题描述 |
|
|
|
|
|
|
|
应用启动时出现 `BeanCreationException` 错误,错误信息显示: |
|
|
|
应用启动时出现复杂的 `BeanCreationException` 错误,涉及多个Bean的循环依赖: |
|
|
|
|
|
|
|
``` |
|
|
|
Error creating bean with name 'bszxBmController': Injection of resource dependencies failed; |
|
|
|
nested exception is org.springframework.beans.factory.BeanCreationException: |
|
|
|
Error creating bean with name 'bszxBmServiceImpl': Injection of resource dependencies failed; |
|
|
|
nested exception is org.springframework.beans.factory.BeanCreationException: |
|
|
|
Error creating bean with name 'cmsArticleServiceImpl': Injection of resource dependencies failed; |
|
|
|
nested exception is org.springframework.beans.factory.BeanCreationException: |
|
|
|
Error creating bean with name 'cmsNavigationServiceImpl': Injection of resource dependencies failed; |
|
|
|
nested exception is org.springframework.beans.factory.BeanCreationException: |
|
|
|
Error creating bean with name 'bszxBmController': Injection of resource dependencies failed; |
|
|
|
nested exception is org.springframework.beans.factory.BeanCreationException: |
|
|
|
Error creating bean with name 'bszxBmServiceImpl': Injection of resource dependencies failed; |
|
|
|
nested exception is org.springframework.beans.factory.BeanCreationException: |
|
|
|
Error creating bean with name 'cmsArticleServiceImpl': Injection of resource dependencies failed; |
|
|
|
nested exception is org.springframework.beans.factory.BeanCreationException: |
|
|
|
Error creating bean with name 'cmsNavigationServiceImpl': Injection of resource dependencies failed; |
|
|
|
nested exception is org.springframework.beans.factory.BeanCreationException: |
|
|
|
Error creating bean with name 'cmsDesignServiceImpl': Injection of resource dependencies failed |
|
|
|
``` |
|
|
|
|
|
|
|
## 根本原因分析 |
|
|
|
|
|
|
|
通过分析代码发现了两个主要的循环依赖问题: |
|
|
|
通过分析代码发现了复杂的循环依赖链,涉及多个层级的Bean相互依赖: |
|
|
|
|
|
|
|
### 1. 自我注入问题 |
|
|
|
在 `CmsNavigationServiceImpl` 中存在自我注入: |
|
|
@ -28,21 +28,29 @@ Error creating bean with name 'cmsDesignServiceImpl': Injection of resource depe |
|
|
|
public class CmsNavigationServiceImpl extends ServiceImpl<CmsNavigationMapper, CmsNavigation> implements CmsNavigationService { |
|
|
|
@Resource |
|
|
|
private CmsNavigationService cmsNavigationService; // 自我注入! |
|
|
|
|
|
|
|
|
|
|
|
// 在方法中使用 |
|
|
|
final CmsNavigation parent = cmsNavigationService.getOne(...); |
|
|
|
} |
|
|
|
``` |
|
|
|
|
|
|
|
### 2. 循环依赖问题 |
|
|
|
- `CmsNavigationServiceImpl` 依赖 `CmsDesignService` |
|
|
|
- `CmsDesignServiceImpl` 依赖 `CmsNavigationService` |
|
|
|
### 2. 复杂的循环依赖链 |
|
|
|
发现了以下循环依赖关系: |
|
|
|
|
|
|
|
这形成了一个循环依赖链: |
|
|
|
**主要循环依赖链**: |
|
|
|
``` |
|
|
|
CmsNavigationServiceImpl → CmsDesignService → CmsDesignServiceImpl → CmsNavigationService → CmsNavigationServiceImpl |
|
|
|
BszxBmController → BszxBmService → CmsArticleService → CmsNavigationService → CmsDesignService → CmsNavigationService |
|
|
|
``` |
|
|
|
|
|
|
|
**具体依赖关系**: |
|
|
|
- `BszxBmController` 依赖 `BszxBmService` 和 `CmsArticleService` |
|
|
|
- `BszxBmServiceImpl` 依赖 `CmsArticleService` |
|
|
|
- `CmsArticleServiceImpl` 依赖 `CmsNavigationService` |
|
|
|
- `CmsNavigationServiceImpl` 依赖 `CmsDesignService` 和自我注入 `CmsNavigationService` |
|
|
|
- `CmsDesignServiceImpl` 依赖 `CmsNavigationService` |
|
|
|
|
|
|
|
这形成了一个复杂的循环依赖网络,导致Spring无法正确初始化这些Bean。 |
|
|
|
|
|
|
|
## 修复方案 |
|
|
|
|
|
|
|
### 修复1:解决自我注入问题 |
|
|
@ -66,17 +74,18 @@ final CmsNavigation parent = cmsNavigationService.getOne(new LambdaQueryWrapper< |
|
|
|
final CmsNavigation parent = this.getOne(new LambdaQueryWrapper<CmsNavigation>()...); |
|
|
|
``` |
|
|
|
|
|
|
|
### 修复2:解决循环依赖问题 |
|
|
|
|
|
|
|
**文件**: `src/main/java/com/gxwebsoft/cms/service/impl/CmsDesignServiceImpl.java` |
|
|
|
### 修复2:使用 @Lazy 注解打破循环依赖 |
|
|
|
|
|
|
|
**修复前**: |
|
|
|
**文件1**: `src/main/java/com/gxwebsoft/cms/service/impl/CmsDesignServiceImpl.java` |
|
|
|
```java |
|
|
|
import org.springframework.context.annotation.Lazy; |
|
|
|
|
|
|
|
@Resource |
|
|
|
@Lazy |
|
|
|
private CmsNavigationService cmsNavigationService; |
|
|
|
``` |
|
|
|
|
|
|
|
**修复后**: |
|
|
|
**文件2**: `src/main/java/com/gxwebsoft/cms/service/impl/CmsArticleServiceImpl.java` |
|
|
|
```java |
|
|
|
import org.springframework.context.annotation.Lazy; |
|
|
|
|
|
|
@ -85,6 +94,24 @@ import org.springframework.context.annotation.Lazy; |
|
|
|
private CmsNavigationService cmsNavigationService; |
|
|
|
``` |
|
|
|
|
|
|
|
**文件3**: `src/main/java/com/gxwebsoft/bszx/service/impl/BszxBmServiceImpl.java` |
|
|
|
```java |
|
|
|
import org.springframework.context.annotation.Lazy; |
|
|
|
|
|
|
|
@Resource |
|
|
|
@Lazy |
|
|
|
private CmsArticleService cmsArticleService; |
|
|
|
``` |
|
|
|
|
|
|
|
**文件4**: `src/main/java/com/gxwebsoft/bszx/controller/BszxBmController.java` |
|
|
|
```java |
|
|
|
import org.springframework.context.annotation.Lazy; |
|
|
|
|
|
|
|
@Resource |
|
|
|
@Lazy |
|
|
|
private CmsArticleService cmsArticleService; |
|
|
|
``` |
|
|
|
|
|
|
|
## 修复详情 |
|
|
|
|
|
|
|
### 1. CmsNavigationServiceImpl.java 修复 |
|
|
|