记一次Spring 循环依赖的踩坑过程

问题

今日Springboot项目在启动的时候报错了,错误如下

ERROR SpringApplication - Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'xxxxxxxx': Unsatisfied dependency expressed through field 'xxxxxxxxxxxx'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'xxxxxxxxxxxxxx': Bean with name 'xxxxxxxxxxxx' has been injected into other beans [xxxxxxxxxxxxxx] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'xxxxxxxxxxxxxx' with the 'xxxxxxxxxxxxxx' flag turned off, for example.

 排查过程

通过网上查阅资料得知可能是Spring循环的问题,但是Spring官方其实已经通过三级缓存解决了这个问题,但是现在报错只能先使用 @Lazy注解看一看,但是运行之后还是不行,这下可就麻烦了,但是考虑之前是没有问题的,但是这次突然问题,一定最近几天提交的代码有问题,于是查阅代码提交记录,只有在循环依赖的一个类中加了一个异步的方法,难道是这个异步方法导致了循环依赖的问题???带着这样的疑问开始查阅相关资料,果然有这样的问题存在,大概就是因为异步方法会改变三级缓存的原始对象,导致在最终实例化依赖对象的时候出现异常,具体的细节可以看下这篇博文分析https://segmentfault.com/a/1190000018835760

推荐阅读:https://segmentfault.com/a/1190000018835760

你可能感兴趣的:(SPRING)