{"id":10006159,"date":"2022-08-10T10:05:29","date_gmt":"2022-08-10T02:05:29","guid":{"rendered":"https:\/\/ityet.com\/?p=10006159"},"modified":"2022-08-10T10:05:29","modified_gmt":"2022-08-10T02:05:29","slug":"%e9%9d%a2%e8%af%95%e7%aa%81%e5%87%bb73%ef%bc%9aioc-%e5%92%8c-di-%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006159","title":{"rendered":"\u9762\u8bd5\u7a81\u51fb73\uff1aIoC \u548c DI \u6709\u4ec0\u4e48\u533a\u522b\uff1f"},"content":{"rendered":"<p>IoC \u548c DI \u90fd\u662f Spring \u6846\u67b6\u4e2d\u7684\u91cd\u8981\u6982\u5ff5\uff0c\u5c31\u50cf\u73ab\u7470\u82b1\u4e0e\u7231\u60c5\u4e00\u6837\uff0cIoC \u548c DI \u901a\u5e38\u60c5\u51b5\u4e0b\u4e5f\u662f\u6210\u5bf9\u51fa\u73b0\u7684\u3002\u90a3 IoC \u548c DI \u4ec0\u4e48\u5173\u7cfb\u548c\u533a\u522b\u5462\uff1f\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u3002<\/p>\n<h2>1.IoC \u4ecb\u7ecd<\/h2>\n<p>IoC \u662f Inversion of Control \u7684\u7f29\u5199\uff0c\u7ffb\u8bd1\u6210\u4e2d\u6587\u662f\u201c\u63a7\u5236\u53cd\u8f6c\u201d\u7684\u610f\u601d\uff0c<strong>\u5b83\u4e0d\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u6280\u672f\uff0c\u800c\u662f\u4e00\u4e2a\u5b9e\u73b0\u5bf9\u8c61\u89e3\u8026\u7684\u601d\u60f3\u3002<\/strong><\/p>\n<p>\u8981\u89e3\u91ca\u4ec0\u4e48\u662f\u89e3\u8026\uff1f\u5c31\u8981\u4e86\u89e3\u4ec0\u4e48\u662f\u8026\u5408\uff0c\u6240\u8c13\u7684<strong>\u8026\u5408\u662f\u6307\uff1a\u4e24\u4e2a\u6216\u4e24\u4e2a\u4ee5\u4e0a\u5bf9\u8c61\u5b58\u5728\u4f9d\u8d56\uff0c\u5f53\u4e00\u65b9\u4fee\u6539\u4e4b\u540e\u4f1a\u5f71\u54cd\u53e6\u4e00\u65b9\uff0c\u90a3\u4e48\u5c31\u8bf4\u8fd9\u4e9b\u5bf9\u8c61\u95f4\u5b58\u5728\u8026\u5408\u3002<\/strong>\u800c\u89e3\u8026\u5c31\u662f\u89e3\u9664\u4e24\u4e2a\u6216\u4e24\u4e2a\u4ee5\u4e0a\u5bf9\u8c61\uff0c\u4fee\u6539\u4e4b\u540e\u5f71\u54cd\u53e6\u4e00\u65b9\u7684\u95ee\u9898\u3002<\/p>\n<p>\u90a3 IoC\uff08\u63a7\u5236\u53cd\u8f6c\uff09\u662f\u5982\u4f55\u5b9e\u73b0\u89e3\u8026\u7684\u5462\uff1f<\/p>\n<p>\u6211\u4eec\u6765\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6bd4\u5982 A \u5bf9\u8c61\u4e2d\u9700\u8981\u4f7f\u7528 B \u5bf9\u8c61\u7684\u67d0\u4e2a\u65b9\u6cd5\uff0c\u90a3\u4e48\u6211\u4eec\u901a\u5e38\u7684\u5b9e\u73b0\u65b9\u6cd5\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre><code class=\"java\">class A {\n    public void init() {\n        \/\/ \u8c03\u7528 B \u7c7b\u4e2d\u7684 init \u65b9\u6cd5\n        B b = new B();\n        b.init();\n    }\n}\nclass B {\n    public B() {\n    }\n\n    public void init() {\n        System.out.println(&quot;\u4f60\u597d\uff0c\u4e16\u754c\u3002&quot;);\n    }\n}<\/code><\/pre>\n<p>\u7136\u800c\u6b64\u65f6\u5bf9\u8c61 A \u548c\u5bf9\u8c61 B \u662f\u5b58\u5728\u8026\u5408\u7684\uff0c\u56e0\u4e3a\u4e00\u65e6\u4fee\u6539\u4e86 B \u5bf9\u8c61\u6784\u9020\u65b9\u6cd5\u7684\u53c2\u6570\u4e4b\u540e\uff0c\u90a3\u4e48 A \u5bf9\u8c61\u91cc\u9762\u7684\u5199\u6cd5\u4e5f\u8981\u8ddf\u7740\u6539\u53d8\uff0c\u6bd4\u5982\u5f53\u6211\u4eec\u5c06\u6784\u9020\u65b9\u6cd5\u6539\u4e3a\u4ee5\u4e0b\u4ee3\u7801\u65f6\uff1a<\/p>\n<pre><code class=\"java\">class B {\n    public B(String name) {\n        System.out.println(&quot;\u59d3\u540d\uff1a&quot; + name);\n    }\n    public void init() {\n        System.out.println(&quot;\u4f60\u597d\uff0c\u4e16\u754c\u3002&quot;);\n    }\n}<\/code><\/pre>\n<p>\u6b64\u65f6\u6784\u9020\u65b9\u6cd5\u5df2\u7ecf\u4ece\u539f\u672c\u65e0\u53c2\u6784\u9020\u65b9\u6cd5\u53d8\u6210\u4e86\u6709\u53c2\u7684\u6784\u9020\u65b9\u6cd5\uff0c\u8fd9\u91cc\u4e0d\u8003\u8651\u6784\u9020\u65b9\u6cd5\u91cd\u8f7d\u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u5b9e\u9645\u4e1a\u52a1\u4e2d\uff0c\u5f88\u53ef\u80fd\u662f B \u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u5199\u9519\u4e86\uff0c\u5fd8\u8bb0\u52a0\u53c2\u6570\u4e86\uff0c\u4e8e\u662f\u540e\u9762\u53c8\u8865\u5145\u4e86\u4e00\u4e2a\u53c2\u6570\uff0c\u6b64\u65f6\u662f\u4e0d\u9700\u8981\u5bf9\u6784\u9020\u65b9\u6cd5\u8fdb\u884c\u91cd\u8f7d\u7684\uff0c\u90a3\u4e48\u6b64\u65f6\uff0c\u4e4b\u524d\u5bf9\u8c61 A \u91cc\u9762\u7684\u8c03\u7528\u5c31\u4f1a\u62a5\u9519\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042295094\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>\u8fd9\u5c31\u662f\u5f00\u53d1\u4e2d\u7ecf\u5e38\u9047\u5230\u7684\u4e00\u4e2a\u95ee\u9898\uff0c\u90a3\u600e\u4e48\u89e3\u51b3\u5462\uff1f<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06\u5bf9\u8c61\u4f20\u9012\u800c\u5e76 new \u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u89e3\u51b3\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n<pre><code class=\"java\">class A {\n    \/\/ \u5148\u5b9a\u4e49\u4e00\u4e2a\u9700\u8981\u4f9d\u8d56\u7684 B \u5bf9\u8c61\n    private B b;\n    \/\/ \u901a\u8fc7\u6784\u9020\u65b9\u6cd5\u5b9e\u73b0\u8d4b\u503c\uff08\u521d\u59cb\u5316\uff09\n    public A(B b) {\n        this.b = b;\n    }\n    public void init() {\n        \/\/ \u8c03\u7528 B \u7c7b\u4e2d\u7684 init \u65b9\u6cd5\n        b.init();\n    }\n}\nclass B {\n    public B(String name) {\n        System.out.println(&quot;\u59d3\u540d\uff1a&quot; + name);\n    }\n    public void init() {\n        System.out.println(&quot;\u4f60\u597d\uff0c\u4e16\u754c\u3002&quot;);\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u6837\u6539\u9020\u4e4b\u540e\uff0c\u65e0\u8bba\u6784\u9020\u65b9\u6cd5\u600e\u4e48\u4fee\u6539\uff0c\u5373\u4f7f\u9700\u8981\u52a0\u66f4\u591a\u7684\u53c2\u6570\uff0c\u800c\u8c03\u7528\u5b83\u7684 A \u7c7b\u90fd\u65e0\u9700\u505a\u4efb\u4f55\u4fee\u6539\uff0c\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u5bf9\u8c61\u7684\u89e3\u8026\u3002<\/p>\n<p>\u90a3\u8fd9\u4e2a\u89e3\u8026\u7684\u793a\u4f8b\u548c IoC \u6709\u4ec0\u4e48\u5173\u7cfb\u5462\uff1f<\/p>\n<p><strong>IoC \u5b9e\u73b0\u7684\u601d\u8def\u548c\u4e0a\u8ff0\u793a\u4f8b\u4e00\u6837\uff0c\u5c31\u662f\u901a\u8fc7\u5c06\u5bf9\u8c61\u4ea4\u7ed9 Spring \u4e2d IoC \u5bb9\u5668\u7ba1\u7406\uff0c\u5728\u5176\u4ed6\u7c7b\u4e2d\u4e0d\u76f4\u63a5 new \u5bf9\u8c61\uff0c\u800c\u662f\u901a\u8fc7\u5c06\u5bf9\u8c61\u4f20\u9012\u5230\u5f53\u524d\u7c7b\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u89e3\u8026\u7684\u3002<\/strong><\/p>\n<h3>\u5c0f\u7ed3<\/h3>\n<p><strong>\u5728 new \u5bf9\u8c61\u7684\u65f6\u4ee3\uff0c\u5bf9\u8c61\u7684\u7ba1\u7406\u6743\u662f\u7531\u5f53\u524d\u7c7b\u63a7\u5236\u7684\uff0c\u800c\u6709\u4e86 IoC \u4e4b\u540e\uff0c\u5bf9\u8c61\u7684\u7ba1\u7406\u6743\u5c31\u4ea4\u7ed9\u975e\u5f53\u524d\u7c7b\u7684 IoC \u5bb9\u5668\u7ba1\u7406\u4e86\uff0c\u6b64\u65f6\u5bf9\u8c61\u7684\u7ba1\u7406\u6743\u5c31\u53d1\u751f\u4e86\u53cd\u8f6c\u548c\u6539\u53d8\uff0c\u8fd9\u5c31\u662f IoC\uff0c\u8fd9\u5c31\u662f\u63a7\u5236\uff08\u6743\uff09\u53cd\u8f6c\u3002<\/strong><\/p>\n<h2>2.DI \u4ecb\u7ecd<\/h2>\n<p>DI \u662f Dependency Injection \u7684\u7f29\u5199\uff0c\u7ffb\u8bd1\u6210\u4e2d\u6587\u662f\u201c\u4f9d\u8d56\u6ce8\u5165\u201d\u7684\u610f\u601d\u3002<strong>\u4f9d\u8d56\u6ce8\u5165\u4e0d\u662f\u4e00\u79cd\u8bbe\u8ba1\u5b9e\u73b0\uff0c\u800c\u662f\u4e00\u79cd\u5177\u4f53\u7684\u6280\u672f<\/strong>\uff0c\u5b83\u662f<strong>\u5728 IoC \u5bb9\u5668\u8fd0\u884c\u671f\u95f4\uff0c\u52a8\u6001\u5730\u5c06\u67d0\u4e2a\u4f9d\u8d56\u5bf9\u8c61\u6ce8\u5165\u5230\u5f53\u524d\u5bf9\u8c61\u7684\u6280\u672f\u5c31\u53eb\u505a DI\uff08\u4f9d\u8d56\u6ce8\u5165\uff09<\/strong>\u3002<\/p>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0cA \u7c7b\u5728\u4f7f\u7528 B \u7c7b\u65f6\uff0c\u5c31\u662f\u901a\u8fc7\u6784\u9020\u65b9\u6cd5\u5c06\u4f9d\u8d56\u5bf9\u8c61 B \u5f15\u5165\u7684\uff0c\u8fd9\u79cd\u5b9e\u73b0\u65b9\u6cd5\u5c31\u53ef\u4ee5\u770b\u4f5c\u662f\u901a\u8fc7\u6784\u9020\u65b9\u6cd5\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165\u7684\u624b\u6bb5\uff0c\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">class A {\n    \/\/ \u5148\u5b9a\u4e49\u4e00\u4e2a\u9700\u8981\u4f9d\u8d56\u7684 B \u5bf9\u8c61\n    private B b;\n    \/\/ \u901a\u8fc7\u6784\u9020\u65b9\u6cd5\u5b9e\u73b0\u8d4b\u503c\n    public A(B b) {\n        this.b = b;\n    }\n    public void init() {\n        \/\/ \u8c03\u7528 B \u7c7b\u4e2d\u7684 init \u65b9\u6cd5\n        b.init();\n    }\n}<\/code><\/pre>\n<h2>3.IoC VS DI<\/h2>\n<p>\u4ece\u4e0a\u9762\u7684\u5185\u5bb9\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0cIoC \u548c DI \u867d\u7136\u5b9a\u4e49\u4e0d\u540c\uff0c\u4f46\u5b83\u4eec\u6240\u505a\u7684\u4e8b\u60c5\u90fd\u662f\u4e00\u6837\u7684\uff0c\u90fd\u662f\u7528\u6765\u5b9e\u73b0\u5bf9\u8c61\u89e3\u8026\u7684\uff0c\u800c\u4e8c\u8005\u53c8\u6709\u6240\u4e0d\u540c\uff1a<strong>IoC \u662f\u4e00\u79cd\u8bbe\u8ba1\u601d\u60f3\uff0c\u800c DI \u662f\u4e00\u79cd\u5177\u4f53\u7684\u5b9e\u73b0\u6280\u672f<\/strong>\u3002<\/p>\n<p>\u6bd4\u5982\uff0c\u78ca\u54e5\u4eca\u5929\u5fc3\u60c5\u6bd4\u8f83\u597d\uff0c\u60f3\u51fa\u53bb\u5403\u987f\u597d\u7684\uff0c\u90a3\u4e48\u201c\u60f3\u5403\u987f\u597d\u7684\u201d\u5c31\u662f\u4e00\u79cd\u601d\u60f3\u3001\u5c31\u662f IoC\u3002<\/p>\n<p>\u4f46\u4ec0\u4e48\u624d\u662f\u201c\u597d\u7684\uff08\u996d\uff09\u201d\u5462\uff1f\u6d77\u5e95\u635e\u8fd8\u662f\u97e9\u5f0f\u6599\u7406\uff1f\u5177\u4f53\u5403\u4ec0\u4e48\u662f DI\u3002<\/p>\n<p><strong>\u56e0\u6b64\u201c\u78ca\u54e5\u4eca\u5929\u5fc3\u60c5\u6bd4\u8f83\u597d\uff0c\u60f3\u51fa\u53bb\u5403\u987f\u597d\u7684\u201d\u662f\u4e00\u79cd\u601d\u60f3\u3001\u662f IoC\uff0c\u800c\u5403\u4e00\u987f\u6d77\u5e95\u635e\u800c\u975e\u97e9\u5f0f\u6599\u7406\u5c31\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u3001\u662f DI\u3002<\/strong><\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>IoC \u548c DI \u90fd\u662f Spring \u6846\u67b6\u4e2d\u7684\u91cd\u8981\u6982\u5ff5\uff0c\u5b83\u4eec\u90fd\u662f\u7528\u6765\u5b9e\u73b0\u5bf9\u8c61\u89e3\u8026\u7684\uff0c\u5176\u4e2d IoC\uff08\u63a7\u5236\u53cd\u8f6c\uff09\u662f\u4e00\u79cd\u8bbe\u8ba1\u601d\u60f3\uff0c\u800c DI\uff08\u4f9d\u8d56\u6ce8\u5165\uff09\u662f\u4e00\u79cd\u5177\u4f53\u7684\u5b9e\u73b0\u624b\u6bb5\u3002<\/p>\n<blockquote>\n<p>\u662f\u975e\u5ba1\u4e4b\u4e8e\u5df1\uff0c\u6bc1\u8a89\u542c\u4e4b\u4e8e\u4eba\uff0c\u5f97\u5931\u5b89\u4e4b\u4e8e\u6570\u3002<\/p>\n<p>\u516c\u4f17\u53f7\uff1aJava\u9762\u8bd5\u771f\u9898\u89e3\u6790<\/p>\n<p>\u9762\u8bd5\u5408\u96c6\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=SNTvia7bv7Y2JO%2FW1l0JKg%3D%3D.Any2b7ZYmvI9kcwsvqCviMd%2FrVWdjWoOers3bu9nRXdY3K2p7X17ndpBsN2DDKTE\" rel=\"nofollow\">https:\/\/gitee.com\/mydb\/interview<\/a><\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042295092\">https:\/\/segmentfault.com\/a\/1190000042295092<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>IoC \u548c DI \u90fd\u662f Spring \u6846\u67b6\u4e2d\u7684\u91cd\u8981\u6982\u5ff5\uff0c\u5c31\u50cf\u73ab\u7470\u82b1\u4e0e\u7231\u60c5\u4e00\u6837\uff0cIoC \u548c DI \u901a\u5e38\u60c5\u51b5\u4e0b\u4e5f\u662f [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006159","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006159"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006159\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}