springboot启动报错Bean with name ‘xxxxService‘ has been injected into other beans


今天在本地测试启动项目的时候报错:

Bean with name ‘commonService’ has been injected into other beans [] 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.**

报错原因:
依赖循环,我现在有一个ServiceA需要调用ServiceB的方法,那么ServiceA就依赖于ServiceB,并且在ServiceB中有调用ServiceA的方法,就形成了循环依赖。Spring在初始化bean的时候就不知道先初始化哪个,bean就会报错。

循环依赖是什么?
Bean A 依赖 B,Bean B 依赖 A这种情况下出现循环依赖。
Bean A → Bean B → Bean A
更复杂的间接依赖造成的循环依赖如下。
Bean A → Bean B → Bean C → Bean D → Bean E → Bean A

循环依赖会产生什么结果?
当Spring正在加载所有Bean时,Spring尝试以能正常创建Bean的顺序去创建Bean。
例如,有如下依赖:
Bean A → Bean B → Bean C
Spring先创建beanC,接着创建bean B(将C注入B中),最后创建bean A(将B注入A中)。

但当存在循环依赖时,Spring将无法决定先创建哪个bean。这种情况下,Spring将产生异常BeanCurrentlyInCreationException。

解决办法:
1、重构代码,进行解耦
2、在注入bean时,在互相依赖的两个bean上加上@Lazy注解

 

 


————————————————
版权声明:本文为CSDN博主「ycblog.top」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_41916305/article/details/118569174


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



猜您在找 Spring循环依赖报错Bean with name '**' has been injected into other beans [**] in its raw version as part Bean with name 'xxxService' has been injected into other beans [xxxServiceA,xxxServiceB] in its raw version as part of a circular reference, but has eventually been wrapped has been injected into other beans[XXXXXXXXXX] in its raw version as part of a circular reference FeignClient 报错: A bean with that name has already been defined and overriding is disabled. idea 启动springboot项目时报错:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource Feign 报错:The bean 'service-producer.FeignClientSpecification', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled. springboot启动报错:Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zxkj.lockserver.dao.CompanyDao' available: expected at least 1 bean which qua 【报错】org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSocketHandlerMapping' defined in class path resource springboot启动报错start bean 'eurekaAutoServiceRegistration' NullPointerException A bean with that name has already been defined in class path resource [org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class] and overriding is disabled
 
粤ICP备18138465号  © 2018-2026 CODEPRJ.COM