问题产生原因
- A类引入B类
- B类引入A类
- 造成Spring Bean的循环依赖
解决方案看下面
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-12-22 20:28:03.186 ERROR 1492 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter -
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
mallSalesController defined in file [D:\Data\GitLab\badger-brand-cloud\badger-brand-admin\badger-brand-admin-server\target\classes\com\badger\controller\MallSalesController.class]
┌─────┐
| mallSalesServiceImpl defined in file [D:\Data\GitLab\badger-brand-cloud\badger-brand-admin\badger-brand-admin-server\target\classes\com\badger\service\impl\MallSalesServiceImpl.class]
↑ ↓
| sysBrandMallServiceImpl defined in file [D:\Data\GitLab\badger-brand-cloud\badger-brand-admin\badger-brand-admin-server\target\classes\com\badger\service\impl\SysBrandMallServiceImpl.class]
└─────┘
Disconnected from the target VM, address: '127.0.0.1:65479', transport: 'socket'
Process finished with exit code 1
解决方案
- A 类继续使用
@Autowired
注入 - B类使用
@Lazy
注解加set
注入
示例:A类
@Service
public class AServiceImpl implements AUserService{
private final BUserService service;
public AServiceImpl (@Lazy BUserService service ) {
this.service = service ;
}
示例:B类
@Service
public class AServiceImpl implements BUserService{
private final AUserService service;
public BServiceImpl (@Lazy AUserService service) {
this.service = service;
}