项目中在使用lombok的@RequiredArgsConstructor配合final做自动注入时,出现了循环依赖的问题。
idea 控制台输出:
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
dutyShiftController defined in file [D:\workspace\idea\itff\itff-service\target\classes\com\bdyl\msc\itff\service\controller\fireFightingManage\duty\DutyShiftController.class]
┌─────┐
| dutyShiftService defined in file [D:\workspace\idea\itff\itff-service\target\classes\com\bdyl\msc\itff\service\service\fireFightingManage\duty\DutyShiftService.class]
↑ ↓
| dutyTaskService defined in file [D:\workspace\idea\itff\itff-service\target\classes\com\bdyl\msc\itff\service\service\fireFightingManage\duty\DutyTaskService.class]
└─────┘
Action:
Despite circular references being allowed, the dependency cycle between beans could not be broken. Update your application to remove the dependency cycle.
Disconnected from the target VM, address: '127.0.0.1:63542', transport: 'socket'
Process finished with exit code 0
解决办法:
1、改用@Autowird注解注入
2、@RequiredArgsConstructor(onConstructor_ = {@Lazy})
原因:
@RequiredArgsConstructor是使用构造器注入方式,这种方式会有循环依赖的问题。