Spring bean循环依赖问题: Bean with name '..' has been injected into other be

时间:2022-11-05 15:56:32

Spring boot启动报错: 

Bean with name 'bpmProcessInstanceApiImpl' has been injected into other beans [bpmProcessInstanceServiceImpl] 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 'getBeanNamesForType' with the 'allowEagerInit' flag turned off

看起来像是过早的初始化了, 不过后面分析发现没那么简单.


详细错误日志如下: 


2022-11-05 15:01:08.470 | INFO 9812 | main [TID: N/A] o.f.e.impl.cmd.ValidateV5EntitiesCmd | Total of v5 deployments found: 0
2022-11-05 15:01:10.883 | WARN 9812 | main [TID: N/A] ConfigServletWebServerApplicationContext | Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'bpmProcessInstanceApiImpl': Bean with name 'bpmProcessInstanceApiImpl' has been injected into other beans [bpmProcessInstanceServiceImpl] 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 'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.
2022-11-05 15:01:11.697 | INFO 9812 | main [TID: N/A] o.s.s.quartz.SchedulerFactoryBean | Shutting down Quartz Scheduler
2022-11-05 15:01:11.697 | INFO 9812 | main [TID: N/A] org.quartz.core.QuartzScheduler | Scheduler schedulerName_$_LAPTOP-MIP39M3P1667631653171 shutting down.
2022-11-05 15:01:11.697 | INFO 9812 | main [TID: N/A] org.quartz.core.QuartzScheduler | Scheduler schedulerName_$_LAPTOP-MIP39M3P1667631653171 paused.
2022-11-05 15:01:11.930 | INFO 9812 | main [TID: N/A] org.quartz.core.QuartzScheduler | Scheduler schedulerName_$_LAPTOP-MIP39M3P1667631653171 shutdown complete.
2022-11-05 15:01:11.941 | INFO 9812 | main [TID: N/A] c.b.d.d.DynamicRoutingDataSource | dynamic-datasource start closing ....
2022-11-05 15:01:11.958 | INFO 9812 | main [TID: N/A] com.alibaba.druid.pool.DruidDataSource | {dataSource-2} closing ...
2022-11-05 15:01:11.965 | INFO 9812 | main [TID: N/A] com.alibaba.druid.pool.DruidDataSource | {dataSource-2} closed
2022-11-05 15:01:11.966 | INFO 9812 | main [TID: N/A] com.alibaba.druid.pool.DruidDataSource | {dataSource-1} closing ...
2022-11-05 15:01:11.967 | INFO 9812 | main [TID: N/A] com.alibaba.druid.pool.DruidDataSource | {dataSource-1} closed
2022-11-05 15:01:11.968 | INFO 9812 | main [TID: N/A] c.b.d.d.DynamicRoutingDataSource | dynamic-datasource all closed success,bye
2022-11-05 15:01:12.180 | INFO 9812 | main [TID: N/A] o.apache.catalina.core.StandardService | Stopping service [Tomcat]
2022-11-05 15:01:12.218 | INFO 9812 | main [TID: N/A] ConditionEvaluationReportLoggingListener |

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-11-05 15:01:12.330 | ERROR 9812 | main [TID: N/A] o.s.boot.SpringApplication | Application run failed

org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'bpmProcessInstanceApiImpl': Bean with name 'bpmProcessInstanceApiImpl' has been injected into other beans [bpmProcessInstanceServiceImpl] 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 'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:649)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:745)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:420)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1317)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
at cn.iocoder.yudao.server.YudaoServerApplication.main(YudaoServerApplication.java:24)

2022-11-05 15:04:51.806 | INFO 9812 | Apollo-DBConfigRepository-1 [TID: N/A] c.i.y.f.a.internals.DBConfigRepository | [loadConfigIfUpdate][增量加载全量配置]
2022-11-05 15:04:51.924 | INFO 9812 | Apollo-DBConfigRepository-1 [TID: N/A] c.i.y.f.a.internals.DBConfigRepository | [sync][缓存配置,数量为:6]

注意到启动的日志中, 前面部分出现

2022-11-05 15:00:04.889 |  INFO 9812 | main [TID: N/A] o.s.c.a.ConfigurationClassPostProcessor  | Cannot enhance @Configuration bean definition 'com.ctrip.framework.apollo.spring.boot.ApolloAutoConfiguration' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.


难道是 过早初始化导致的?  


其实不是的, 因为那个其实也仅仅是个 info日志.  那个不是问题根本原因.


真正的原因是bean互相依赖, 导致无法创建,  如下图:

Spring bean循环依赖问题: Bean with name '..' has been injected into other be

然后:

Spring bean循环依赖问题: Bean with name '..' has been injected into other be


如何解决:

把 BpmProcessInstanceServiceImpl 中的下面语句删除即可!


@Resource
private BpmProcessInstanceApi processInstanceApi;