feign循环依赖注入报错的解决过程
feign循环依赖注入报错的解决过程
1、问题记录
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘webMvcConfig’: Unsatisfied dependency expressed through field ‘interceptor’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘authenticationInterceptor’: Unsatisfied dependency expressed through field ‘securityUtil’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘securityUtil’: Unsatisfied dependency expressed through field ‘monitorUserService’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘com.example.client.service.MonitorUserService’: FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘mvcResourceUrlProvider’: Requested bean is currently in creation: Is there an unresolvable circular reference?
2、原因
根据提示的异常,mvcResourceUrlProvider
发生了循环依赖,报错的注入和在拦截器内注入的service有依赖关系,且最下一级定位到了feignClient,故判定是feign在拦截器内的注入发生了循环依赖。
原先代码如下:
3、解决方法
解决方法一般为两:添加@Lazy
懒加载注解或使用getBean方法手动注入;
1.使用getBean手动注入
改为手动配置后,出现Constructor threw exception; nested exception is java.lang.NullPointerException
的问题;
报错说是 实体类里面缺少空参构造方法;
解决方法:更改getBean方法的位置,放到需要使用的地方;如下所示:
运行成功!
2.使用懒加载@Lazy
默认情况下,Spring会在应用程序上下文的启动时创建所有单例bean。原因:立即避免和检测所有可能的错误,而不是在运行时。
但是,有些情况下我们需要创建一个bean,而不是在应用程序上下文启动时,而是在我们请求时。所以通过@Lazy
同样可以解决问题;
所在包:import org.springframework.context.annotation.Lazy;
标签:
相关文章
-
无相关信息