{"id":527,"date":"2026-06-24T21:17:27","date_gmt":"2026-06-24T13:17:27","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=527"},"modified":"2026-06-24T21:17:27","modified_gmt":"2026-06-24T13:17:27","slug":"ioc%e7%9a%84%e6%a0%b8%e5%bf%83%e6%80%9d%e6%83%b3","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/06\/24\/ioc%e7%9a%84%e6%a0%b8%e5%bf%83%e6%80%9d%e6%83%b3\/","title":{"rendered":"IoC\u7684\u6838\u5fc3\u601d\u60f3"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728\u540e\u7aef\u7cfb\u7edf\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u51fa\u73b0<strong>\u591a\u4e2a\u4e0d\u540c\u7684Controller\u6a21\u5757\uff0c\u8c03\u7528\u540c\u4e00\u4e2aService\u7684\u60c5\u51b5<\/strong>\u3002\u90a3\u8fd9\u4e2a\u65f6\u5019\uff0c\u5982\u679c\u91cd\u65b0new\u4e0d\u540c\u7684Service\u5bf9\u8c61\uff0c\u662f\u4e0d\u5408\u9002\u7684\uff0c\u56e0\u4e3a\u6211\u4eec\u5e0c\u671b\u786e\u4fdd\u5b83\u4eec\u4f7f\u7528\u7684\u662f<strong>\u540c\u4e00\u4e2aService\u5b9e\u4f8b<\/strong>\uff0c\u4e5f\u5c31\u662f<strong>\u7ef4\u6301\u5355\u4f8b\u6a21\u5f0f<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5927\u578b\u5e94\u7528\u4e2d\uff0c\u624b\u52a8\u7ba1\u7406\u8fd9\u79cd\u4f9d\u8d56\u5173\u7cfb\uff0c\u4f1a\u53d8\u5f97\u975e\u5e38\u590d\u6742\u548c\u7e41\u7410\u3002\u6240\u4ee5\uff0c<strong>IoC\uff08Inverse of Control\uff0c\u63a7\u5236\u53cd\u8f6c\uff09<\/strong>\u5c31\u63d0\u4f9b\u4e86\u8fd9\u4e48\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">IoC\u5bb9\u5668\u5728\u5e94\u7528\u521d\u59cb\u5316\u65f6\uff0c\u4f1a\u67e5\u627e\u6bcf\u4e2a\u7c7b\u4e0a\u58f0\u660e\u7684\u4f9d\u8d56\uff0c\u5e76\u6309\u7167\u987a\u5e8f\u521b\u5efa\u76f8\u5e94\u7684\u5b9e\u4f8b\uff0c\u5e76\u4e14\u7ba1\u7406\u8fd9\u4e9b\u5b9e\u4f8b\uff0c\u5f53\u9700\u8981\u4f7f\u7528\u67d0\u4e2a\u4f9d\u8d56\u65f6\uff0c<strong>IoC\u5bb9\u5668\u4f1a\u5e2e\u4f60\u63d0\u4f9b\u76f8\u5e94\u7684\u5bf9\u8c61\u5b9e\u4f8b<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7531\u4e8e\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u90fd\u662f\u6839\u636e\u7c7b\u7684\u4f9d\u8d56\u5173\u7cfb\u6765\u8fdb\u884c\u5bb9\u5668\u7684\u5b9e\u4f8b\u521b\u5efa\u7684\uff0c\u6240\u4ee5\uff0c\u4e5f\u4f1a\u66f4\u5177\u8fd9\u79cd\u5b9e\u73b0\u65b9\u5f0f\u79f0\u4e4b\u4e3a<strong>\u4f9d\u8d56\u6ce8\u5165(Dependency Injection, DI)<\/strong>\uff0c\u901a\u8fc7\u4f9d\u8d56\u6ce8\u5165\uff0c\u5916\u90e8\u5bb9\u5668\u5c06\u6240\u9700\u7684\u4f9d\u8d56\u5bf9\u8c61\u6ce8\u5165\u5230\u76ee\u6807\u5bf9\u8c61\u4e2d\uff0c\u800c\u4e0d\u662f\u7531\u76ee\u6807\u5bf9\u8c61\u81ea\u5df1\u521b\u5efa\u4f9d\u8d56\u5bf9\u8c61<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>IoC<\/strong> \u63d0\u4f9b\u4e86\u4e00\u79cd\u7ed3\u6784\u4e0a\u7684\u601d\u60f3\uff1a\u5c06\u63a7\u5236\u6743\u4ece\u7a0b\u5e8f\u7684\u5bf9\u8c61\u8f6c\u79fb\u5230\u5916\u90e8\u5bb9\u5668\u6216\u6846\u67b6\u4e2d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>DI<\/strong> \u662f <strong>IoC<\/strong> \u7684\u5177\u4f53\u5b9e\u73b0\u4e4b\u4e00\uff0c\u5b83\u901a\u8fc7\u5916\u90e8\u6ce8\u5165\u4f9d\u8d56\u7684\u65b9\u5f0f\u5c06\u5bf9\u8c61\u6240\u4f9d\u8d56\u7684\u7ec4\u4ef6\u6216\u670d\u52a1\u4ea4\u7ed9\u5bb9\u5668\u8fdb\u884c\u7ba1\u7406\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f20\u7edf\u65b9\u5f0f<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class PaymentService {<br> &nbsp;processPayment(amount) {<br> &nbsp; &nbsp;console.log(`\u5904\u7406\u4ed8\u6b3e\u91d1\u989d: $${amount}`);<br> &nbsp; &nbsp;return true; \/\/ \u5047\u8bbe\u652f\u4ed8\u6210\u529f<br>  }<br>}<br>\u200b<br>class ShippingService {<br> &nbsp;shipOrder(orderDetails) {<br> &nbsp; &nbsp;console.log(`\u8ba2\u5355\u53d1\u8d27\u81f3: ${orderDetails.address}`);<br>  }<br>}<br>\u200b<br>class OrderService {<br> &nbsp;constructor() {<br> &nbsp; &nbsp;\/\/ \u76f4\u63a5\u5728\u6784\u9020\u51fd\u6570\u4e2d\u521b\u5efa\u4f9d\u8d56<br> &nbsp; &nbsp;this.paymentService = new PaymentService(); &nbsp;\/\/ \u76f4\u63a5\u521b\u5efa PaymentService \u5b9e\u4f8b<br> &nbsp; &nbsp;this.shippingService = new ShippingService(); \/\/ \u76f4\u63a5\u521b\u5efa ShippingService \u5b9e\u4f8b<br>  }<br>\u200b<br> &nbsp;createOrder(amount, address) {<br> &nbsp; &nbsp;console.log(`\u521b\u5efa\u8ba2\u5355\u91d1\u989d\uff1a $${amount}`);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;\/\/ \u8c03\u7528 paymentService \u8fdb\u884c\u652f\u4ed8<br> &nbsp; &nbsp;const paymentSuccess = this.paymentService.processPayment(amount);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;if (paymentSuccess) {<br> &nbsp; &nbsp; &nbsp;console.log(`\u8ba2\u5355\u521b\u5efa\u6210\u529f!`);<br> &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp;\/\/ \u8c03\u7528 shippingService \u53d1\u8d27<br> &nbsp; &nbsp; &nbsp;this.shippingService.shipOrder({ address });<br> &nbsp;  } else {<br> &nbsp; &nbsp; &nbsp;console.log(`\u4ed8\u6b3e\u5931\u8d25\u3002\u8ba2\u5355\u521b\u5efa\u5df2\u4e2d\u6b62!`);<br> &nbsp;  }<br>  }<br>}<br>\u200b<br>\/\/ \u76f4\u63a5\u521b\u5efa OrderService \u7684\u5b9e\u4f8b\uff0c\u800c\u4e0d\u662f\u4ece\u5bb9\u5668\u83b7\u53d6<br>const orderService = new OrderService();<br>\u200b<br>\/\/ \u521b\u5efa\u8ba2\u5355<br>orderService.createOrder(200, '\u6210\u90fd\u5e02\u5929\u5e9c\u4e09\u8857');<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">IoC\u65b9\u5f0f<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class Container {<br> &nbsp;constructor() {<br> &nbsp; &nbsp;this.services = new Map(); \/\/ \u5b58\u50a8\u670d\u52a1\u5b9e\u4f8b<br>  }<br>\u200b<br> &nbsp;\/\/ \u6ce8\u518c\u670d\u52a1\u5230\u5bb9\u5668\u4e2d<br> &nbsp;register(name, service) {<br> &nbsp; &nbsp;this.services.set(name, service);<br>  }<br>\u200b<br> &nbsp;\/\/ \u83b7\u53d6\u670d\u52a1\u5b9e\u4f8b<br> &nbsp;get(name) {<br> &nbsp; &nbsp;const service = this.services.get(name);<br> &nbsp; &nbsp;if (!service) {<br> &nbsp; &nbsp; &nbsp;throw new Error(`Service ${name} not found!`);<br> &nbsp;  }<br> &nbsp; &nbsp;\/\/ \u5982\u679c\u670d\u52a1\u662f\u4e00\u4e2a\u5de5\u5382\u51fd\u6570\uff0c\u6267\u884c\u5b83\u6765\u521b\u5efa\u5b9e\u4f8b<br> &nbsp; &nbsp;if (typeof service === 'function') {<br> &nbsp; &nbsp; &nbsp;const instance = service(this); \/\/ \u521b\u5efa\u5b9e\u4f8b<br> &nbsp; &nbsp; &nbsp;this.services.set(name, instance); \/\/ \u7f13\u5b58\u5b9e\u4f8b<br> &nbsp; &nbsp; &nbsp;return instance;<br> &nbsp;  }<br> &nbsp; &nbsp;return service;<br>  }<br>}<br>\u200b<br>\/\/ PaymentService \u8d1f\u8d23\u5904\u7406\u652f\u4ed8<br>class PaymentService {<br> &nbsp;processPayment(amount) {<br> &nbsp; &nbsp;console.log(`\u5904\u7406\u4ed8\u6b3e\u91d1\u989d: $${amount}`);<br> &nbsp; &nbsp;return true; \/\/ \u5047\u8bbe\u652f\u4ed8\u6210\u529f<br>  }<br>}<br>\u200b<br>class ShippingService {<br> &nbsp;shipOrder(orderDetails) {<br> &nbsp; &nbsp;console.log(`\u8ba2\u5355\u53d1\u8d27\u81f3: ${orderDetails.address}`);<br>  }<br>}<br>\u200b<br>\/\/ OrderService \u4f9d\u8d56 PaymentService\u548cShippingService\uff0c\u521b\u5efa\u8ba2\u5355\u65f6\u9700\u8981\u8c03\u7528\u652f\u4ed8\u548c\u53d1\u8d27\u670d\u52a1<br>class OrderService {<br> &nbsp;constructor(paymentService, shippingService) {<br> &nbsp; &nbsp;this.paymentService = paymentService;<br> &nbsp; &nbsp;this.shippingService = shippingService;<br>  }<br>\u200b<br> &nbsp;createOrder(amount, address) {<br> &nbsp; &nbsp;console.log(`\u521b\u5efa\u8ba2\u5355\u91d1\u989d\uff1a$${amount}`);<br> &nbsp; &nbsp;const paymentSuccess = this.paymentService.processPayment(amount);<br> &nbsp; &nbsp;if (paymentSuccess) {<br> &nbsp; &nbsp; &nbsp;console.log(`\u8ba2\u5355\u521b\u5efa\u6210\u529f!`);<br> &nbsp; &nbsp; &nbsp;this.shippingService.shipOrder({ address });<br> &nbsp;  } else {<br> &nbsp; &nbsp; &nbsp;console.log(`\u4ed8\u6b3e\u5931\u8d25\u3002\u8ba2\u5355\u521b\u5efa\u5df2\u4e2d\u6b62!`);<br> &nbsp;  }<br>  }<br>}<br>\u200b<br>\/\/ \u521b\u5efa\u4e00\u4e2a IoC \u5bb9\u5668\u5b9e\u4f8b<br>const container = new Container();<br>\u200b<br>container.register('paymentService', new PaymentService()); \/\/ \u76f4\u63a5\u6ce8\u518c PaymentService<br>container.register('shippingService', new ShippingService()); \/\/ \u76f4\u63a5\u6ce8\u518c ShippingService<br>\u200b<br>container.register('orderService', (container) =&gt; {<br> &nbsp;return new OrderService(<br> &nbsp; &nbsp;container.get('paymentService'),<br> &nbsp; &nbsp;container.get('shippingService')<br>  ); \/\/ \u6ce8\u5165 PaymentService \u548c ShippingService<br>});<br>\u200b<br>\/\/ \u4ece\u5bb9\u5668\u4e2d\u83b7\u53d6 OrderService\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u8ba2\u5355<br>const orderService = container.get('orderService');<br>orderService.createOrder(200, '\u6210\u90fd\u5e02\u5929\u5e9c\u4e09\u8857');<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u7ec8\u5b9e\u73b0\u7684\u6548\u679c\u5176\u5b9e\u662f\u4e00\u81f4\u7684\uff0c\u4f46\u662f\u6211\u4eec\u4ed4\u7ec6\u770b\u4e00\u4e0b\uff0c\u5982\u679c\u4f20\u7edf\u7684\u65b9\u5f0f\uff0c\u518d\u6b21\u6765\u83b7\u53d6\u8ba2\u5355\u670d\u52a1\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u4e0d\u7ecf\u610f\u4e4b\u95f4\u76f4\u63a5\u53c8\u4f1a<strong>\u91cd\u65b0new\u4e00\u6b21\u8ba2\u5355\u670d\u52a1<\/strong>\uff0c\u4f46\u662f\uff0cIoC\u7684\u6a21\u5f0f\u4e0b\uff0c\u6211\u4eec\u662f\u4ece\u5bb9\u5668\u4e2d\u76f4\u63a5\u83b7\u53d6\u7684\uff0c\u4ece\u800c\u4fdd\u8bc1\u4e86\u8ba2\u5355\u670d\u52a1\u59cb\u7ec8\u90fd\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u540e\u7aef\u7cfb\u7edf\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u51fa\u73b0\u591a\u4e2a\u4e0d\u540c\u7684Controll..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[44,43],"class_list":["post-527","post","type-post","status-publish","format-standard","hentry","category-back","tag-di","tag-ioc"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/\" rel=\"category tag\">\u540e\u7aef<\/a>","tag_info":"\u540e\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=527"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/527\/revisions"}],"predecessor-version":[{"id":528,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/527\/revisions\/528"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}