{"id":11643,"date":"2023-11-28T22:29:15","date_gmt":"2023-11-28T14:29:15","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=11643"},"modified":"2023-12-11T11:49:54","modified_gmt":"2023-12-11T03:49:54","slug":"spring-zhong-shen-me-shi-chuang-jianbean-he-zhuang","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhong-shen-me-shi-chuang-jianbean-he-zhuang\/","title":{"rendered":"Spring\u4e2d\u4ec0\u4e48\u662f\u521b\u5efabean\u548c\u88c5\u914dbean\u6709\u4ec0\u4e48\u533a\u522b\uff1f"},"content":{"rendered":"<p>\u5728Spring\u6846\u67b6\u4e2d\uff0c\u521b\u5efaBean\u548c\u88c5\u914dBean\u662f\u4e24\u4e2a\u5173\u952e\u7684\u6982\u5ff5\uff0c\u5b83\u4eec\u5206\u522b\u6d89\u53ca\u5230Bean\u7684\u5b9e\u4f8b\u5316\u548c\u4f9d\u8d56\u6ce8\u5165\u3002<\/p>\n<h3><a id=\"1%E5%88%9B%E5%BB%BA-bean%EF%BC%88creating-bean%EF%BC%89%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>1. \u521b\u5efaBean\uff08Creating Bean\uff09\uff1a<\/h3>\n<p>\u521b\u5efaBean\u662f\u6307\u5728Spring\u5bb9\u5668\u4e2d\u5b9e\u4f8b\u5316\u4e00\u4e2aJava\u5bf9\u8c61\u7684\u8fc7\u7a0b\u3002Spring\u6846\u67b6\u652f\u6301\u591a\u79cd\u65b9\u5f0f\u6765\u521b\u5efaBean\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u6784\u9020\u5668\u5b9e\u4f8b\u5316\uff08Constructor-Based\uff09\uff1a<\/strong> \u901a\u8fc7\u8c03\u7528Bean\u7c7b\u7684\u6784\u9020\u5668\u6765\u521b\u5efaBean\u3002<\/li>\n<li><strong>\u5de5\u5382\u65b9\u6cd5\u5b9e\u4f8b\u5316\uff08Factory Method\uff09\uff1a<\/strong> \u4f7f\u7528\u9759\u6001\u5de5\u5382\u65b9\u6cd5\u6216\u5b9e\u4f8b\u5de5\u5382\u65b9\u6cd5\u6765\u521b\u5efaBean\u3002<\/li>\n<li><strong>\u5de5\u5382Bean\u5b9e\u4f8b\u5316\uff08FactoryBean\uff09\uff1a<\/strong> \u4f7f\u7528\u5b9e\u73b0\u4e86<code>org.springframework.beans.factory.FactoryBean<\/code>\u63a5\u53e3\u7684\u5de5\u5382Bean\u6765\u521b\u5efaBean\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre><code class=\"language-java\">\/\/ \u6784\u9020\u5668\u5b9e\u4f8b\u5316\npublic class MyBean {\n    \/\/ ...\n}\n\n\/\/ \u5de5\u5382\u65b9\u6cd5\u5b9e\u4f8b\u5316\npublic class MyBeanFactory {\n    public static MyBean createInstance() {\n        return new MyBean();\n    }\n}\n\n\/\/ \u5de5\u5382Bean\u5b9e\u4f8b\u5316\npublic class MyBeanFactoryBean implements FactoryBean&lt;MyBean&gt; {\n    @Override\n    public MyBean getObject() throws Exception {\n        return new MyBean();\n    }\n\n    @Override\n    public Class&lt;?&gt; getObjectType() {\n        return MyBean.class;\n    }\n}\n<\/code><\/pre>\n<h3><a id=\"2%E8%A3%85%E9%85%8D-bean%EF%BC%88wiring-bean%EF%BC%89%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>2. \u88c5\u914dBean\uff08Wiring Bean\uff09\uff1a<\/h3>\n<p>\u88c5\u914dBean\u662f\u6307\u5c06Bean\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u5efa\u7acb\u8d77\u6765\uff0c\u786e\u4fdd\u4e00\u4e2aBean\u53ef\u4ee5\u8bbf\u95ee\u5230\u5b83\u6240\u4f9d\u8d56\u7684\u5176\u4ed6Bean\u3002Spring\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0Bean\u4e4b\u95f4\u7684\u4f9d\u8d56\u6ce8\u5165\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u6784\u9020\u5668\u6ce8\u5165\uff08Constructor Injection\uff09\uff1a<\/strong> \u901a\u8fc7Bean\u7c7b\u7684\u6784\u9020\u5668\u6765\u8fdb\u884c\u4f9d\u8d56\u6ce8\u5165\u3002<\/li>\n<li><strong>Setter\u6ce8\u5165\uff08Setter Injection\uff09\uff1a<\/strong> \u901a\u8fc7Bean\u7c7b\u7684Setter\u65b9\u6cd5\u6765\u8fdb\u884c\u4f9d\u8d56\u6ce8\u5165\u3002<\/li>\n<li><strong>\u5b57\u6bb5\u6ce8\u5165\uff08Field Injection\uff09\uff1a<\/strong> \u76f4\u63a5\u901a\u8fc7\u5b57\u6bb5\uff08\u5c5e\u6027\uff09\u8fdb\u884c\u4f9d\u8d56\u6ce8\u5165\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre><code class=\"language-java\">\/\/ \u6784\u9020\u5668\u6ce8\u5165\npublic class MyService {\n    private MyDependency myDependency;\n\n    public MyService(MyDependency myDependency) {\n        this.myDependency = myDependency;\n    }\n}\n\n\/\/ Setter\u6ce8\u5165\npublic class MyService {\n    private MyDependency myDependency;\n\n    public void setMyDependency(MyDependency myDependency) {\n        this.myDependency = myDependency;\n    }\n}\n\n\/\/ \u5b57\u6bb5\u6ce8\u5165\npublic class MyService {\n    @Autowired\n    private MyDependency myDependency;\n}\n<\/code><\/pre>\n<h3><a id=\"%E5%8C%BA%E5%88%AB%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u533a\u522b\uff1a<\/h3>\n<ul>\n<li>\n<strong>\u521b\u5efaBean\u5173\u6ce8\u5bf9\u8c61\u7684\u5b9e\u4f8b\u5316\uff1a<\/strong> \u521b\u5efaBean\u4e3b\u8981\u5173\u6ce8\u5982\u4f55\u5b9e\u4f8b\u5316\u4e00\u4e2aJava\u5bf9\u8c61\uff0c\u5373\u901a\u8fc7\u6784\u9020\u5668\u3001\u5de5\u5382\u65b9\u6cd5\u6216\u5de5\u5382Bean\u7b49\u65b9\u5f0f\u521b\u5efaBean\u7684\u5b9e\u4f8b\u3002\n<\/li>\n<li>\n<strong>\u88c5\u914dBean\u5173\u6ce8\u4f9d\u8d56\u7684\u6ce8\u5165\uff1a<\/strong> \u88c5\u914dBean\u5173\u6ce8Bean\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u5373\u5982\u4f55\u5c06\u4e00\u4e2aBean\u6240\u4f9d\u8d56\u7684\u5176\u4ed6Bean\u6ce8\u5165\u5230\u5b83\u7684\u5c5e\u6027\u3001\u6784\u9020\u5668\u6216\u5b57\u6bb5\u4e2d\u3002\n<\/li>\n<\/ul>\n<p>\u5728Spring\u4e2d\uff0cBean\u7684\u521b\u5efa\u548c\u88c5\u914d\u662f\u5bc6\u5207\u76f8\u5173\u7684\u8fc7\u7a0b\uff0cSpring\u5bb9\u5668\u5728\u521b\u5efaBean\u7684\u540c\u65f6\uff0c\u4f1a\u901a\u8fc7\u4f9d\u8d56\u6ce8\u5165\u5c06\u76f8\u5173\u7684Bean\u88c5\u914d\u8d77\u6765\uff0c\u786e\u4fdd\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u7684\u5bf9\u8c61\u56fe\u8c31\u662f\u5b8c\u5907\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Spring\u6846\u67b6\u4e2d\uff0c\u521b\u5efaBean\u548c\u88c5\u914dBean\u662f\u4e24\u4e2a\u5173\u952e\u7684\u6982\u5ff5\uff0c\u5b83\u4eec\u5206\u522b\u6d89\u53ca\u5230Bean\u7684\u5b9e\u4f8b\u5316\u548c\u4f9d\u8d56\u6ce8\u5165\u3002  [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[],"class_list":["post-11643","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=11643"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11643\/revisions"}],"predecessor-version":[{"id":17100,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11643\/revisions\/17100"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=11643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=11643"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=11643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}