SpringCloud学习笔记(一) Spring循环依赖问题
在学习SpringCloud的过程中遇到一个问题,在使用feign编写失败降级逻辑之后重启服务调用方 orderService
时,服务重启失败。
失败原因如下:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'orderService': Unsatisfied dependency expressed through field 'userClient'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'cn.itcast.feign.clients.UserClient': Requested bean is currently in creation: Is there an unresolvable circular reference?
日志提示我可能存在循环依赖问题,可是我检查了一遍之后发现好像并没有循环依赖的问题(也有可能是我没发现),总之就是因为这个问题导致我这个 orderService
服务无法启动。那么如何解决这一问题呢?看过弹幕里给出的两种解决方法。
- 在服务调用方
orderService
的启动类上添加上扫描feign包的注解@ComponentScan("cn.itcast.feign")
- 修改父工程里面的springcloud依赖版本
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
(把SR10改成了SR8) - 在服务调用方注入feign客户端
UserClient
时,采用@Lazy
注解使其延迟注入
只要采用上面三种方法中的任何一个,服务调用方都能够重新正常启动。
但是之前导致服务不能正常启动的真正原因我还不清楚,如果有了解原理的同学希望能够多多指教,评论。