Java出现@Autowired注入bean产生循环依赖part of a circular reference解决方案
问题背景
我有两个类相互注入了对方的bean,产生了bean的循环依赖
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-03-25 09:09:44.889 ERROR [main] o.s.b.SpringApplication.reportFailure(SpringApplication.java:837): Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'computeController': Unsatisfied dependency expressed through field 'computeService'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'computeService': Bean with name 'computeService' has been injected into other beans [flowService] 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.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422)
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:226)
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:893)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
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:1237)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at com.adayun.dataxcompute.DataxComputeApplication.main(DataxComputeApplication.java:18)
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'computeService': Bean with name 'computeService' has been injected into other beans [flowService] 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.
问题产生
@Service
public class A{
@Autowire
B b;
}
@Service
public class B{
@Autowire
A a;
}
解决方案
我们可以把两个类公用的方法单独写一个类,然后两个类引入公共bean就好了
@Service
public class A{
@Autowire
C c;
}
@Service
public class B{
@Autowire
C c;
}
@Service
public class C{
}
作为程序员第 83 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …