{"id":250,"date":"2021-05-09T15:10:06","date_gmt":"2021-05-09T07:10:06","guid":{"rendered":"https:\/\/explause.com\/?p=250"},"modified":"2022-07-03T00:58:19","modified_gmt":"2022-07-02T16:58:19","slug":"spring-autowire%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/devdoge.org\/?p=250","title":{"rendered":"Spring Autowire\u914d\u7f6e"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">bean\u6807\u7b7e\u7684autowire\uff08\u81ea\u52a8\u88c5\u914d\uff09\u5c5e\u6027\u53ef\u9009\u7684\u503c\u6709\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter is-style-stripes\"><table><tbody><tr><td><strong>no<\/strong><\/td><td>\u9ed8\u8ba4\uff0c\u4e0d\u6267\u884c\u81ea\u52a8\u88c5\u914d<\/td><\/tr><tr><td><strong>byName<\/strong><\/td><td>\u901a\u8fc7setter\u65b9\u6cd5\u540d\u79f0\u6765\u786e\u5b9a<\/td><\/tr><tr><td><strong>byType<\/strong><\/td><td>\u901a\u8fc7setter\u65b9\u6cd5\u7684\u53c2\u6570\u7c7b\u578b\u786e\u5b9a<\/td><\/tr><tr><td><strong>constructor<\/strong><\/td><td>\u901a\u8fc7\u6784\u9020\u51fd\u6570\u53c2\u6570\u7c7b\u578b\u786e\u5b9a<\/td><\/tr><tr><td><strong>autodetect<\/strong><\/td><td>\u5148<em><strong>constructor<\/strong><\/em>\u7136\u540e<strong>byType<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u81ea\u52a8\u88c5\u914d\u4e5f\u6709\u4e00\u4e9b\u9650\u5236\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>&lt;constructor-arg> \u548c &lt;property>\u4f1a\u8986\u76d6autowire\u7684\u8bbe\u5b9a\u3002<\/li><li>\u4e0d\u53ef\u4ee5\u88c5\u914d\u539f\u59cb\uff08\u57fa\u672c\uff09\u6570\u636e\u7c7b\u578b\u3001\u5b57\u7b26\u4e32\u548c\u7c7b\u3002<\/li><li>\u5f88\u5bb9\u6613\u6df7\u6dc6\u3002<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u81ea\u52a8\u88c5\u914d\u662f\u8ba9\u5bb9\u5668\u5c06Bean\u5185\u7684\u53d8\u91cf\uff08\u5b57\u6bb5\u6216\u5c5e\u6027\uff09\u81ea\u52a8\u914d\u7f6e\u597d\u7684\u6280\u672f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bd4\u5982\uff1a\u5728HelloWorld\u7c7b\u91cc\u9762\u6709\u4e2a\u53d8\u91cfHelloWorld2\uff0c\u8fd9\u65f6\u5019\u53ea\u8981\u5c06HelloWorld\u6ce8\u518c\u6210Bean\u5e76\u5199\u597dsetter\u65b9\u6cd5\u6216\u8005\u6784\u9020\u51fd\u6570\u5c31\u80fd\u8ba9\u5bb9\u5668\u81ea\u52a8\u53bb\u914d\u7f6eHelloWorld2\uff0c\u5f53\u7136\u4e86\uff0c\u524d\u63d0\u662fHelloWorld2\u4e5f\u6ce8\u518c\u4e3aBean\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9e\u4f8b\uff1a<a href=\"https:\/\/explause.com\/?p=221\">\u521b\u5efa\u65b0\u9879\u76ee<\/a>\u6216\u8005<a href=\"https:\/\/explause.com\/wp-content\/uploads\/2022\/07\/AutowireExample.zip\">\u4e0b\u8f7d\u6211\u505a\u597d\u7684<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>DependencyInjectionDemo.java\u6587\u4ef6\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.explause.AutowireExamples;\r\n\r\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\r\n\r\npublic class DependencyInjectionDemo {\r\n\tpublic static void main(String&#91;] args) {\r\n\t\tClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(\"Beans.xml\");\r\n\t\tHelloWorld obj1 = (HelloWorld) context.getBean(\"helloWorld\");\r\n\t\tobj1.getMessage();\r\n\t\tcontext.close();\r\n\t}\r\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>HelloWorld.java\u6587\u4ef6\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.explause.AutowireExamples;\r\n\r\npublic class HelloWorld {\r\n\tprivate HelloWorld2 helloWorld2;\r\n\t\r\n\tpublic void setHelloWorld2(HelloWorld2 helloWorld2) {\r\n\t\tthis.helloWorld2 = helloWorld2;\r\n\t}\r\n\tpublic void getMessage() {\r\n\t\thelloWorld2.getMessage();\r\n\t}\r\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>HelloWorld2.java\u6587\u4ef6\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.explause.AutowireExamples;\r\n\r\npublic class HelloWorld2 {\r\n\tprivate String message;\r\n\tprivate String message2;\r\n\t\r\n\tpublic void setMessage(String message){\r\n\t\tthis.message  = message;\r\n\t}\r\n\tpublic void getMessage(){\r\n\t\tSystem.out.println(\"Your Message : \" + message);\r\n\t\tSystem.out.println(\"Your Message 2 : \" + message2);\r\n\t}\r\n\tpublic void setMessage2(String message2){\r\n\t\tthis.message2  = message2;\r\n\t}\r\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u53caBean\u6ce8\u518c\u6587\u4ef6Beans.xml\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n&lt;beans xmlns = \"http:\/\/www.springframework.org\/schema\/beans\"\r\n   xmlns:xsi = \"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n   xsi:schemaLocation = \"http:\/\/www.springframework.org\/schema\/beans\r\n   http:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd\">\r\n\r\n\t&lt;bean id='helloWorld' class='com.explause.AutowireExamples.HelloWorld' autowire=\"byType\"\/>\r\n\t\r\n\t&lt;bean id='helloWorld2' class='com.explause.AutowireExamples.HelloWorld2'>\r\n\t\t&lt;property name=\"message\" value='Hello World'\/>\r\n\t\t&lt;property name=\"message2\" value='Hello World 2'\/>\r\n\t&lt;\/bean>\r\n&lt;\/beans><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762id\u4e3ahelloWorld\u7684bean\u5185\u6ca1\u6709\u914d\u7f6e\u4efb\u4f55\u4e1c\u897f\uff0c\u53ea\u662f\u5728bean\u6807\u7b7e\u5185\u6dfb\u52a0\u4e86autowire\u5c5e\u6027\uff0c\u793a\u4f8b\u914d\u7f6e\u6210byType\u3002\u8fd0\u884cmain\u51fd\u6570\uff08\u65b9\u6cd5\uff09\u4e4b\u540e\u5e76\u4e0d\u4f1a\u62a5\u9519\uff0c\u800c\u662f\u663e\u793ahelloWorld2\u7684\u5185\u5bb9\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"456\" height=\"232\" src=\"https:\/\/explause.com\/wp-content\/uploads\/2022\/07\/autowire.png\" alt=\"\" class=\"wp-image-796\" srcset=\"https:\/\/devdoge.org\/wp-content\/uploads\/2022\/07\/autowire.png 456w, https:\/\/devdoge.org\/wp-content\/uploads\/2022\/07\/autowire-300x153.png 300w\" sizes=\"auto, (max-width: 456px) 100vw, 456px\" \/><figcaption>\u9700\u8981\u6ce8\u610f\u7684\u662fHelloWorld\u7c7b\u5185\u5fc5\u987b\u8981\u6709setter\u65b9\u6cd5\u3002<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528xml\u6587\u4ef6\u8fdb\u884c\u81ea\u52a8\u88c5\u914d\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[18],"class_list":["post-250","post","type-post","status-publish","format-standard","hentry","category-spring","tag-spring"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"_links":{"self":[{"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/posts\/250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/devdoge.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=250"}],"version-history":[{"count":16,"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions"}],"predecessor-version":[{"id":801,"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions\/801"}],"wp:attachment":[{"href":"https:\/\/devdoge.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devdoge.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devdoge.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}