{"id":203547,"date":"2025-05-22T09:43:08","date_gmt":"2025-05-22T01:43:08","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203547\/"},"modified":"2025-05-22T09:43:08","modified_gmt":"2025-05-22T01:43:08","slug":"spring-%e4%b8%adfactorybean-%e4%b8%8e-new-%e5%ae%9e%e4%be%8b%e5%8c%96%e5%af%b9%e8%b1%a1%e7%9a%84%e5%8c%ba%e5%88%ab%ef%bc%9a%e4%bd%95%e6%97%b6%e5%88%9b%e5%bb%ba%e3%80%81%e8%b0%81%e6%9d%a5%e7%ae%a1","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203547\/","title":{"rendered":"Spring \u4e2dFactoryBean \u4e0e new \u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u533a\u522b\uff1a\u4f55\u65f6\u521b\u5efa\u3001\u8c01\u6765\u7ba1\u7406\u3001\u5982\u4f55\u533a\u5206\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Spring \u4e2dFactoryBean \u4e0e new \u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u533a\u522b\uff1a\u4f55\u65f6\u521b\u5efa\u3001\u8c01\u6765\u7ba1\u7406\u3001\u5982\u4f55\u533a\u5206\uff1f<\/h1>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300aSpring \u4e2dFactoryBean \u4e0e new \u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u533a\u522b\uff1a\u4f55\u65f6\u521b\u5efa\u3001\u8c01\u6765\u7ba1\u7406\u3001\u5982\u4f55\u533a\u5206\uff1f\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241111\/17313086466731ac666a28e.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>factorybean\u4e0enew\u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u533a\u522b\u4e0e\u5b9e\u73b0<\/strong><\/p>\n<p>\u5728 spring \u4e2d\uff0c\u4f7f\u7528 @factorybean \u6ce8\u89e3\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a factorybean\uff0c\u5b83\u8d1f\u8d23\u521b\u5efa\u548c\u7ba1\u7406 bean \u5b9e\u4f8b\u3002\u4e0e\u76f4\u63a5\u4f7f\u7528 new \u5b9e\u4f8b\u5316\u5bf9\u8c61\u76f8\u6bd4\uff0cfactorybean \u6709\u4ee5\u4e0b\u533a\u522b\uff1a<\/p>\n<p><strong>bean \u7684\u521b\u5efa\u65f6\u673a<\/strong><\/p>\n<ul>\n<li>new \u5b9e\u4f8b\u5316\u5bf9\u8c61\uff1a\u5728 bean \u88ab\u4f7f\u7528\u65f6\u7acb\u5373\u521b\u5efa\u3002<\/li>\n<li>factorybean\uff1a\u5728\u5bb9\u5668\u542f\u52a8\u65f6\u521b\u5efa\uff0c\u5e76\u7f13\u5b58\u521b\u5efa\u7684 bean \u5b9e\u4f8b\u3002<\/li>\n<\/ul>\n<p><strong>bean \u7684\u7ba1\u7406<\/strong><\/p>\n<ul>\n<li>new \u5b9e\u4f8b\u5316\u5bf9\u8c61\uff1a\u5f00\u53d1\u8005\u9700\u8981\u81ea\u5df1\u7ba1\u7406 bean \u7684\u751f\u547d\u5468\u671f\u3002<\/li>\n<li>factorybean\uff1aspring \u5bb9\u5668\u8d1f\u8d23\u7ba1\u7406 factorybean \u53ca\u5176\u521b\u5efa\u7684 bean \u5b9e\u4f8b\u7684\u751f\u547d\u5468\u671f\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u73b0factorybean<\/strong><\/p>\n<p>\u8981\u5b9e\u73b0\u4e00\u4e2a factorybean\uff0c\u9700\u8981\u5b9e\u73b0\u4ee5\u4e0b\u63a5\u53e3\uff1a<\/p>\n<pre>public interface factorybean&lt;t&gt; {\n\n    t getobject() throws exception;\n\n    class&lt;?&gt; getobjecttype();\n\n    boolean issingleton();\n}<\/pre>\n<p><strong>\u5982\u4f55\u533a\u5206 new \u548c factorybean \u521b\u5efa\u7684 bean<\/strong><\/p>\n<p>springboot \u4e2d\u63d0\u4f9b\u4e86 @primary \u6ce8\u89e3\uff0c\u53ef\u4ee5\u5c06\u7279\u5b9a bean \u6807\u8bb0\u4e3a\u4f18\u5148\u7ea7\u66f4\u9ad8\u7684\u5019\u9009\u5bf9\u8c61\u3002\u5f53\u5b58\u5728\u591a\u4e2a\u5019\u9009\u5bf9\u8c61\u65f6\uff0cspring \u4f1a\u81ea\u52a8\u9009\u62e9\u4f18\u5148\u7ea7\u66f4\u9ad8\u7684\u90a3\u4e2a\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<p>\u5047\u8bbe\u9700\u8981\u533a\u5206\u4f7f\u7528 new \u5b9e\u4f8b\u5316\u548c\u901a\u8fc7 factorybean \u521b\u5efa\u7684 a \u63a5\u53e3\u5b9e\u73b0\uff1a<\/p>\n<pre>public class test {\n\n    @primary\n    public a newa() {\n        return new b();\n    }\n\n    @bean\n    public factorybean&lt;a&gt; factorybeana() {\n        return new factorybean&lt;a&gt;() {\n            @override\n            public a getobject() throws exception {\n                return new c();\n            }\n\n            @override\n            public class&lt;?&gt; getobjecttype() {\n                return a.class;\n            }\n\n            @override\n            public boolean issingleton() {\n                return true;\n            }\n        };\n    }\n}<\/pre>\n<p>\u5728 main \u65b9\u6cd5\u4e2d\u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165\u83b7\u53d6 a \u63a5\u53e3\u7684\u5b9e\u73b0\uff1a<\/p>\n<pre>@Autowired\nprivate A a;<\/pre>\n<p>\u6b64\u65f6\uff0ca \u6307\u5411\u7684\u5c06\u662f\u901a\u8fc7 factorybean \u521b\u5efa\u7684 c \u7c7b\u5b9e\u4f8b\uff0c\u56e0\u4e3a newa() \u65b9\u6cd5\u88ab\u6807\u8bb0\u4e3a @primary\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aSpring \u4e2dFactoryBean \u4e0e new \u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u533a\u522b\uff1a\u4f55\u65f6\u521b\u5efa\u3001\u8c01\u6765\u7ba1\u7406\u3001\u5982\u4f55\u533a\u5206\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring \u4e2dFactoryB&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203547","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203547"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203547\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}