The dependencies of some of the beans in the application context form a cycle:

【问题】

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

   customerContactController (field private com.jyxpackaging.ecp.microservices.customer.service.ICustomerContactService com.jyxpackaging.ecp.microservices.customer.controller.CustomerContactController.customerContactService)
┌─────┐
|  customerContactServiceImpl (field private com.jyxpackaging.ecp.microservices.customer.service.ICustomerService com.jyxpackaging.ecp.microservices.customer.service.impl.CustomerContactServiceImpl.customerService)
↑     ↓
|  customerServiceImpl (field private com.jyxpackaging.ecp.microservices.customer.service.ICustomerContactService com.jyxpackaging.ecp.microservices.customer.service.impl.CustomerServiceImpl.customerContactService)
└─────┘


Action:

Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.

【原因】两个类相互引用对方,导致Spring在初始化bean的时候不知道先初始化哪个,从而形成循环依赖注入。

【解决】

1、其中一个不要引用对方,避免循环依赖,代码解耦肯定是最优解。

2、任选其中一个使用@Lazy 注解。(通过延迟互相依赖的其中一个bean的加载,从而解决Spring在初始化bean的时候不知道先初始化哪个的问题。)

 

posted @ 2022-08-02 09:52  changlinlo  阅读(1436)  评论(7编辑  收藏  举报