循环依赖问题
2025年7月20日小于 1 分钟
循环依赖问题
出现场景
== 两个Service互相注入(字段注入或构造器注入)或者复杂的Bean之间的相互引用==
1.在记录service中注入了课表service
@Service
@RequiredArgsConstructor
public class LearningRecordServiceImpl extends ServiceImpl<LearningRecordMapper, LearningRecord> implements ILearningRecordService {
final ILearningLessonService lessonService;
final CourseClient courseClient;
}
2.又在课表service中注入了记录service
@Service
@RequiredArgsConstructor
public class LearningLessonServiceImpl extends ServiceImpl<LearningLessonMapper, LearningLesson> implements ILearningLessonService {
final LearningRecordService learningRecordService ;
}
解决方案
- 在其中的一个service中调用对方的mapper进行业务查询。就不要都使用对方的service,导致相互调用循环依赖。