(Bean A和B循环依赖,A中的方法带有@Async注解,系统环境不一样会出现本地启动OK,测试环境或其他环境无法启动,报错.BeanCurrentlyInCreationException)
Controller和Service都包含了同一个Component!这个Bean中有@Async注解的方法,由于它与generateUtilbean循环依赖所致!
2023-01-12 14:20:18.256 ERROR [][][] [ main] c.m.c.RemittanceApplication 28 : application start error
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cipsBookingController': Unsatisfied dependency expressed through field 'bookingService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookingService': Unsatisfied dependency expressed through field 'emailUtil'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'emailUtil': Bean with name 'emailUtil' has been injected into other beans [generalUtil] 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:584)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:370)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1336)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:548)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230)
at com.mdb.cips.CipsRemittanceApplication.main(CipsRemittanceApplication.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookingService': Unsatisfied dependency expressed through field 'emailUtil'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'emailUtil': Bean with name 'emailUtil' has been injected into other beans [generalUtil] 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:584)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:370)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1336)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1135)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:581)
... 27 common frames omitted
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'emailUtil': Bean with name 'emailUtil' has been injected into other beans [generalUtil] 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:602)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1135)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:581)
... 40 common frames omitted
普通的代理对象去进行循环依赖是完全没问题的,那么同样的spring对异步调用的实现也是使用的代理对象去实现的,为什么异步代理循环依赖却失败了呢?
假设此时的A加上了@Async注解,那么在第二次实例化A的时候是通过A的对象工厂去创建的,也就是通过调用了AbstractAutoProxyCreator的wrapIfNecessary方法返回并作为B对象的属性A的值,正常来说B此时是可以拿到A的异步代理对象的,但是遗憾的是,异步代理对象并不是通过AbstractAutoProxyCreator这个BeanPostProcessor去创建的,而是通过AsyncAnnotationBeanPostProcessor去创建,而AsyncAnnotationBeanPostProcessor却并不是SmartInstantiationAwareBeanPostProcessor类型的,所以在getEarlyBeanReference方法中是调用不到的,也就是说对象工厂无法返回一个异步代理对象,所以B注入的是一个A的原始对象。
当回到第一次实例化A的时候,在执行一系列的BeanPostProcessor的时候,此时会遍历到AsyncAnnotationBeanPostProcessor,进而调用它的后置处理方法返回一个异步代理对象,然后此时重点就来了!回到最后上面最后的一段代码,此时getSingleton返回的对象就是一个A的原始对象,所以earlySingletonReference不等于null,然后exposedObject就是A的异步代理对象,exposedObject == bean并不成立,所以代码跳转到else if,默认的allowRawInjectionDespiteWrapping等于false,所以else if成立,最终就会报BeanCurrentlyInCreationException这个循环依赖的错误。
总结:
异步代理不是基于AbstractAutoProxyCreator进行实现的,而是重新使用了另外的一个BeanPostProcessor去进行实现的(AsyncAnnotationBeanPostProcessor),为什么不使用基于AbstractAutoProxyCreator的方式就会出现循环依赖的错误呢?因为解决循环依赖的一个关键点在与实例化一个bean的时候提前暴露出一个对象工厂,而这个对象工厂最终返回的只是针对基于AbstractAutoProxyCreator的方式实现的代理对象,所以异步代理用它自己的方式去实现异步代理的话就出现循环依赖的错误
参考:解决办法
解决,
本地环境OK,线上环境起不来
根源在于在不同的操作系统或者环境下, bean 的加载顺序是不固定的。bean 加载顺序变化之后,就可能会导致循环依赖报错问题产生!因为,顺序变化之后,循环依赖的主体变了!
bean 加载时,会先将所有的 BeanDefinition 扫描出来,扫描出来的顺序基本上就决定了 bean 的加载顺序。
扫描 BeanDefiniton 的方法是 ClassPathScanningCandidateComponentProvider#scanCandidateComponents(),这个方法在不同的环境下扫描出类的顺序是不固定的。
它的底层走的是 java.lang.ClassLoader#getResources() ,这个方法没有承诺获取到资源文件的顺序!
// PathMatchingResourcePatternResolver#doFindAllClassPathResources
protected Set doFindAllClassPathResources(String path) throws IOException {
Set result = new LinkedHashSet<>(16);
ClassLoader cl = getClassLoader();
Enumeration resourceUrls = (cl != null ? cl.getResources(path) : ClassLoader.getSystemResources(path));
while (resourceUrls.hasMoreElements()) {
URL url = resourceUrls.nextElement();
result.add(convertClassLoaderURL(url));
}
if (!StringUtils.hasLength(path)) {
// The above result is likely to be incomplete, i.e. only containing file system references.
// We need to have pointers to each of the jar files on the classpath as well...
addAllClassLoaderJarRoots(cl, result);
}
return result;
}