{"id":12396,"date":"2023-11-28T22:52:32","date_gmt":"2023-11-28T14:52:32","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=12396"},"modified":"2023-12-11T17:07:27","modified_gmt":"2023-12-11T09:07:27","slug":"spring-zhongautowired-zhu-jie-you-shen-me-yong","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhongautowired-zhu-jie-you-shen-me-yong\/","title":{"rendered":"Spring\u4e2d@Autowired\u6ce8\u89e3\u6709\u4ec0\u4e48\u7528\uff1f"},"content":{"rendered":"<p><code>@Autowired<\/code> \u6ce8\u89e3\u662f Spring \u6846\u67b6\u4e2d\u7528\u4e8e\u8fdb\u884c\u81ea\u52a8\u88c5\u914d\uff08\u81ea\u52a8\u6ce8\u5165\uff09\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u5b83\u53ef\u4ee5\u6807\u8bb0\u5728\u7c7b\u7684\u5b57\u6bb5\u3001\u6784\u9020\u65b9\u6cd5\u3001Setter \u65b9\u6cd5\u7b49\u4f4d\u7f6e\uff0c\u7528\u4e8e\u544a\u8bc9 Spring \u5bb9\u5668\u5728\u521b\u5efa Bean \u7684\u8fc7\u7a0b\u4e2d\uff0c\u81ea\u52a8\u5c06\u5339\u914d\u7684\u4f9d\u8d56\u5bf9\u8c61\u6ce8\u5165\u5230\u6807\u8bb0\u4e86 <code>@Autowired<\/code> \u7684\u4f4d\u7f6e\u3002<\/p>\n<p><code>@Autowired<\/code> \u6ce8\u89e3\u7684\u4f5c\u7528\u5305\u62ec\uff1a<\/p>\n<ol>\n<li>\n<strong>\u81ea\u52a8\u88c5\u914dBean\uff1a<\/strong><\/p>\n<ul>\n<li>\u5f53 Spring \u5bb9\u5668\u5728\u5b9e\u4f8b\u5316\u4e00\u4e2a Bean \u7684\u65f6\u5019\uff0c\u5982\u679c\u53d1\u73b0\u8be5 Bean \u7684\u67d0\u4e2a\u5c5e\u6027\u4e0a\u6807\u8bb0\u4e86 <code>@Autowired<\/code>\uff0c\u5bb9\u5668\u4f1a\u5c1d\u8bd5\u81ea\u52a8\u5bfb\u627e\u4e0e\u8be5\u5c5e\u6027\u7c7b\u578b\u5339\u914d\u7684 Bean\uff0c\u5e76\u5c06\u5176\u6ce8\u5165\u5230\u6807\u8bb0\u4e86 <code>@Autowired<\/code> \u7684\u5c5e\u6027\u4e2d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>\u6d88\u9664\u7e41\u7410\u7684\u624b\u52a8\u6ce8\u5165\uff1a<\/strong><\/p>\n<ul>\n<li><code>@Autowired<\/code> \u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u6d88\u9664\u624b\u52a8\u8fdb\u884c\u5c5e\u6027\u6ce8\u5165\u7684\u7e41\u7410\u6b65\u9aa4\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>\u89e3\u51b3\u4f9d\u8d56\u5bf9\u8c61\u7684\u6b67\u4e49\u6027\uff1a<\/strong><\/p>\n<ul>\n<li>\u5f53\u5bb9\u5668\u4e2d\u5b58\u5728\u591a\u4e2a\u4e0e\u88ab\u6ce8\u5165\u5c5e\u6027\u7c7b\u578b\u5339\u914d\u7684 Bean \u65f6\uff0c<code>@Autowired<\/code> \u53ef\u4ee5\u4e0e <code>@Qualifier<\/code> \u6ce8\u89e3\u4e00\u8d77\u4f7f\u7528\uff0c\u6307\u5b9a\u5177\u4f53\u8981\u6ce8\u5165\u7684 Bean\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>\u6784\u9020\u65b9\u6cd5\u6ce8\u5165\uff1a<\/strong><\/p>\n<ul>\n<li><code>@Autowired<\/code> \u8fd8\u53ef\u4ee5\u7528\u4e8e\u6784\u9020\u65b9\u6cd5\u4e0a\uff0c\u7528\u4e8e\u8fdb\u884c\u6784\u9020\u65b9\u6cd5\u6ce8\u5165\uff0c\u5373\u5728\u521b\u5efa Bean \u65f6\u901a\u8fc7\u6784\u9020\u65b9\u6cd5\u6ce8\u5165\u76f8\u5173\u4f9d\u8d56\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3><a id=\"%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528\u793a\u4f8b\uff1a<\/h3>\n<pre><code class=\"language-java\">public class MyService {\n    private MyRepository myRepository;\n\n    \/\/ \u6784\u9020\u65b9\u6cd5\u6ce8\u5165\n    @Autowired\n    public MyService(MyRepository myRepository) {\n        this.myRepository = myRepository;\n    }\n\n    \/\/ Setter \u65b9\u6cd5\u6ce8\u5165\n    @Autowired\n    public void setMyRepository(MyRepository myRepository) {\n        this.myRepository = myRepository;\n    }\n\n    \/\/ \u5b57\u6bb5\u6ce8\u5165\n    @Autowired\n    private AnotherService anotherService;\n\n    \/\/ \u5176\u4ed6\u65b9\u6cd5\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>MyService<\/code> \u7c7b\u4e2d\u7684 <code>myRepository<\/code> \u548c <code>anotherService<\/code> \u90fd\u4f7f\u7528\u4e86 <code>@Autowired<\/code> \u6ce8\u89e3\uff0c\u544a\u8bc9 Spring \u5bb9\u5668\u8981\u81ea\u52a8\u6ce8\u5165\u8fd9\u4e24\u4e2a\u5c5e\u6027\u3002<code>MyService<\/code> \u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u4e5f\u4f7f\u7528\u4e86 <code>@Autowired<\/code> \u6ce8\u89e3\uff0c\u8868\u793a\u5728\u521b\u5efa <code>MyService<\/code> Bean \u7684\u65f6\u5019\u8981\u901a\u8fc7\u6784\u9020\u65b9\u6cd5\u6ce8\u5165\u76f8\u5173\u4f9d\u8d56\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>@Autowired \u6ce8\u89e3\u662f Spring \u6846\u67b6\u4e2d\u7528\u4e8e\u8fdb\u884c\u81ea\u52a8\u88c5\u914d\uff08\u81ea\u52a8\u6ce8\u5165\uff09\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u5b83\u53ef\u4ee5\u6807\u8bb0\u5728\u7c7b\u7684\u5b57\u6bb5 [&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-12396","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12396","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=12396"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12396\/revisions"}],"predecessor-version":[{"id":47901,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12396\/revisions\/47901"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=12396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=12396"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=12396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}