Problem description

Using the @Async annotation when beanA and beanB are circularly referenced will throw an error.

Reasons

The bean with @Async annotation is created as a proxy instance during initialization, and the bean that is retrieved from the cache of the earlier singleton is still the original instance.

Solution

When the bean is fetched from the cache of the earlier singleton, it will be created as a proxy instance. The initialization phase will be checked and processing will be skipped if the proxy instance has already been created.

Source Code

Error message

``` org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'beanA': Bean with name 'beanA' has been injected into other beans [beanB] 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:622) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]

Comment From: pivotal-cla

@wangzhengsi Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

Comment From: pivotal-cla

@wangzhengsi Thank you for signing the Contributor License Agreement!

Comment From: sbrannen

Hi @wangzhengsi,

Congratulations on submitting your first PR to the Spring Framework! 👍

We'll look into it.


As a side note, I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference.

Comment From: wangzhengsi

Start the project to see the error message https://github.com/wangzhengsi/0321Code