在开发spring boot 项目时候,出现以下spring 循环引用的报错,关键在日志是:
Bean with name ‘deviceService’ has been injected into other beans [transFlowService] 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 ‘getBeanNamesOfType’ with the ‘allowEagerInit’ flag turned off, for example

以下是报错详细日志:

===2019-06-13 15:12:47.830 ERROR org.springframework.boot.SpringApplication Line:858 - Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batteryController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'deviceService': Bean with name 'deviceService' has been injected into other beans [transFlowService] 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 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example. at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:324) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1395) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) at com.csii.AppCsiiServerApplication.main(AppCsiiServerApplication.java:26) Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'deviceService': Bean with name 'deviceService' has been injected into other beans [transFlowService] 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 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:622) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeanByName(AbstractAutowireCapableBeanFactory.java:452) at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:526) at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:496) at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:636) at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:180) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:321) ... 17 common frames omitted Disconnected from the target VM, address: '127.0.0.1:54794', transport: 'socket'

原因:
该异常时因为spring的ioc容易注入中出现循环依赖情况报错,就是ServiceA 引用 ServiceB,ServiceB又引用了 ServiceA ,如果代码这样写,spring在初始化的时候根本不知道要把哪个bean装入容器,导致bean加载失败
解决方法一:
所以在使用spring 开发时候代码设计的时候,要做到解耦,抽出公共部分新建一个service,SericeA只有一个方法引用到了ServiceB,所以我把这个方法写进了ServiceB,然后去掉了@Autowire ServiceB,这样就不会报错啦。
解决方法二:
循环依赖的地方使用@Lazy注解表示注入的bean延迟加载。

spring循环引用异常:in its raw version as part of a circular reference, but has eventually been wrapped相关推荐

  1. has been injected into other beans[XXXXXXXXXX] in its raw version as part of a circular reference

    最近项目在重构,代码层面的优化,然后从svn更新代码之后,启动tomcat报出下面这个异常 找了很久,试了很多方法,比如懒加载,没作用,查找spring配置文件,也没问题,最后发现了spring的循环 ...

  2. android 循环引用,spring循环引用

    在配置shiro的时候出了这么个问题: org.springframework.beans.factory.BeanCreationException: Error creating bean wit ...

  3. Spring循环引用-@Async注解启动报错,而@Transactional则不会

    目录 一.问题描述 二.问题分析 1.代理类 2.代理过程 三.结论 上接<Spring循环引用-@Async注解启动报错分析> 一.问题描述 Bean1Service注解@Async 启 ...

  4. Bean with name ‘‘ has been injected into other beans [] in its raw version as part of a circular ref

    Bean with name '你自己项目中A类' has been injected into other beans [你自己项目中类另外的B类] in its raw version as pa ...

  5. spring生命周期七个过程_Spring杂文(三)Spring循环引用

    众所周知spring在默认单例的情况下是支持循环引用的 Appconfig.java类的代码 @Configurable @ComponentScan("com.sadow") p ...

  6. spring 源码_spring源码系列(一)——spring循环引用

    众所周知spring在默认单例的情况下是支持循环引用的 为了节省图片大小我把那些可以动得gif图片做成了只循环一次,如果看到图片不动了请右键选择在新标签打开,那么图片就会动,手机用户则更简单,直接手指 ...

  7. Spring 循环引用 ——理解singleton与prototype初始化的区别

    所谓的循环引用,就是A依赖B,B又依赖A,A与B两个对象相互持有.像下面这种情况: class A {B b;public A(B b) {this.b=b;} }class B {A a;publi ...

  8. spring三级缓存以及@Async产生循环引用

    spring三级缓存以及@Async产生循环引用 spring三级缓存介绍 三级缓存解除循环引用原理 源码对应 1.获取A,从三级缓存中获取,没有获取到 2.构造A,将A置入三级缓存 构造A(创建A实 ...

  9. 通过循环引用问题来分析Spring源码

    本文主要分析Spring的循环依赖问题.开始正文之前,我们需要定义这2个类.LoopReferenceA中引用LoopReferenceB,LoopReferenceB中引用LoopReference ...

最新文章

  1. stm32的rxne和idle中断_HAL库的STM32F767的DMA通过IDLE中断接收数据但不能访问
  2. mysql + Fluently NHibernate + WebAPI + Autofac
  3. 学习jQuery顺便学习下CSS选择器:奇偶匹配nth-child(even)
  4. python导入csv数据例子-使用python读取csv文件快速插入数据库的实例
  5. 数组的一些与遍历相关的方法总结
  6. PHP foreach遍历数组(多种方式)
  7. .post与 .ajax,关于post与ajax post的数据类型
  8. 朴素贝叶斯实现分类_关于朴素贝叶斯分类及其实现的简短教程
  9. SpringMVC遇到的问题——GET http://localhost/spring_mvc_war_exploded/js/jquery-3.3.1.js net::ERR_ABORTED 404
  10. 学习笔记14-C语言-小项目-通讯录
  11. 如果有这样一台服务器……
  12. 256qam调制星座图_科普:5G是怎么调制的?
  13. 前端使用工具sublime text 3下载
  14. TreeNode.trage的使用
  15. 【DATAGUARD】物理dg在主库丢失归档文件的情况下的恢复(七)
  16. Android学习Matrix对称变换5
  17. 实验7 寻址方式在结构化数据访问中的应用
  18. SVN 分支与主干的合并
  19. 居然可以一键删除微博内容
  20. [乡土民间故事_徐苟三传奇]第十五回_拦县令写诗救难民

热门文章

  1. [C语言编程入门]带参数宏定义
  2. Java订单编号生成
  3. python获取企鹅电竞弹幕信息
  4. 虚拟机安装Win7(创建虚拟机)
  5. ps2015安装失败解决方案
  6. BZOJ3850: ZCC Loves Codefires
  7. 4379(c++)衣柜里总共有 N 件衣服依次排列,第 i 件衣服的编号为 i。 第 i 件衣服有它的美观值 ai 和当前的穿戴次数 bi,每件衣服的优先值为 ai / (bi+1) ,向下取整 。
  8. 计算机专业大学生应该在大学四年踏实学哪些东西?(2021 年 3 月持续更新)
  9. H5APP中监听返回事件处理
  10. 【Python数据可视化】使用geoplotlib绘制地理空间数据