{"id":11415,"date":"2023-11-28T22:12:52","date_gmt":"2023-11-28T14:12:52","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=11415"},"modified":"2023-12-11T10:46:32","modified_gmt":"2023-12-11T02:46:32","slug":"spring-zhong-zi-dong-zhuang-pei-de-fang-shi-you-na","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhong-zi-dong-zhuang-pei-de-fang-shi-you-na\/","title":{"rendered":"Spring\u4e2d\u81ea\u52a8\u88c5\u914d\u7684\u65b9\u5f0f\u6709\u54ea\u4e9b\uff1f"},"content":{"rendered":"<p>\u5728Spring\u6846\u67b6\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8fdb\u884c\u81ea\u52a8\u88c5\u914d\uff08Autowiring\uff09\u3002\u81ea\u52a8\u88c5\u914d\u662f\u6307Spring\u5bb9\u5668\u6839\u636e\u4e00\u5b9a\u89c4\u5219\uff0c\u81ea\u52a8\u5730\u5728\u5bb9\u5668\u4e2d\u4e3abean\u7684\u5c5e\u6027\u8d4b\u503c\u3002\u4ee5\u4e0b\u662fSpring\u4e2d\u81ea\u52a8\u88c5\u914d\u7684\u51e0\u79cd\u65b9\u5f0f\uff1a<\/p>\n<ol>\n<li>\n<strong>\u81ea\u52a8\u88c5\u914d\u6a21\u5f0f\uff08Autowiring Modes\uff09\uff1a<\/strong><\/p>\n<ul>\n<li><strong>no<\/strong>: \u9ed8\u8ba4\u6a21\u5f0f\uff0c\u4e0d\u8fdb\u884c\u81ea\u52a8\u88c5\u914d\uff0c\u9700\u8981\u624b\u52a8\u6307\u5b9a\u4f9d\u8d56\u5173\u7cfb\u3002<\/li>\n<li><strong>byName<\/strong>: \u6839\u636e\u5c5e\u6027\u540d\u79f0\u81ea\u52a8\u88c5\u914d\uff0c\u5bb9\u5668\u4e2d\u7684bean\u7684\u540d\u79f0\u5fc5\u987b\u548c\u5c5e\u6027\u540d\u79f0\u76f8\u540c\u3002<\/li>\n<li><strong>byType<\/strong>: \u6839\u636e\u5c5e\u6027\u7684\u6570\u636e\u7c7b\u578b\u81ea\u52a8\u88c5\u914d\uff0c\u5bb9\u5668\u4e2d\u5fc5\u987b\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u5339\u914d\u7684bean\u3002<\/li>\n<li><strong>constructor<\/strong>: \u901a\u8fc7\u6784\u9020\u51fd\u6570\u81ea\u52a8\u88c5\u914d\uff0c\u7c7b\u4f3c\u4e8ebyType\uff0c\u4f46\u5e94\u7528\u4e8e\u6784\u9020\u51fd\u6570\u53c2\u6570\u3002<\/li>\n<\/ul>\n<p>\u4f60\u53ef\u4ee5\u5728bean\u7684\u5b9a\u4e49\u4e2d\u4f7f\u7528<code>autowire<\/code>\u5c5e\u6027\u6765\u8bbe\u7f6e\u81ea\u52a8\u88c5\u914d\u6a21\u5f0f\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;bean id=&quot;exampleBean&quot; class=&quot;com.example.ExampleBean&quot; autowire=&quot;byName&quot;\/&gt;\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u4f7f\u7528<code>@Autowired<\/code>\u6ce8\u89e3\uff1a<\/strong><br \/>\n\u5728\u7c7b\u7684\u5b57\u6bb5\u3001\u6784\u9020\u51fd\u6570\u3001Setter\u65b9\u6cd5\u4e0a\u4f7f\u7528<code>@Autowired<\/code>\u6ce8\u89e3\uff0cSpring\u4f1a\u81ea\u52a8\u5728\u5bb9\u5668\u4e2d\u67e5\u627e\u5339\u914d\u7684bean\u8fdb\u884c\u6ce8\u5165\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-java\">public class ExampleClass {\n    @Autowired\n    private AnotherBean anotherBean;\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u4f7f\u7528<code>@Resource<\/code>\u6ce8\u89e3\uff1a<\/strong><br \/>\n\u7c7b\u4f3c\u4e8e<code>@Autowired<\/code>\uff0c<code>@Resource<\/code>\u4e5f\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u81ea\u52a8\u88c5\u914d\uff0c\u4f46\u5b83\u66f4\u6ce8\u91cd\u540d\u79f0\u800c\u975e\u7c7b\u578b\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>name<\/code>\u5c5e\u6027\u6307\u5b9a\u8981\u6ce8\u5165\u7684bean\u7684\u540d\u79f0\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-java\">public class ExampleClass {\n    @Resource(name = &quot;anotherBean&quot;)\n    private AnotherBean anotherBean;\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u4f7f\u7528Java\u914d\u7f6e\u7c7b\uff1a<\/strong><br \/>\n\u5728\u4f7f\u7528Java\u914d\u7f6e\u7c7b\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>@Autowired<\/code>\u6ce8\u89e3\u8fdb\u884c\u81ea\u52a8\u88c5\u914d\u3002\u540c\u65f6\uff0cSpring\u63d0\u4f9b\u4e86<code>@Configuration<\/code>\u548c<code>@ComponentScan<\/code>\u7b49\u6ce8\u89e3\uff0c\u5e2e\u52a9\u4f60\u914d\u7f6e\u548c\u626b\u63cf\u81ea\u52a8\u88c5\u914d\u7684bean\u3002\n<\/li>\n<\/ol>\n<p>\u8fd9\u4e9b\u65b9\u5f0f\u53ef\u4ee5\u5355\u72ec\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u7ec4\u5408\u4f7f\u7528\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u9879\u76ee\u7684\u9700\u6c42\u548c\u8bbe\u8ba1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Spring\u6846\u67b6\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8fdb\u884c\u81ea\u52a8\u88c5\u914d\uff08Autowiring\uff09\u3002\u81ea\u52a8\u88c5\u914d\u662f\u6307Spring\u5bb9\u5668\u6839\u636e\u4e00\u5b9a [&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-11415","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11415","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=11415"}],"version-history":[{"count":3,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11415\/revisions"}],"predecessor-version":[{"id":17043,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11415\/revisions\/17043"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=11415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=11415"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=11415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}