{"id":64433,"date":"2025-05-03T17:00:49","date_gmt":"2025-05-03T09:00:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64433\/"},"modified":"2025-05-03T17:00:49","modified_gmt":"2025-05-03T09:00:49","slug":"spring-%e4%b8%ad%e7%9a%84-bean-%e5%88%9b%e5%bb%ba%ef%bc%9afactorybean-%e5%92%8c%e7%9b%b4%e6%8e%a5%e5%ae%9e%e4%be%8b%e5%8c%96%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab%ef%bc%9f-3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64433\/","title":{"rendered":"Spring \u4e2d\u7684 bean \u521b\u5efa\uff1aFactoryBean \u548c\u76f4\u63a5\u5b9e\u4f8b\u5316\u6709\u4ec0\u4e48\u533a\u522b\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173061324446051.jpg\" class=\"aligncenter\" title=\"Spring \u4e2d\u7684 bean \u521b\u5efa\uff1aFactoryBean \u548c\u76f4\u63a5\u5b9e\u4f8b\u5316\u6709\u4ec0\u4e48\u533a\u522b\uff1f\u63d2\u56fe\" alt=\"Spring \u4e2d\u7684 bean \u521b\u5efa\uff1aFactoryBean \u548c\u76f4\u63a5\u5b9e\u4f8b\u5316\u6709\u4ec0\u4e48\u533a\u522b\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>factorybean vs \u76f4\u63a5\u5b9e\u4f8b\u5316\uff1aspring \u4e2d\u7684 bean \u521b\u5efa\u533a\u5206<\/strong><\/p>\n<p>\u5728 spring \u4e2d\uff0c\u901a\u8fc7\u6ce8\u89e3\u65b9\u5f0f\u521b\u5efa bean \u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 factorybean \u6216\u76f4\u63a5\u5b9e\u4f8b\u5316\u4e24\u79cd\u65b9\u5f0f\u3002\u90a3\u4e48\uff0c\u5b83\u4eec\u4e4b\u95f4\u6709\u4ec0\u4e48\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u4ee3\u7801\u4e2d\u533a\u5206\u5462\uff1f<\/p>\n<p><strong>factorybean<\/strong><\/p>\n<p>factorybean \u662f\u4e00\u79cd factory bean\uff0c\u5b83\u7528\u4e8e\u521b\u5efa\u5176\u4ed6 bean\u3002factorybean \u672c\u8eab\u662f\u4e00\u4e2a bean\uff0c\u4f46\u5b83\u4e0d\u76f4\u63a5\u521b\u5efa\u76ee\u6807 bean\uff0c\u800c\u662f\u521b\u5efa\u4e00\u4e2a\u5de5\u5382\uff0c\u7528\u4e8e\u751f\u4ea7\u76ee\u6807 bean\u3002<\/p>\n<p><strong>\u76f4\u63a5\u5b9e\u4f8b\u5316<\/strong><\/p>\n<p>\u76f4\u63a5\u5b9e\u4f8b\u5316\u662f\u6307\u901a\u8fc7\u4f7f\u7528 new \u5173\u952e\u5b57\u76f4\u63a5\u521b\u5efa bean \u7684\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u5f0f\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a bean \u5b9e\u4f8b\uff0c\u800c\u4e0d\u9700\u8981\u989d\u5916\u7684\u5de5\u5382\u673a\u5236\u3002<\/p>\n<p><strong>\u533a\u5206\u65b9\u6cd5<\/strong><\/p>\n<p>\u60f3\u8981\u533a\u5206 factorybean \u548c\u76f4\u63a5\u5b9e\u4f8b\u5316\u7684 bean\uff0c\u53ef\u4ee5\u67e5\u770b\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li> <strong>\u5de5\u5382\u7c7b\u578b\uff1a<\/strong>factorybean \u7684\u7c7b\u578b\u4e3a org.springframework.beans.factory.factorybean\uff0c\u800c\u76f4\u63a5\u5b9e\u4f8b\u5316\u7684 bean \u5219\u6ca1\u6709\u8fd9\u4e2a\u7c7b\u3002<\/li>\n<li> <strong>\u6784\u9020\u65b9\u6cd5\uff1a<\/strong>factorybean \u5728\u521d\u59cb\u5316\u65f6\u4f1a\u6ce8\u5165\u4e00\u4e2a\u5b9e\u73b0\u4e86 factorybean \u63a5\u53e3\u7684\u6784\u9020\u65b9\u6cd5\uff0c\u800c\u76f4\u63a5\u5b9e\u4f8b\u5316\u7684 bean \u5219\u6ca1\u6709\u8fd9\u4e2a\u6784\u9020\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5728\u7ed9\u5b9a\u7684\u793a\u4f8b\u4e2d\uff1a<\/p>\n<pre>A b = new B();  \/\/ \u76f4\u63a5\u5b9e\u4f8b\u5316\nA c = new C();  \/\/ \u76f4\u63a5\u5b9e\u4f8b\u5316<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4e24\u4e2a\u90fd\u662f\u76f4\u63a5\u5b9e\u4f8b\u5316\u7684 bean\uff0c\u56e0\u4e3a\u6ca1\u6709\u4f7f\u7528 factorybean\u3002<\/p>\n<p>spring \u4e2d\u8fd8\u652f\u6301\u901a\u8fc7\u6ce8\u89e3\u65b9\u5f0f\u4f7f\u7528 factorybean\uff0c\u4f8b\u5982 @bean \u6ce8\u89e3\u3002\u4f7f\u7528 @bean \u6ce8\u89e3\u5e76\u6307\u5b9a factorybean \u7c7b\u578b\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a factorybean\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSpring \u4e2d\u7684 bean \u521b\u5efa\uff1aFactoryBean \u548c\u76f4\u63a5\u5b9e\u4f8b\u5316\u6709\u4ec0\u4e48\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>factorybean vs \u76f4\u63a5\u5b9e\u4f8b\u5316\uff1aspring \u4e2d\u7684 bean \u521b\u5efa\u533a\u5206 \u5728 spring \u4e2d\uff0c\u901a\u8fc7\u6ce8\u89e3\u65b9\u5f0f\u521b\u5efa bean \u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 factorybean \u6216\u76f4\u63a5\u5b9e\u4f8b\u5316\u4e24\u79cd\u65b9\u5f0f\u3002\u90a3\u4e48\uff0c\u5b83\u4eec\u4e4b\u95f4\u6709\u4ec0\u4e48\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u4ee3\u7801\u4e2d\u533a\u5206\u5462\uff1f factorybean factorybean \u662f\u4e00\u79cd factory bean\uff0c\u5b83\u7528\u4e8e\u521b\u5efa\u5176\u4ed6 bean\u3002factorybean \u672c\u8eab\u662f\u4e00\u4e2a bean\uff0c\u4f46\u5b83\u4e0d\u76f4\u63a5\u521b\u5efa\u76ee\u6807 bean\uff0c\u800c\u662f\u521b\u5efa\u4e00\u4e2a\u5de5\u5382\uff0c\u7528\u4e8e\u751f\u4ea7\u76ee\u6807 bean\u3002 \u76f4\u63a5\u5b9e\u4f8b\u5316 \u76f4\u63a5\u5b9e\u4f8b\u5316\u662f\u6307\u901a\u8fc7\u4f7f\u7528 new \u5173\u952e\u5b57\u76f4\u63a5\u521b\u5efa bean \u7684\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u5f0f\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a bean \u5b9e\u4f8b\uff0c\u800c\u4e0d\u9700\u8981\u989d\u5916\u7684\u5de5\u5382\u673a\u5236\u3002 \u533a\u5206\u65b9\u6cd5 \u60f3\u8981\u533a\u5206 factorybean \u548c\u76f4\u63a5\u5b9e\u4f8b\u5316\u7684 bean\uff0c\u53ef\u4ee5\u67e5\u770b\u4ee5\u4e0b\u51e0\u70b9\uff1a \u5de5\u5382\u7c7b\u578b\uff1afactorybean \u7684\u7c7b\u578b\u4e3a org.springframework.beans.factory.factorybean\uff0c\u800c\u76f4\u63a5\u5b9e\u4f8b\u5316\u7684 bean \u5219\u6ca1\u6709\u8fd9\u4e2a\u7c7b\u3002 \u6784\u9020\u65b9\u6cd5\uff1afactorybean \u5728\u521d\u59cb\u5316\u65f6\u4f1a\u6ce8\u5165\u4e00\u4e2a\u5b9e\u73b0\u4e86 factorybean \u63a5\u53e3\u7684\u6784\u9020\u65b9\u6cd5\uff0c\u800c\u76f4\u63a5\u5b9e\u4f8b\u5316\u7684 bean \u5219\u6ca1\u6709\u8fd9\u4e2a\u6784\u9020\u65b9\u6cd5\u3002 \u793a\u4f8b \u5728\u7ed9\u5b9a\u7684\u793a\u4f8b\u4e2d\uff1a A b = new B(); \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-64433","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=64433"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64433\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}