报错信息:
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
mqttConfig (field private com.awei.mqtt.MqttAcceptClient com.awei.config.MqttConfig.mqttAcceptClient)
┌─────┐
| mqttAcceptClient (field private com.awei.mqtt.MqttAcceptCallback com.awei.mqtt.MqttAcceptClient.mqttAcceptCallback)
↑ ↓
| mqttAcceptCallback (field private com.awei.mqtt.MqttAcceptClient com.awei.mqtt.MqttAcceptCallback.mqttAcceptClient)
└─────┘
报错原因:
两个类相互引用对方,导致Spring在初始化bean的时候不知道先初始化哪个,从而形成循环依赖注入。
解决方案:
在产生循环依赖的任意一个类上添加@Lazy注解即可。
@Autowired
@Lazy
private IAdminService adminService;