当前位置: 首页 > 工具软件 > ER Framework > 使用案例 >

springboot项目出错org.springframework.beans.factory.UnsatisfiedDependencyException

岳俊雅
2023-12-01

springboot项目出错org.springframework.beans.factory.UnsatisfiedDependencyException

Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'analyzeController': Unsatisfied dependency expressed through field 'analyzeService'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'analyzeServiceImpl': Bean with name 'analyzeServiceImpl' has been injected into other beans [asyncServiceImpl] 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.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:397)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1429)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
	at com.yueshui.rsvrforecast.DemoApplication.main(DemoApplication.java:28)
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'analyzeServiceImpl': Bean with name 'analyzeServiceImpl' has been injected into other beans [asyncServiceImpl] 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:624)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1287)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:636)
	... 19 common frames omitted
	

解决方式是,在引入这个service的地方加上@Lazy看注解。如下图。

@Autowired
    private FloodRepository floodRepository;

    @Autowired
    private RainstormRepository rainstormRepository;

    @Autowired
    private ForecastService forecastService;

    @Lazy
    @Autowired
    private AnalyzeService analyzeService;

    @Autowired
    private LSFloodService lSFloodService;

    @Autowired
    private TrackService trackService;

网上很多解决方式都没说过用@Lazy解决这个问题,百度搜索大部分都说仔细检查检查一下哪个bean上面没写@service这种废话。
其实那段异常翻译成中文(用百度翻译)得到如下内容

应用程序运行失败
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“analyzeController”的bean时出错:通过字段“analyzeService”表达的不满意依赖;嵌套异常是 org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为 'analyzeServiceImpl' 的 bean 时出错:名为 'analyzeServiceImpl' 的 Bean 已作为循环引用的一部分注入到其原始版本中的其他 bean [asyncServiceImpl] 中,但是最终被包裹。这意味着所述其他 bean 不使用该 bean 的最终版本。这通常是过于急切的类型匹配的结果 - 例如,考虑使用 'getBeanNamesOfType' 并关闭 'allowEagerInit' 标志。
在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639)
在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116)
在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:397)
在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1429)
在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
在 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
在 org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
在 com.yueshui.rsvrforecast.DemoApplication.main(DemoApplication.java:28)
引起:org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为“analyzeServiceImpl”的bean时出错:名为“analyzeServiceImpl”的bean已作为循环引用的一部分注入到其原始版本中的其他bean [asyncServiceImpl]中,但已注入最终被包裹。这意味着所述其他 bean 不使用该 bean 的最终版本。这通常是过于急切的类型匹配的结果 - 例如,考虑使用 'getBeanNamesOfType' 并关闭 'allowEagerInit' 标志。
在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:624)
在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
在 org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1287)
在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:636)
... 省略了 19 个常用帧

通过上面翻译后的仅有的几句中文,也能大概知道原因,即这个bean互相依赖了,别的类引了这个bean,这个bean下面引用的又间接引用了别的bean。
当知道这个原因后,所以解决方式可以有很多种,@Lazy只是一个解决方式而已,也可以试试上面翻译中提到的那句话,然后百度找一下怎么实现这个解决办法。

考虑使用 ‘getBeanNamesOfType’ 并关闭 ‘allowEagerInit’ 标志。

 类似资料: