{"id":10006996,"date":"2022-12-21T13:54:38","date_gmt":"2022-12-21T05:54:38","guid":{"rendered":"https:\/\/ityet.com\/?p=10006996"},"modified":"2022-12-21T13:54:38","modified_gmt":"2022-12-21T05:54:38","slug":"%e4%bb%8e%e6%ba%90%e7%a0%81%e5%b1%82%e9%9d%a2%e6%b7%b1%e5%ba%a6%e5%89%96%e6%9e%90spring%e5%be%aa%e7%8e%af%e4%be%9d%e8%b5%96","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006996","title":{"rendered":"\u4ece\u6e90\u7801\u5c42\u9762\u6df1\u5ea6\u5256\u6790Spring\u5faa\u73af\u4f9d\u8d56"},"content":{"rendered":"<p><strong>\u4f5c\u8005\uff1a\u90ed\u8273\u7ea2<\/strong><\/p>\n<p><code>\u4ee5\u4e0b\u4e3e\u4f8b\u7686\u9488\u5bf9\u5355\u4f8b\u6a21\u5f0f\u8ba8\u8bba<\/code><\/p>\n<p>\u56fe\u89e3\u53c2\u8003 <a href=\"https:\/\/link.segmentfault.com\/?enc=X6GbLTF6N5ZW1WG48f1f2A%3D%3D.t7zyr7rvD%2BehEyRsskQ%2FlN%2Bc454q207RjJQQVGybCCQPwLHcbKwMbM9MW7Xi80Q%2BO1ex1eE8KDfJaxlmz40bAg%3D%3D\" rel=\"nofollow\">https:\/\/www.processon.com\/vie&#8230;<\/a><\/p>\n<h2><strong>1\u3001Spring \u5982\u4f55\u521b\u5efaBean\uff1f<\/strong><\/h2>\n<p>\u5bf9\u4e8e\u5355\u4f8bBean\u6765\u8bf4\uff0c\u5728Spring\u5bb9\u5668\u6574\u4e2a\u751f\u547d\u5468\u671f\u5185\uff0c\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n<p>Spring \u5728\u521b\u5efa Bean \u8fc7\u7a0b\u4e2d\uff0c\u4f7f\u7528\u5230\u4e86\u4e09\u7ea7\u7f13\u5b58\uff0c\u5373 DefaultSingletonBeanRegistry.java \u4e2d\u5b9a\u4e49\u7684\uff1a<\/p>\n<pre><code>    \/** Cache of singleton objects: bean name to bean instance. *\/\n    private final Map&lt;String, Object&gt; singletonObjects = new ConcurrentHashMap&lt;&gt;(256);\n\u200b\n    \/** Cache of singleton factories: bean name to ObjectFactory. *\/\n    private final Map&lt;String, ObjectFactory&lt;?&gt;&gt; singletonFactories = new HashMap&lt;&gt;(16);\n\u200b\n    \/** Cache of early singleton objects: bean name to bean instance. *\/\n    private final Map&lt;String, Object&gt; earlySingletonObjects = new ConcurrentHashMap&lt;&gt;(16);<\/code><\/pre>\n<p>\u4ee5 com.gyh.general \u5305\u4e0b\u7684 OneBean \u4e3a\u4f8b\uff0cdebug springboot \u542f\u52a8\u8fc7\u7a0b\uff0c\u5206\u6790spring\u662f\u5982\u4f55\u521b\u5efabean\u7684\u3002<\/p>\n<p>\u53c2\u8003\u56fe\u4e2d <strong>spring\u521b\u5efabean<\/strong> \u7684\u8fc7\u7a0b\u3002\u5176\u4e2d\u6700\u5173\u952e\u7684\u51e0\u6b65\u6709\uff1a<\/p>\n<p>1.<code>getSingleton(beanName, true)<\/code> \u4f9d\u6b21\u4ece\u4e00\u4e8c\u4e09\u7ea7\u7f13\u5b58\u4e2d\u67e5\u627ebean\u5bf9\u8c61\uff0c\u5982\u679c\u7f13\u5b58\u4e2d\u5b58\u5728\u5bf9\u8c61\uff0c\u5219\u76f4\u63a5\u8fd4\u56de(early)\uff1b<\/p>\n<p>2.<code>createBeanInstance(beanName, mbd, args)<\/code> \u9009\u4e00\u4e2a\u5408\u9002\u7684\u6784\u9020\u51fd\u6570\uff0cnew\u5b9e\u4f8b\u5bf9\u8c61(instance)\uff0c\u6b64\u65f6\u7684instance\u4e2d\u4f9d\u8d56\u7684\u5c5e\u6027\u8fd8\u90fd\u662fnull\uff0c\u5c5e\u4e8e\u534a\u6210\u54c1\uff1b<\/p>\n<p>3.<code>singletonFactories.put(beanName, oneSingletonFactory)<\/code> \u5229\u7528\u4e0a\u4e00\u6b65\u7684instance\uff0c\u6784\u5efa\u4e00\u4e2a singletonFactory\uff0c\u5e76\u5c06\u5176\u653e\u5230\u4e09\u7ea7\u7f13\u5b58\u4e2d\uff1b<\/p>\n<p>4.<code>populateBean(beanName, mbd, instanceWrapper)<\/code> \u586b\u5145bean\uff1a\u4e3a\u8be5bean\u5b9a\u4e49\u7684\u5c5e\u6027\u521b\u5efa\u5bf9\u8c61\u6216\u8d4b\u503c\uff1b<\/p>\n<p>5.<code>initializeBean(&quot;one&quot;,oneInstance, mbd)<\/code> \u521d\u59cb\u5316bean\uff1a\u5bf9bean\u8fdb\u884c\u521d\u59cb\u5316\u6216\u5176\u4ed6\u52a0\u5de5\uff0c\u5982\u751f\u6210\u4ee3\u7406\u5bf9\u8c61(proxy)\uff1b<\/p>\n<p>6.<code>getSingleton(beanName, false)<\/code> \u4f9d\u6b21\u5728\u4e00\u4e8c\u7ea7\u7f13\u5b58\u4e2d\u67e5\u627e\uff0c\u68c0\u67e5\u662f\u5426\u6709\u56e0\u5faa\u73af\u4f9d\u8d56\u5bfc\u81f4\u63d0\u524d\u751f\u6210\u7684\u5bf9\u8c61\uff0c\u6709\u7684\u8bdd\u4e0e\u521d\u59cb\u5316\u540e\u7684\u5bf9\u8c61\u662f\u5426\u4e00\u81f4\uff1b<\/p>\n<h2><strong>2\u3001Spring \u5982\u4f55\u89e3\u51b3\u5faa\u73af\u4f9d\u8d56\uff1f<\/strong><\/h2>\n<p>\u4ee5 com.gyh.circular.threeCache \u5305\u4e0b\u7684 OneBean \u548c TwoBean \u4e3a\u4f8b \uff0c\u4e24\u4e2a Bean \u76f8\u4e92\u4f9d\u8d56\uff08\u5373\u5f62\u6210\u95ed\u73af\uff09\u3002<\/p>\n<p>\u53c2\u8003\u56fe\u4e2d <strong>spring\u89e3\u51b3\u5faa\u73af\u4f9d\u8d56<\/strong> \u7684\u8fc7\u7a0b\u53ef\u77e5\uff0cspring\u5229\u7528\u4e09\u7ea7\u7f13\u4e2d\u7684 objectFactory \u751f\u6210\u5e76\u8fd4\u56de\u4e00\u4e2a early \u5bf9\u8c61\uff0c\u63d0\u524d\u66b4\u9732\u8fd9\u4e2a early \u5730\u5740\uff0c\u4f9b\u5176\u4ed6\u5bf9\u8c61\u4f9d\u8d56\u6ce8\u5165\u4f7f\u7528\uff0c\u4ee5\u6b64\u89e3\u51b3\u5faa\u73af\u4f9d\u8d56\u95ee\u9898\u3002<\/p>\n<h2><strong>3\u3001Spring \u4e0d\u80fd\u89e3\u51b3\u54ea\u4e9b\u5faa\u73af\u4f9d\u8d56\uff1f<\/strong><\/h2>\n<h3><strong>3.1 \u5faa\u73af\u4e2d\u4f7f\u7528\u4e86 @Async \u6ce8\u89e3<\/strong><\/h3>\n<h4><strong>3.1.1 \u4e3a\u4ec0\u4e48\u5faa\u73af\u4e2d\u4f7f\u7528\u4e86 @Async \u4f1a\u62a5\u9519\uff1f<\/strong><\/h4>\n<p>\u4ee5 com.gyh.circular.async \u5305\u4e0b\u7684 OneBean \u548c TwoBean \u4e3a\u4f8b\uff0c\u4e24\u4e2abean\u76f8\u4e92\u4f9d\u8d56\uff0c\u4e14oneBean\u4e2d\u7684\u65b9\u6cd5\u4f7f\u7528\u4e86 @Async \u6ce8\u89e3\uff0c\u6b64\u65f6\u542f\u52a8spring\u5931\u8d25\uff0c\u62a5\u9519\u4fe1\u606f\u4e3a\uff1a<code>org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'a.one': Bean with name 'a.one' has been injected into other beans [a.two] 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.<\/code><\/p>\n<p>\u5e76\u901a\u8fc7debug\u4ee3\u7801\uff0c\u53d1\u73b0\u62a5\u9519\u4f4d\u7f6e\u5728 AbstractAutowireCapableBeanFactory#doCreateBean \u65b9\u6cd5\u5185\uff0c\u7531\u4e8e earlySingletonReference != null \u4e14 exposedObject != bean\uff0c\u5bfc\u81f4\u62a5\u9519\u3002<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043130580\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u7ed3\u5408\u6d41\u7a0b\u56fe\u4e2d <strong>spring\u89e3\u51b3\u5faa\u73af\u4f9d\u8d56<\/strong> \u53ca\u4e0a\u8ff0\u56fe\u7247\u4e2d\u53ef\u77e5\uff1a<\/p>\n<p>1.\u884c1\u4e2d bean \u4e3a createBeanInstance \u521b\u5efa\u7684\u5b9e\u4f8b(address1)<\/p>\n<p>2.\u884c2\u4e2d exposedObject \u4e3a initializeBean \u540e\u751f\u6210\u7684\u4ee3\u7406\u5bf9\u8c61(address2)<\/p>\n<p>3.\u884c3\u4e2d earlySingletonReference \u4e3a getEarlyBeanReference \u65f6\u521b\u5efa\u7684\u5bf9\u8c61\u3010\u6b64\u5904\u5730\u5740\u540cbean(address1)\u3011<\/p>\n<p>\u6df1\u5c42\u539f\u56e0\u4e3a\uff1a\u5148\u524d TwoBean \u5728 populateBean \u65f6\u5df2\u7ecf\u4f9d\u8d56\u4e86\u5730\u5740\u4e3a address1 \u7684 earlySingletonReference \u5bf9\u8c61\uff0c\u800c\u6b64\u65f6 OneBean \u7ecf\u8fc7 initializeBean \u4e4b\u540e\uff0c\u8fd4\u56de\u4e86\u5730\u5740\u4e3a address2 \u7684\u65b0\u5bf9\u8c61\uff0c\u5bfc\u81f4spring\u4e0d\u77e5\u9053\u54ea\u4e2a\u624d\u662f\u6700\u7ec8\u7248\u7684bean\uff0c\u6240\u4ee5\u62a5\u9519\u3002<\/p>\n<p>earlySingletonReference \u662f\u5982\u4f55\u751f\u6210\u7684\uff0c\u53c2\u8003getSingleton(&#8220;one&#8221;, true)\u8fc7\u7a0b\u3002<\/p>\n<h4><strong>3.1.2 \u5faa\u73af\u4e2d\u4f7f\u7528\u4e86 @Async \u4e00\u5b9a\u4f1a\u62a5\u9519\u5417\uff1f<\/strong><\/h4>\n<p>\u4f9d\u7136\u4ee5 com.gyh.circular.async \u5305\u4e0b\u7684 OneBean \u548c TwoBean \u4e3a\u4f8b\uff0c\u4e24\u4e2abean\u76f8\u4e92\u4f9d\u8d56\uff0c\u4f7f TwoBean(\u975eOneBean)\u4e2d\u7684\u65b9\u6cd5\u4f7f\u7528\u4e86 @Async \u6ce8\u89e3\uff0c\u6b64\u65f6\u542f\u52a8spring\u6210\u529f\uff0c\u5e76\u672a\u62a5\u9519\u3002<\/p>\n<p>debug\u4ee3\u7801\u53ef\u77e5\uff1a\u867d\u7136TwoBean \u4f7f\u7528\u4e86 @Async \u6ce8\u89e3\uff0c\u4f46\u5176 earlySingletonReference = null; \u6545\u4e0d\u4f1a\u5f15\u8d77\u62a5\u9519\u3002<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043130581\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u6df1\u5c42\u539f\u56e0\u4e3a\uff1aOneBean \u5148\u88ab\u521b\u5efa\uff0cTwoBean \u540e\u521b\u5efa\uff0c\u518d\u6574\u6761\u94fe\u8def\u4e2d\uff0c\u5e76\u672a\u5728\u4e09\u7ea7\u7f13\u5b58\u4e2d\u67e5\u627e\u8fc7 TwoBean \u7684 objectFactory \u3002\uff08OneBean\u5728\u521b\u5efa\u8fc7\u7a0b\u4e2d\uff0c\u88ab\u627e\u8fc7\u4e24\u6b21\uff0c\u5373 one-&gt; two -&gt;one\uff1bTwoBean \u7684\u521b\u5efa\u8fc7\u7a0b\u4e2d\uff0c\u53ea\u627e\u8fc7\u5b83\u4e00\u6b21\uff0c\u5373 two -&gt;one\u3002)<\/p>\n<p>\u7531\u6b64\u53ef\u5f97\uff1a@Async \u9020\u6210\u5faa\u73af\u4f9d\u8d56\u62a5\u9519\u7684\u5148\u7ea6\u6761\u4ef6\u4e3a\uff1a<\/p>\n<p>1.\u5faa\u73af\u4f9d\u8d56\u4e2d\u7684 Bean \u4f7f\u7528\u4e86 @Async \u6ce8\u89e3<\/p>\n<p>2.\u4e14\u8fd9\u4e2a Bean\uff0c\u6bd4\u5faa\u73af\u5185\u5176\u4ed6 Bean \u5148\u521b\u5efa\u3002<\/p>\n<p>3.\u6ce8\uff1a\u4e00\u4e2aBean\u53ef\u80fd\u4f1a\u540c\u65f6\u5b58\u5728\u4e8e\u591a\u4e2a\u5faa\u73af\u5185\uff1b\u53ea\u8981\u5b58\u5728\u5b83\u662f\u67d0\u4e2a\u5faa\u73af\u5185\u7b2c\u4e00\u4e2a\u88ab\u521b\u5efa\u7684Bean\uff0c\u90a3\u4e48\u5c31\u4f1a\u62a5\u9519\u3002<\/p>\n<h4><strong>3.1.3 \u4e3a\u4ec0\u4e48\u5faa\u73af\u4e2d\u4f7f\u7528\u4e86 @Transactional \u4e0d\u4f1a\u62a5\u9519\uff1f<\/strong><\/h4>\n<p>\u5df2\u77e5\u4f7f\u7528\u4e86 @Transactional \u6ce8\u89e3\u7684 Bean\uff0cSpring \u4e5f\u4f1a\u4e3a\u5176\u751f\u6210\u4ee3\u7406\u5bf9\u8c61\uff0c\u4f46\u4e3a\u4ec0\u4e48\u8fd9\u79cd Bean \u5728\u5faa\u73af\u91cc\u65f6\u4e0d\u4f1a\u4ea7\u751f\u62a5\u9519\u5462\uff1f<\/p>\n<p>\u4ee5 com.gyh.circular.transactional \u5305\u4e0b\u7684 OneBean \u548c TwoBean \u4e3a\u4f8b\uff0c\u4e24\u4e2a Bean \u76f8\u4e92\u4f9d\u8d56\uff0c\u4e14 OneBean \u4e2d\u7684\u65b9\u6cd5\u4f7f\u7528\u4e86 @Transactional \u6ce8\u89e3\uff0c\u542f\u52a8Spring\u6210\u529f\uff0c\u5e76\u4e0d\u4f1a\u62a5\u9519\u3002<\/p>\n<p>debug \u4ee3\u7801\u53ef\u77e5\uff0c\u751f\u6210 OneBean \u8fc7\u7a0b\u4e2d\uff0c\u867d\u7136 earlySingletonReference != null\uff0c\u4f46 initializeBean \u4e4b\u540e\u7684 exposedObject \u548c \u539f\u59cb\u5b9e\u4f8b\u7684\u5730\u5740\u76f8\u540c\uff08\u5373 initializeBean \u6b65\u9aa4\u4e2d\uff0c\u5e76\u672a\u5bf9\u5b9e\u4f8b\u751f\u6210\u4ee3\u7406\uff09\uff0c\u6240\u4ee5\u4e0d\u4f1a\u4ea7\u751f\u62a5\u9519\u3002<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043130582\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<h4><strong>3.1.4 \u4e3a\u4ec0\u4e48\u540c\u6837\u662f\u4ee3\u7406\u4f1a\u4ea7\u751f\u4e24\u79cd\u4e0d\u540c\u7684\u73b0\u8c61\uff1f<\/strong><\/h4>\n<p>\u540c\u6837\u662f\u751f\u6210\u4ee3\u7406\u5bf9\u8c61\uff0c\u540c\u6837\u662f\u53c2\u4e0e\u5230\u5faa\u73af\u4f9d\u8d56\u4e2d\uff0c\u4f1a\u4ea7\u751f\u4e0d\u540c\u73b0\u8c61\u7684\u539f\u56e0\u662f\uff1a\u5f53\u4ed6\u4eec\u5904\u5728\u5faa\u73af\u4f9d\u8d56\u4e2d\u65f6\uff0c\u751f\u6210\u4ee3\u7406\u7684\u8282\u70b9\u4e0d\u540c\uff1a<\/p>\n<p>1.@Transactional \u5728 getEarlyBeanReference \u65f6\u751f\u6210\u4ee3\u7406\uff0c\u63d0\u524d\u66b4\u9732\u51fa\u4ee3\u7406\u4e4b\u540e\u7684\u5730\u5740\uff08\u5373\u6700\u7ec8\u5730\u5740\uff09\uff1b<\/p>\n<p>2.@Async \u5728 initializeBean \u65f6\u751f\u6210\u4ee3\u7406\uff0c\u5bfc\u81f4\u63d0\u524d\u66b4\u9732\u51fa\u53bb\u7684\u5730\u5740\u4e0d\u662f\u6700\u7ec8\u5730\u5740\uff0c\u9020\u6210\u62a5\u9519\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48 @Async \u4e0d\u80fd\u5728 getEarlyBeanReference \u65f6\u751f\u6210\u4ee3\u7406\u5462\uff1f\u5bf9\u6bd4\u4e0b\u4e24\u8005\u6267\u884c\u7684\u4ee3\u7801\u8fc7\u7a0b\u53d1\u73b0\uff1a<\/p>\n<p>\u4e24\u8005\u90fd\u662f\u5728 AbstractAutoProxyCreator#getEarlyBeanReference \u7684\u65b9\u6cd5\u5bf9\u539f\u59cb\u5b9e\u4f8b\u5bf9\u8c61\u8fdb\u884c\u5305\u88c5\uff0c\u5982\u4e0b\u56fe<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043130583\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u4f7f\u7528 @Transactional \u7684Bean \u5728 create proxy \u65f6\uff0c\u83b7\u53d6\u5230\u4e00\u4e2aadvice \uff0c\u968f\u5373\u751f\u6210\u4e86\u4ee3\u7406\u5bf9\u8c61 proxy.<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043130584\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u800c\u4f7f\u7528 @Async \u7684Bean \u5728 create proxy \u65f6\uff0c\u6ca1\u6709\u83b7\u53d6\u5230 advice\uff0c\u4e0d\u80fd\u88ab\u4ee3\u7406.<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043130585\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<h4><strong>3.1.5 \u4e3a\u4ec0\u4e48@Async \u5728 getEarlyBeanReference \u65f6\u4e0d\u80fd\u8fd4\u56de\u4e00\u4e2a advice\uff1f<\/strong><\/h4>\n<p>\u5728 AbstractAutoProxyCreator#getAdvicesAndAdvisorsForBean \u65b9\u6cd5\u5185\uff0c\u5176\u4e3b\u8981\u505a\u7684\u4e8b\u60c5\u6709\uff1a<\/p>\n<p>1.\u627e\u5230\u5f53\u524d spring \u5bb9\u5668\u4e2d\u6240\u6709\u7684 Advisor<\/p>\n<p>2.\u8fd4\u56de\u9002\u914d\u5f53\u524d bean \u7684\u6240\u6709 Advisor<\/p>\n<p>\u7b2c\u4e00\u6b65\u8fd4\u56de\u7684 Advisor \u6709 BeanFactoryCacheOperationSourceAdvisor \u548c BeanFactoryTransactionAttributeSourceAdvisor\uff0c\u5e76\u65e0\u5904\u7406 Async \u76f8\u5173\u7684 Advisor.<\/p>\n<p>\u5228\u6839\u95ee\u5e95\uff0c\u8ffd\u67e5\u4e3a\u4ec0\u4e48\u7b2c\u4e00\u6b65\u4e0d\u4f1a\u8fd4\u56de\u5904\u7406 Async \u76f8\u5173\u7684 Advisor\uff1f<\/p>\n<p>\u5df2\u77e5\u4f7f\u7528 @Async @Transactional @Cacheable \u9700\u8981\u63d0\u524d\u8fdb\u884c\u5f00\u542f\uff0c\u5373\u63d0\u524d\u6807\u6ce8 @EnableAsync\u3001@EnableTransactionManagement\u3001@EnableCaching \u3002<\/p>\n<p>\u4ee5 @EnableTransactionManagement\u3001@EnableCaching \u4e3a\u4f8b\uff0c\u5728\u5176\u6ce8\u89e3\u5b9a\u4e49\u4e2d\uff0c\u5f15\u5165\u4e86Selector\u7c7b\uff0cSelector\u4e2d\u53c8\u5f15\u5165\u4e86Configuration \u7c7b\uff0c\u5728 Configuration \u7c7b\u4e2d\uff0c\u521b\u5efa\u4e86\u5bf9\u5e94 Advisor \u5e76\u653e\u5230\u4e86 spring\u5bb9\u5668\u4e2d\uff0c\u6240\u4ee5\u7b2c\u4e00\u6b65\u624d\u80fd\u5f97\u5230\u8fd9\u4e24\u4e2a Advisor.<\/p>\n<p>\u800c @EnableAsync\u7684\u5b9a\u4e49\u4e2d\u5f15\u5165\u7684 Configuration \u7c7b\uff0c\u521b\u5efa\u7684\u662f AsyncAnnotationBeanPostProcessor \u5e76\u975e\u4e00\u4e2a Advisor\uff0c\u6240\u4ee5\u7b2c\u4e00\u6b65\u4e0d\u4f1a\u5f97\u5230\u5b83\uff0c\u6240\u4ee5 @Async \u7684 bean \u4e0d\u4f1a\u5728\u8fd9\u4e00\u6b65\u88ab\u4ee3\u7406\u3002<\/p>\n<h3><strong>3.2 \u6784\u9020\u51fd\u6570\u5f15\u8d77\u7684\u5faa\u73af\u4f9d\u8d56<\/strong><\/h3>\n<p>\u4ee5 com.gyh.circular.constructor \u5305\u4e0b\u7684 OneBean \u548c TwoBean \u4e3a\u4f8b\uff0c\u4e24\u4e2a\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\u5404\u81ea\u4f9d\u8d56\u5bf9\u65b9\uff0c\u542f\u52a8spring\uff0c\u62a5\u9519\uff1a<code>org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'c.one': Requested bean is currently in creation: Is there an unresolvable circular reference?<\/code><\/p>\n<p>debug \u4ee3\u7801\u53ef\u77e5\uff0c\u4e24\u4e2abean\u5728\u6839\u636e\u6784\u9020\u51fd\u6570 new instance \u65f6\uff0c\u5c31\u5df2\u7ecf\u9677\u5165\u7684\u6b7b\u5faa\u73af\uff0c\u65e0\u6cd5\u63d0\u524d\u66b4\u9732\u53ef\u7528\u7684\u5730\u5740\uff0c\u6240\u4ee5\u53ea\u80fd\u62a5\u9519\u3002<\/p>\n<\/p>\n<h2><strong>4\u3001\u5982\u4f55\u89e3\u51b3\u4ee5\u4e0a\u5faa\u73af\u4f9d\u8d56\u62a5\u9519\uff1f<\/strong><\/h2>\n<p>1.\u4e0d\u7528 @Async\uff0c\u5c06\u9700\u8981\u5f02\u6b65\u64cd\u4f5c\u7684\u65b9\u6cd5\uff0c\u653e\u5230\u7ebf\u7a0b\u6c60\u4e2d\u6267\u884c\u3002\uff08\u63a8\u8350\uff09<\/p>\n<p>2.\u63d0\u51fa @Async \u6807\u6ce8\u7684\u65b9\u6cd5\u3002\uff08\u63a8\u8350\uff09<\/p>\n<p>3.\u5c06\u4f7f\u7528 @Async \u7684\u65b9\u6cd5\u63d0\u51fa\u5230\u5355\u72ec\u7684\u7c7b\u4e2d\uff0c\u8be5\u7c7b\u53ea\u505a\u5f02\u6b65\u5904\u7406\uff0c\u4e0d\u505a\u5176\u4ed6\u4e1a\u52a1\u4f9d\u8d56\uff0c\u5373\u907f\u514d\u5f62\u6210\u5faa\u73af\u4f9d\u8d56\uff0c\u4ece\u800c\u89e3\u51b3\u62a5\u9519\u95ee\u9898\u3002\u53c2\u8003 com.gyh.circular.async.extract \u5305\u3002<\/p>\n<p>4.\u5c3d\u91cf\u4e0d\u4f7f\u7528\u6784\u9020\u51fd\u6570\u4f9d\u8d56\u5bf9\u8c61\u3002\uff08\u63a8\u8350\uff09<\/p>\n<p>5.\u7834\u574f\u5faa\u73af\uff08\u4e0d\u63a8\u8350\uff09\u5373\u4e0d\u5f62\u6210\u95ed\u73af\uff0c\u5728\u5f00\u53d1\u4e4b\u524d\uff0c\u89c4\u5212\u597d\u5bf9\u8c61\u4f9d\u8d56\uff0c\u65b9\u6cd5\u8c03\u7528\u94fe\uff0c\u5c3d\u91cf\u505a\u5230\u4e0d\u4f7f\u7528\u5faa\u73af\u4f9d\u8d56\u3002\uff08\u8f83\u96be\uff0c\u968f\u7740\u8fed\u4ee3\u5f00\u53d1\u4e0d\u65ad\u53d8\u5316\uff0c\u5f88\u53ef\u80fd\u4ea7\u751f\u5faa\u73af\uff09<\/p>\n<p>6.\u7834\u574f\u521b\u5efa\u987a\u5e8f\uff08\u4e0d\u63a8\u8350\uff09<\/p>\n<p>7.\u7531\u4e8e\u4f7f\u7528 @Async \u6ce8\u89e3\u7684\u6240\u5728\u7c7b\uff0c\u6bd4\u5faa\u73af\u4f9d\u8d56\u5185\u5176\u4ed6\u7c7b\u5148\u521b\u5efa\u65f6\u624d\u4f1a\u62a5\u9519\uff0c\u90a3\u4e48\u60f3\u529e\u6cd5\u4f7f\u8be5\u7c7b\u4e0d\u5148\u4e8e\u5176\u4ed6\u7c7b\u5148\u521b\u5efa\uff0c\u4e5f\u53ef\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u5982\uff1a@DependsOn\u3001 @Lazy<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043130578\">https:\/\/segmentfault.com\/a\/1190000043130578<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u90ed\u8273\u7ea2 \u4ee5\u4e0b\u4e3e\u4f8b\u7686\u9488\u5bf9\u5355\u4f8b\u6a21\u5f0f\u8ba8\u8bba \u56fe\u89e3\u53c2\u8003 https:\/\/www.processon.com\/vie [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006996","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006996"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006996\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}