springboot 在一个Service类的方法上用异步注解@Async,启动就报循环依赖错误

我只要在directoryProcurementApplyServiceImpl这个类中的一个方法上加上@Async,启动就报错。

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

| ESInitController (field private com.zhichubao.core.service.p2p.payables.PayablesService com.zhichubao.web.api.ESInitController.payablesService)
↑ ↓
| payablesServiceImpl (field private com.zhichubao.core.service.common.CommonService com.zhichubao.core.service.p2p.payables.impl.PayablesServiceImpl.commonService)
↑ ↓
| commonServiceImpl (field private com.zhichubao.core.service.p2p.procurement.DirectoryProcurementApplyService com.zhichubao.core.service.common.impl.CommonServiceImpl.directoryProcurementApplyService)
↑ ↓
| directoryProcurementApplyServiceImpl
└─────┘

但是我在controller中用过异步注解很多次都没报错,放到service上的方法上就报错,什么原因呢?

阅读 7k
1 个回答

在你循环依赖的两个bean的任意一个上加上lazy-init属性试试。
例如:

@Autowired
@Lazy
private Test test; 
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题