Browse Source

修复优惠券模块

main
科技小王子 2 weeks ago
parent
commit
c93eeaa5e3
  1. 71
      spring_bean_circular_dependency_fix.md
  2. 2
      src/main/java/com/gxwebsoft/bszx/controller/BszxBmController.java
  3. 2
      src/main/java/com/gxwebsoft/bszx/service/impl/BszxBmServiceImpl.java
  4. 2
      src/main/java/com/gxwebsoft/cms/controller/CmsArticleController.java
  5. 9
      src/main/java/com/gxwebsoft/cms/service/impl/CmsArticleContentServiceImpl.java
  6. 16
      src/main/java/com/gxwebsoft/cms/service/impl/CmsArticleServiceImpl.java
  7. 1
      src/main/java/com/gxwebsoft/cms/service/impl/CmsDesignServiceImpl.java
  8. 2
      src/main/java/com/gxwebsoft/cms/service/impl/CmsNavigationServiceImpl.java

71
spring_bean_circular_dependency_fix.md

@ -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 修复

2
src/main/java/com/gxwebsoft/bszx/controller/BszxBmController.java

@ -13,6 +13,7 @@ import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@ -34,6 +35,7 @@ public class BszxBmController extends BaseController {
@Resource
private BszxBmService bszxBmService;
@Resource
@Lazy
private CmsArticleService cmsArticleService;
@PreAuthorize("hasAuthority('bszx:bszxBm:list')")

2
src/main/java/com/gxwebsoft/bszx/service/impl/BszxBmServiceImpl.java

@ -24,6 +24,7 @@ import com.gxwebsoft.common.core.utils.ImageUtil;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
@ -49,6 +50,7 @@ public class BszxBmServiceImpl extends ServiceImpl<BszxBmMapper, BszxBm> impleme
@Resource
private ConfigProperties config;
@Resource
@Lazy
private CmsArticleService cmsArticleService;
@Resource
private BszxClassService bszxClassService;

2
src/main/java/com/gxwebsoft/cms/controller/CmsArticleController.java

@ -20,6 +20,7 @@ import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.UserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
@ -52,6 +53,7 @@ public class CmsArticleController extends BaseController {
@Resource
private CmsArticleContentService articleContentService;
@Resource
@Lazy
private CmsNavigationService cmsNavigationService;
@Resource
private CmsModelService cmsModelService;

9
src/main/java/com/gxwebsoft/cms/service/impl/CmsArticleContentServiceImpl.java

@ -17,6 +17,7 @@ import com.gxwebsoft.common.core.utils.AliYunSender;
import com.gxwebsoft.common.core.utils.JSONUtil;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -33,12 +34,12 @@ import java.util.Map;
@Service
public class CmsArticleContentServiceImpl extends ServiceImpl<CmsArticleContentMapper, CmsArticleContent> implements CmsArticleContentService {
@Resource
@Lazy
private CmsNavigationService cmsNavigationService;
@Resource
@Lazy
private CmsArticleService cmsArticleService;
@Resource
private CmsArticleContentService cmsArticleContentService;
@Resource
private CmsLangLogService cmsLangLogService;
@Override
public PageResult<CmsArticleContent> pageRel(CmsArticleContentParam param) {
@ -139,7 +140,7 @@ public class CmsArticleContentServiceImpl extends ServiceImpl<CmsArticleContentM
target.setContent(article.getContent());
System.out.println("target = " + target);
cmsArticleService.updateById(target);
cmsArticleContentService.update(new LambdaUpdateWrapper<CmsArticleContent>().eq(CmsArticleContent::getArticleId, target.getArticleId()).set(CmsArticleContent::getContent,target.getContent()));
this.update(new LambdaUpdateWrapper<CmsArticleContent>().eq(CmsArticleContent::getArticleId, target.getArticleId()).set(CmsArticleContent::getContent,target.getContent()));
}
}else {
// 新增操作
@ -149,7 +150,7 @@ public class CmsArticleContentServiceImpl extends ServiceImpl<CmsArticleContentM
content.setArticleId(article.getArticleId());
content.setContent(article.getContent());
content.setTenantId(article.getTenantId());
cmsArticleContentService.save(content);
this.save(content);
}
}

16
src/main/java/com/gxwebsoft/cms/service/impl/CmsArticleServiceImpl.java

@ -20,6 +20,7 @@ import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.service.UserService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
@ -41,12 +42,11 @@ import static com.gxwebsoft.common.core.constants.ArticleConstants.CACHE_KEY_ART
@Service
public class CmsArticleServiceImpl extends ServiceImpl<CmsArticleMapper, CmsArticle> implements CmsArticleService {
@Resource
@Lazy
private CmsNavigationService cmsNavigationService;
@Resource
private CmsArticleContentService cmsArticleContentService;
@Resource
private CmsArticleContentService articleContentService;
@Resource
private UserService userService;
@Resource
private CmsModelService cmsModelService;
@ -122,7 +122,7 @@ public class CmsArticleServiceImpl extends ServiceImpl<CmsArticleMapper, CmsArti
// article.setBanner(model.getBanner());
// }
// 附加文字内容
CmsArticleContent content = articleContentService.getOne(new LambdaQueryWrapper<CmsArticleContent>().eq(CmsArticleContent::getArticleId, article.getArticleId()).last("limit 1"));
CmsArticleContent content = cmsArticleContentService.getOne(new LambdaQueryWrapper<CmsArticleContent>().eq(CmsArticleContent::getArticleId, article.getArticleId()).last("limit 1"));
if (content != null) {
article.setContent(content.getContent());
}
@ -172,9 +172,9 @@ public class CmsArticleServiceImpl extends ServiceImpl<CmsArticleMapper, CmsArti
content.setArticleId(article.getArticleId());
content.setContent(article.getContent());
content.setTenantId(article.getTenantId());
articleContentService.save(content);
cmsArticleContentService.save(content);
// 同步翻译并保存
articleContentService.translate(article);
cmsArticleContentService.translate(article);
return true;
}
} catch (Exception e) {
@ -221,11 +221,11 @@ public class CmsArticleServiceImpl extends ServiceImpl<CmsArticleMapper, CmsArti
String key = CACHE_KEY_ARTICLE + article.getArticleId();
redisUtil.delete(key);
// 更新内容
final boolean update = articleContentService.update(new LambdaUpdateWrapper<CmsArticleContent>().eq(CmsArticleContent::getArticleId, article.getArticleId()).set(CmsArticleContent::getContent, article.getContent()));
final boolean update = cmsArticleContentService.update(new LambdaUpdateWrapper<CmsArticleContent>().eq(CmsArticleContent::getArticleId, article.getArticleId()).set(CmsArticleContent::getContent, article.getContent()));
if (update) {
// 同步翻译并保存
article.setIsUpdate(true);
articleContentService.translate(article);
cmsArticleContentService.translate(article);
return true;
} else {
// 添加内容
@ -233,7 +233,7 @@ public class CmsArticleServiceImpl extends ServiceImpl<CmsArticleMapper, CmsArti
content.setArticleId(article.getArticleId());
content.setContent(article.getContent());
content.setTenantId(article.getTenantId());
articleContentService.save(content);
cmsArticleContentService.save(content);
}
return true;
}

1
src/main/java/com/gxwebsoft/cms/service/impl/CmsDesignServiceImpl.java

@ -43,6 +43,7 @@ public class CmsDesignServiceImpl extends ServiceImpl<CmsDesignMapper, CmsDesign
@Lazy
private CmsNavigationService cmsNavigationService;
@Resource
@Lazy
private CmsArticleContentService cmsArticleContentService;
@Override

2
src/main/java/com/gxwebsoft/cms/service/impl/CmsNavigationServiceImpl.java

@ -16,6 +16,7 @@ import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.service.UserService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -31,6 +32,7 @@ import java.util.List;
@Service
public class CmsNavigationServiceImpl extends ServiceImpl<CmsNavigationMapper, CmsNavigation> implements CmsNavigationService {
@Resource
@Lazy
private CmsDesignService cmsDesignService;
@Resource
private CmsModelService cmsModelService;

Loading…
Cancel
Save