@Async引发的BeanCurrentlyInCreationException
创始人
2025-05-28 09:08:39

异常出现条件

  1. 开启异步处理注解 @EnableAsync
  2. 代码环境
@Service
public class AyncTestImpl implements AyncTest {@Autowiredprivate HelloServiceA helloServiceA;@Async@Overridepublic void sayHello() {helloServiceA.hello();}
}
@Service
public class HelloServiceA {@Autowiredprivate AyncTest ayncTest;public void hello(){System.out.println("hello A");}}
  1. 加载顺序,这里我们得确保Spring首先实例化AyncTestImpl 然后在实例化HelloServiceA。,同一个文件夹下AyncTestImpl 在HelloServiceA 前面默认加载顺序就可以符合。
    在这里插入图片描述
    DefaultListableBeanFactorypreInstantiateSingletons() 方法
    在这里插入图片描述

出现如下异常

警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'ayncTestImpl': Bean with name 'ayncTestImpl' has been injected into other beans [helloServiceA] 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.
Exception in thread "main" org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'ayncTestImpl': Bean with name 'ayncTestImpl' has been injected into other beans [helloServiceA] 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:682)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:540)at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:369)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:283)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:367)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:905)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:943)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:591)at org.springframework.context.annotation.AnnotationConfigApplicationContext.(AnnotationConfigApplicationContext.java:108)at com.demo.Entrance.main(Entrance.java:41)

解释:: helloServiceA 注入的ayncTestImpl 不是最终的版本。我们知道ayncTestImpl的方法是需要进行异步执行,正常ayncTestImpl对象是没有这种功能的,要添加这种功能需要对ayncTestImpl对象进行代理,所以Spring有处理器对ayncTestImpl对象进行动态代理,使之具有异步方法功能。出现这个异常就是说helloServiceA 注入的ayncTestImpl的原始对象, 而Spring容器最终的aynnTestImpl的对象是进行代理之后具有异步功能的代理对象

解决方法

让Spring容器先完全的加载ayncTestImpl对象,容器注入ayncTestImpl代理对象之后再去,即可以在ayncTestImpl的注入对象加入@Lazy注解。

@Service
public class AyncTestImpl implements AyncTest {@Autowired@Lazyprivate HelloServiceA helloServiceA;@Async@Overridepublic void sayHello() {helloServiceA.hello();}
}

原理探究

设置Debug断点,我们直接看ayncTestImpl的Bean生命周期过程就行。
在这里插入图片描述

这里就不就Debug了,建议自己去Debug, 这里只总结出一个流程图,更容易接收

@Async增强时机与一般AOP增强时机的过程

在这里插入图片描述

总结

到这里,Async为什么会引发BeanCurrentlyInCreationException 就结束了,接着笔者还会出一篇博文, 探究@aync的增强原理,为什么我们开启了 @EnableAsync 注解,然后在方法上使用 @Async 就可以使用异步功能,以及如果异步方法出现了异常,Spring Async又提供了怎样获取异常处理扩展点呢?

相关内容

热门资讯

魏牌9年换了8任CEO!魏建军... 魏建军首次回应魏牌频繁换帅。12月21日,哈弗原总经理赵永坡在社交媒体上宣布:“从今天开始,我就是魏...
美国强生被判赔一女子,金额何以... 文丨吕行编辑丨杜海来源丨新商悟(本文约为800字)全球最大的保健产品跨国巨头美国强生公司,这回栽了个...
「收盘」A股再现午后拉升:国防... A股三大股指12月25日开盘涨跌互现。保险股走强,导致大小指数早盘走势分化。午后两市再度出现同频上攻...
1000元的昂跑,攻陷“Lul... 原创首发 | 金角财经(ID: F-Jinjiao)作者 | 温颖颖“雷军就这么踩着一双千元昂跑来了...
青岛银行高增利润难释分红疑云 ... 导语:2025 年前三季度以 15.54% 归母净利润增速登顶 A 股上市银行榜首的青岛银行,正陷入...