{"id":55140,"date":"2025-08-13T17:39:52","date_gmt":"2025-08-13T09:39:52","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/55140.html"},"modified":"2025-08-13T17:39:52","modified_gmt":"2025-08-13T09:39:52","slug":"%e3%80%90%e5%90%8e%e7%ab%af%e3%80%91spring%e6%a1%86%e6%9e%b6%e6%8e%a7%e5%88%b6%e5%8f%8d%e8%bd%ac%ef%bc%88ioc%ef%bc%89%e4%b8%8e%e4%be%9d%e8%b5%96%e6%b3%a8%e5%85%a5%ef%bc%88di%ef%bc%89%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/55140.html","title":{"rendered":"\u3010\u540e\u7aef\u3011Spring\u6846\u67b6\u63a7\u5236\u53cd\u8f6c\uff08IoC\uff09\u4e0e\u4f9d\u8d56\u6ce8\u5165\uff08DI\uff09\u89e3\u6790"},"content":{"rendered":"<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\u6838\u5fc3\u6982\u5ff5<\/li>\n<li>\n<ul>\n<li>\u63a7\u5236\u53cd\u8f6c (Inversion of Control &#8211; IoC)<\/li>\n<li>\u4f9d\u8d56\u6ce8\u5165 (Dependency Injection &#8211; DI)<\/li>\n<\/ul>\n<\/li>\n<li>Spring IoC \u5bb9\u5668&#xff1a;\u673a\u5236\u4e0e\u5b9e\u73b0<\/li>\n<li>\n<ul>\n<li>\u4f9d\u8d56\u6ce8\u5165 (DI) \u5728 Spring \u4e2d\u7684\u4e3b\u8981\u65b9\u5f0f<\/li>\n<li>Spring \u662f\u5982\u4f55\u77e5\u9053\u8981\u6ce8\u5165\u4ec0\u4e48\u7684&#xff1f;\u914d\u7f6e\u4e0e\u626b\u63cf<\/li>\n<\/ul>\n<\/li>\n<li>Spring IoC\/DI \u5e26\u6765\u7684\u5de8\u5927\u597d\u5904<\/li>\n<li>Spring IoC \u5bb9\u5668\u7684\u9ad8\u7ea7\u7279\u6027<\/li>\n<li>\u603b\u7ed3<\/li>\n<\/ul>\n<h2>\u6838\u5fc3\u6982\u5ff5<\/h2>\n<p>\u60f3\u8c61\u4e00\u4e0b\u4f60\u5f00\u53d1\u4e00\u4e2a\u7b80\u5355\u7684\u5e94\u7528&#xff0c;\u6709\u4e00\u4e2a OrderService \u7528\u6765\u5904\u7406\u8ba2\u5355\u903b\u8f91&#xff0c;\u5b83\u9700\u8981\u4f9d\u8d56\u4e00\u4e2a PaymentService \u6765\u5904\u7406\u652f\u4ed8\u3002\u5728\u4f20\u7edf\u7684\u7f16\u7a0b\u65b9\u5f0f&#xff08;\u5e38\u88ab\u79f0\u4e3a \u201c\u63a7\u5236\u6b63\u8f6c\u201d&#xff09;\u4e2d&#xff0c;\u4f60\u4f1a\u8fd9\u6837\u5199&#xff1a;<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">OrderService<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">private<\/span> <span class=\"token class-name\">PaymentService<\/span> paymentService <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">CreditCardPaymentService<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u6216\u8005 new PayPalPaymentService();<\/span><\/p>\n<p>    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">processOrder<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Order<\/span> order<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ &#8230; \u4e1a\u52a1\u903b\u8f91 &#8230;<\/span><br \/>\n        paymentService<span class=\"token punctuation\">.<\/span><span class=\"token function\">processPayment<\/span><span class=\"token punctuation\">(<\/span>order<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token comment\">\/\/ &#8230; \u66f4\u591a\u4e1a\u52a1\u903b\u8f91 &#8230;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u8fd9\u91cc OrderService \u5b8c\u5168\u63a7\u5236\u5e76\u8d1f\u8d23\u521b\u5efa\u5176\u6240\u4f9d\u8d56\u7684 PaymentService \u5b9e\u4f8b (new CreditCardPaymentService())\u3002\u8fd9\u770b\u4f3c\u76f4\u63a5&#xff0c;\u4f46\u5b58\u5728\u51e0\u4e2a\u5173\u952e\u95ee\u9898&#xff1a;<\/p>\n<li>\u7d27\u8026\u5408 (Tight Coupling): OrderService \u76f4\u63a5\u4e0e CreditCardPaymentService \u7684\u5177\u4f53\u5b9e\u73b0\u7ed1\u5b9a\u3002\u5982\u679c\u60f3\u6539\u6210 PayPalPaymentService&#xff0c;\u5c31\u5fc5\u987b\u4fee\u6539 OrderService \u7684\u6e90\u4ee3\u7801\u3002<\/li>\n<li>\u96be\u4ee5\u6d4b\u8bd5 (Hard to Test): \u5355\u5143\u6d4b\u8bd5 OrderService.processOrder \u65b9\u6cd5\u53d8\u5f97\u56f0\u96be\u3002\u56e0\u4e3a processPayment \u65b9\u6cd5\u662f\u771f\u5b9e\u6267\u884c\u7684&#xff08;\u53ef\u80fd\u6d89\u53ca\u5b9e\u9645\u7684\u4fe1\u7528\u5361\u6263\u6b3e\u6216\u7f51\u7edc\u8c03\u7528&#xff09;\u3002\u7406\u60f3\u60c5\u51b5\u4e0b&#xff0c;\u6211\u4eec\u53ea\u60f3\u6d4b\u8bd5 OrderService \u81ea\u5df1\u7684\u903b\u8f91&#xff0c;\u9700\u8981\u4e00\u4e2a\u6a21\u62df\u7684 PaymentService\u3002<\/li>\n<li>\u804c\u8d23\u8fc7\u591a: OrderService \u672c\u5e94\u4e13\u6ce8\u4e8e\u8ba2\u5355\u5904\u7406\u903b\u8f91&#xff0c;\u73b0\u5728\u5374\u8981\u64cd\u5fc3\u5982\u4f55\u521b\u5efa\u548c\u7ba1\u7406\u652f\u4ed8\u670d\u52a1\u7684\u5b9e\u4f8b\u3002<\/li>\n<li>\u53ef\u6269\u5c55\u6027\u5dee: \u5f15\u5165\u4e0d\u540c\u7684\u652f\u4ed8\u7b56\u7565\u6216\u914d\u7f6e\u53d8\u5f97\u7e41\u7410\u4e14\u9700\u8981\u4fb5\u5165\u5f0f\u4fee\u6539\u4ee3\u7801\u3002<\/li>\n<h3>\u63a7\u5236\u53cd\u8f6c (Inversion of Control &#8211; IoC)<\/h3>\n<p>IoC \u662f\u4e00\u79cd\u8bbe\u8ba1\u539f\u5219&#xff0c;\u5b83\u7684\u6838\u5fc3\u7406\u5ff5\u662f&#xff1a;\u5c06\u521b\u5efa\u548c\u7ed1\u5b9a\u4f9d\u8d56\u5bf9\u8c61\u7684\u63a7\u5236\u6743\u4ece\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u8f6c\u79fb\u5230\u5916\u90e8\u5bb9\u5668&#xff08;\u5728 Spring \u4e2d\u5c31\u662f IoC \u5bb9\u5668&#xff09;\u3002<\/p>\n<ul>\n<li>\u53cd\u8f6c\u4e86\u4ec0\u4e48&#xff1f; \u53cd\u8f6c\u4e86\u521b\u5efa\u548c\u7ba1\u7406\u4f9d\u8d56\u5bf9\u8c61\u7684\u8d23\u4efb\u3002\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u88ab\u52a8\u5730\u63a5\u6536\u5b83\u6240\u9700\u8981\u7684\u4f9d\u8d56&#xff0c;\u800c\u4e0d\u662f\u4e3b\u52a8\u5730\u53bb\u521b\u5efa\u6216\u67e5\u627e\u3002<\/li>\n<li>\u597d\u83b1\u575e\u539f\u5219 (\u201cDon\u2019t call us, we\u2019ll call you\u201d): \u5f88\u597d\u5730\u63cf\u8ff0\u4e86 IoC\u3002\u4f60\u7684\u7c7b&#xff08;\u5982 OrderService&#xff09;\u4e0d\u9700\u8981\u53bb\u627e (new) \u5b83\u7684\u4f9d\u8d56&#xff0c;\u53ea\u9700\u8981\u58f0\u660e\u5b83\u9700\u8981\u4ec0\u4e48 (\u201c\u6211\u9700\u8981\u4e00\u4e2a PaymentService\u201d)&#xff0c;IoC \u5bb9\u5668&#xff08;\u5bfc\u6f14&#xff09;\u4f1a\u5728\u5408\u9002\u7684\u65f6\u673a\u521b\u5efa\u597d\u5e76\u201c\u6253\u7ed9\u4f60\u201d&#xff08;\u6ce8\u5165\u7ed9\u4f60&#xff09;\u3002<\/li>\n<li>\u76ee\u6807&#xff1a;\u89e3\u8026 (Decoupling)&#xff1a;\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u4e0d\u4f9d\u8d56\u4e8e\u5177\u4f53\u7684\u4f9d\u8d56\u5b9e\u73b0&#xff0c;\u800c\u662f\u4f9d\u8d56\u4e8e\u62bd\u8c61&#xff08;\u63a5\u53e3&#xff09;\u3002\u5177\u4f53\u7684\u5b9e\u73b0\u9009\u62e9\u548c\u7ec4\u88c5\u5de5\u4f5c\u7531\u5bb9\u5668\u5b8c\u6210\u3002\u8fd9\u662f\u5b9e\u73b0\u677e\u8026\u5408\u7684\u5173\u952e\u3002<\/li>\n<\/ul>\n<h3>\u4f9d\u8d56\u6ce8\u5165 (Dependency Injection &#8211; DI)<\/h3>\n<p>\u4f9d\u8d56\u6ce8\u5165\u662f\u5b9e\u73b0\u63a7\u5236\u53cd\u8f6c\u539f\u5219\u6700\u5e38\u89c1\u3001\u6700\u4e3b\u8981\u7684\u8bbe\u8ba1\u6a21\u5f0f\u3002DI \u5b9a\u4e49\u4e86\u5982\u4f55\u5c06\u4f9d\u8d56\u63d0\u4f9b\u7ed9\u76ee\u6807\u5bf9\u8c61\u3002<\/p>\n<ul>\n<li>\u6838\u5fc3\u601d\u60f3&#xff1a; \u5728\u521b\u5efa\u5bf9\u8c61&#xff08;Bean&#xff09;\u65f6&#xff0c;\u7531\u5916\u90e8\u5b9e\u4f53&#xff08;IoC \u5bb9\u5668&#xff09;\u5c06\u5176\u6240\u4f9d\u8d56\u7684\u5176\u4ed6\u5bf9\u8c61&#xff08;Beans&#xff09;\u901a\u8fc7\u67d0\u79cd\u65b9\u5f0f&#xff08;\u6784\u9020\u5668\u3001Setter\u3001\u5b57\u6bb5&#xff09;\u6ce8\u5165\u8fdb\u53bb\u3002<\/li>\n<li>\u5173\u952e&#xff1a; \u5bf9\u8c61\u4e4b\u95f4\u7684\u5173\u7cfb&#xff08;\u4f9d\u8d56&#xff09;\u4e0d\u518d\u7531\u5bf9\u8c61\u81ea\u8eab\u5728\u5185\u90e8\u5efa\u7acb&#xff0c;\u800c\u662f\u7531\u8fd0\u884c\u73af\u5883&#xff08;\u5bb9\u5668&#xff09;\u5728\u5bf9\u8c61\u5916\u90e8\u5efa\u7acb\u5e76\u201c\u6ce8\u5c04\u201d\u8fdb\u53bb\u3002<\/li>\n<\/ul>\n<h2>Spring IoC \u5bb9\u5668&#xff1a;\u673a\u5236\u4e0e\u5b9e\u73b0<\/h2>\n<p>Spring \u6846\u67b6\u662f IoC \u539f\u5219\u7684\u5353\u8d8a\u5b9e\u73b0\u8005\u3002\u5b83\u7684\u6838\u5fc3\u662f IoC \u5bb9\u5668\u3002\u4e3b\u8981\u7684\u5bb9\u5668\u63a5\u53e3\u662f ApplicationContext&#xff08;\u53ca\u5176\u5177\u4f53\u5b9e\u73b0\u7c7b&#xff0c;\u5982 AnnotationConfigApplicationContext, ClassPathXmlApplicationContext, FileSystemXmlApplicationContext&#xff09;\u3002\u5b83\u7684\u804c\u8d23&#xff1a;<\/p>\n<li>\u5b9e\u4f8b\u5316 Bean: \u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5bf9\u8c61&#xff08;\u79f0\u4e3a Beans&#xff09;\u3002<\/li>\n<li>\u914d\u7f6e Bean: \u8bbe\u7f6e Bean \u7684\u5c5e\u6027\u3002<\/li>\n<li>\u88c5\u914d\u4f9d\u8d56: \u5904\u7406 Bean \u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb&#xff08;DI \u7684\u5177\u4f53\u64cd\u4f5c&#xff09;\u3002<\/li>\n<li>\u7ba1\u7406 Bean \u751f\u547d\u5468\u671f: \u63d0\u4f9b\u5982\u521d\u59cb\u5316\u56de\u8c03\u3001\u9500\u6bc1\u56de\u8c03\u7b49\u673a\u5236\u3002<\/li>\n<li>\u63d0\u4f9b\u8fd0\u884c\u65f6\u73af\u5883: \u5982\u8bbf\u95ee\u6587\u4ef6\u8d44\u6e90\u3001\u56fd\u9645\u5316\u6d88\u606f\u3001\u4e8b\u4ef6\u53d1\u5e03\u7b49\u3002<\/li>\n<h3>\u4f9d\u8d56\u6ce8\u5165 (DI) \u5728 Spring \u4e2d\u7684\u4e3b\u8981\u65b9\u5f0f<\/h3>\n<p>\u5047\u8bbe\u6211\u4eec\u5b9a\u4e49\u63a5\u53e3\u548c\u5b9e\u73b0&#xff1a;<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">interface<\/span> <span class=\"token class-name\">PaymentService<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">processPayment<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Order<\/span> order<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token annotation punctuation\">&#064;Component<\/span> <span class=\"token comment\">\/\/ \u6216 &#064;Service, &#064;Repository\u7b49&#xff0c;\u6807\u8bb0\u8fd9\u4e2a\u7c7b\u662fBean<\/span><br \/>\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">CreditCardPaymentService<\/span> <span class=\"token keyword\">implements<\/span> <span class=\"token class-name\">PaymentService<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token annotation punctuation\">&#064;Override<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">processPayment<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Order<\/span> order<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u4fe1\u7528\u5361\u652f\u4ed8\u903b\u8f91<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token annotation punctuation\">&#064;Service<\/span> <span class=\"token comment\">\/\/ \u6807\u8bb0OrderService\u4e3aBean<\/span><br \/>\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">OrderService<\/span> <span class=\"token punctuation\">{<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u9700\u8981\u4f9d\u8d56\u4e00\u4e2aPaymentService<\/span><br \/>\n    <span class=\"token keyword\">private<\/span> <span class=\"token keyword\">final<\/span> <span class=\"token class-name\">PaymentService<\/span> paymentService<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u65b9\u5f0f1&#xff1a;\u6784\u9020\u5668\u6ce8\u5165 (\u63a8\u8350)<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token class-name\">OrderService<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">PaymentService<\/span> paymentService<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">this<\/span><span class=\"token punctuation\">.<\/span>paymentService <span class=\"token operator\">&#061;<\/span> paymentService<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u5bb9\u5668\u5728\u8fd9\u91cc\u6ce8\u5165\u4f9d\u8d56<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u65b9\u5f0f2&#xff1a;Setter\u65b9\u6cd5\u6ce8\u5165<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">setPaymentService<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">PaymentService<\/span> paymentService<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">this<\/span><span class=\"token punctuation\">.<\/span>paymentService <span class=\"token operator\">&#061;<\/span> paymentService<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u5bb9\u5668\u901a\u8fc7\u8c03\u7528\u6b64\u65b9\u6cd5\u6ce8\u5165<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u65b9\u5f0f3&#xff1a;\u5b57\u6bb5\u6ce8\u5165 (\u4e0d\u63a8\u8350&#xff0c;\u5b58\u5728\u9690\u60a3&#xff0c;\u53ef\u4ee5\u4f7f\u7528&#064;Resource\u66ff\u4ee3)<\/span><br \/>\n    <span class=\"token annotation punctuation\">&#064;Autowired<\/span><br \/>\n    <span class=\"token keyword\">private<\/span> <span class=\"token class-name\">PaymentService<\/span> paymentService<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">processOrder<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Order<\/span> order<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ &#8230; \u4f7f\u7528 paymentService &#8230;<\/span><br \/>\n        paymentService<span class=\"token punctuation\">.<\/span><span class=\"token function\">processPayment<\/span><span class=\"token punctuation\">(<\/span>order<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<li>\n<p>\u6784\u9020\u5668\u6ce8\u5165 (Constructor Injection):<\/p>\n<ul>\n<li>\u600e\u4e48\u505a&#xff1f; \u5728\u7c7b\u7684\u6784\u9020\u5668\u4e0a\u58f0\u660e\u4f9d\u8d56\u53c2\u6570\u3002<\/li>\n<li>Spring \u7684\u88c5\u914d&#xff1a; \u5bb9\u5668\u5728\u521b\u5efa Bean (OrderService) \u65f6&#xff0c;\u8c03\u7528\u5176\u6784\u9020\u5668\u5e76\u4f20\u5165\u6240\u9700\u7684\u4f9d\u8d56 (PaymentService)\u3002<\/li>\n<li>\u4f18\u70b9:\n<ul>\n<li>\u5f3a\u5236\u4f9d\u8d56&#xff1a; \u786e\u4fdd Bean \u5728\u521b\u5efa\u5b8c\u6210\u540e\u5c31\u5904\u4e8e\u5b8c\u5168\u521d\u59cb\u5316\u72b6\u6001&#xff0c;\u6240\u6709\u5fc5\u8981\u4f9d\u8d56\u90fd\u5df2\u6ee1\u8db3\u3002\u907f\u514d NullPointerException\u3002<\/li>\n<li>\u4e0d\u53ef\u53d8&#xff08;Immutable&#xff09;: \u901a\u5e38\u914d\u5408 final \u5b57\u6bb5\u4f7f\u7528&#xff0c;\u4f7f\u5f97\u4f9d\u8d56\u5728\u5bf9\u8c61\u751f\u547d\u5468\u671f\u5185\u4e0d\u53ef\u53d8&#xff0c;\u66f4\u5b89\u5168&#xff08;\u5c24\u5176\u662f\u591a\u7ebf\u7a0b&#xff09;\u3002<\/li>\n<li>\u660e\u786e\u8868\u8fbe Bean \u7684\u5fc5\u9700\u4f9d\u8d56\u9879\u3002<\/li>\n<\/ul>\n<\/li>\n<li>Spring \u9f13\u52b1\u4f7f\u7528\u7684\u65b9\u5f0f\u3002 \u662f Spring Framework \u56e2\u961f\u7684\u9996\u9009\u3002<\/li>\n<li>\u793a\u4f8b: public OrderService(PaymentService paymentService) { &#8230; }<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Setter \u6ce8\u5165 (Setter Injection):<\/p>\n<ul>\n<li>\u600e\u4e48\u505a&#xff1f; \u4e3a\u9700\u8981\u6ce8\u5165\u7684\u4f9d\u8d56\u63d0\u4f9b\u4e00\u4e2a\u516c\u5171\u7684 setter \u65b9\u6cd5&#xff08;\u5982 setPaymentService&#xff09;\u3002<\/li>\n<li>Spring \u7684\u88c5\u914d&#xff1a; \u5bb9\u5668\u9996\u5148\u901a\u8fc7\u65e0\u53c2\u6784\u9020\u5668&#xff08;\u6216\u6307\u5b9a\u6784\u9020\u5668&#xff09;\u521b\u5efa Bean \u5b9e\u4f8b&#xff0c;\u7136\u540e\u8c03\u7528\u76f8\u5e94\u7684 setter \u65b9\u6cd5\u6765\u6ce8\u5165\u4f9d\u8d56\u3002<\/li>\n<li>\u4f18\u70b9:\n<ul>\n<li>\u53ef\u9009\u4f9d\u8d56&#xff1a; \u9002\u5408\u90a3\u4e9b\u4e0d\u662f\u5f3a\u5236\u6027\u7684\u3001\u53ef\u6709\u53ef\u65e0\u3001\u6216\u8005\u6709\u9ed8\u8ba4\u5b9e\u73b0\u7684\u4f9d\u8d56\u3002<\/li>\n<li>\u7075\u6d3b\u6027&#xff1a; \u5bf9\u8c61\u53ef\u4ee5\u5728\u6784\u9020\u540e\u8fdb\u884c\u91cd\u65b0\u914d\u7f6e&#xff08;\u4f46\u5b9e\u8df5\u4e2d\u8f83\u5c11\u6539\u53d8\u5df2\u88c5\u914d Bean \u7684\u4f9d\u8d56&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u793a\u4f8b: public void setPaymentService(PaymentService paymentService) { &#8230; }<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5b57\u6bb5\u6ce8\u5165 (Field Injection):<\/p>\n<ul>\n<li>\u600e\u4e48\u505a&#xff1f; \u76f4\u63a5\u5728\u9700\u8981\u4f9d\u8d56\u7684\u7c7b\u5b57\u6bb5\u4e0a\u6807\u6ce8 &#064;Autowired (\u6216\u5176\u4ed6\u6ce8\u89e3\u5982 &#064;Inject&#xff0c; &#064;Resource)\u3002<\/li>\n<li>Spring \u7684\u88c5\u914d&#xff1a; \u5bb9\u5668\u5229\u7528\u53cd\u5c04\u673a\u5236\u76f4\u63a5\u5c06\u4f9d\u8d56\u6ce8\u5165\u5230\u79c1\u6709\u5b57\u6bb5&#xff08;\u6216\u8005protected\u3001public\u5b57\u6bb5&#xff0c;\u4f46\u79c1\u6709\u66f4\u5e38\u89c1&#xff09;&#xff0c;\u4e0d\u9700\u8981\u6784\u9020\u5668\u6216 setter\u3002<\/li>\n<li>\u7f3a\u70b9&#xff08;\u4e25\u91cd&#xff0c;\u4e0d\u63a8\u8350&#xff01;\u6700\u597d\u4f7f\u7528&#064;Resource&#xff09;:\n<ul>\n<li>\u8fdd\u53cd\u4e86\u5c01\u88c5\u6027&#xff1a; \u76f4\u63a5\u4fee\u6539\u79c1\u6709\u5b57\u6bb5\u7ed5\u8fc7\u4e86\u4efb\u4f55\u53ef\u80fd\u5b58\u5728\u7684\u6784\u9020\u5668\u6216 setter \u903b\u8f91\u3002<\/li>\n<li>\u96be\u4ee5\u6d4b\u8bd5&#xff1a; \u65e0\u6cd5\u901a\u8fc7\u6784\u9020\u5668\u6216 setter \u8f7b\u677e\u4f20\u5165\u6a21\u62df\u4f9d\u8d56&#xff0c;\u5355\u5143\u6d4b\u8bd5\u65f6\u5fc5\u987b\u4f9d\u8d56 Spring \u5bb9\u5668\u6216\u4f7f\u7528\u53cd\u5c04\u3002<\/li>\n<li>\u9690\u85cf\u4f9d\u8d56&#xff1a; \u7c7b\u4ece\u5916\u90e8\u770b&#xff0c;\u54ea\u4e9b\u662f\u5fc5\u9700\u7684\u4f9d\u8d56\u4e0d\u6e05\u6670&#xff08;\u4e0d\u50cf\u6784\u9020\u5668\u90a3\u6837\u4e00\u76ee\u4e86\u7136&#xff09;\u3002<\/li>\n<li>\u6f5c\u5728\u7684\u7a7a\u6307\u9488\u5f02\u5e38&#xff1a; \u5982\u679c\u8131\u79bb\u5bb9\u5668\u624b\u52a8\u5b9e\u4f8b\u5316\u7c7b&#xff0c;\u5b57\u6bb5\u4f9d\u8d56\u4e0d\u4f1a\u81ea\u52a8\u6ce8\u5165&#xff0c;\u5bb9\u6613\u5f15\u53d1 NPE\u3002<\/li>\n<\/ul>\n<\/li>\n<li>Spring \u5b98\u65b9\u4e0d\u63a8\u8350\u8fd9\u79cd\u65b9\u5f0f\u3002 \u5e94\u8be5\u4f18\u5148\u4f7f\u7528\u6784\u9020\u5668\u6ce8\u5165&#xff0c;\u5176\u6b21\u662f setter \u6ce8\u5165\u3002<\/li>\n<li>\u793a\u4f8b: &#064;Autowired private PaymentService paymentService;<\/li>\n<\/ul>\n<\/li>\n<h3>Spring \u662f\u5982\u4f55\u77e5\u9053\u8981\u6ce8\u5165\u4ec0\u4e48\u7684&#xff1f;\u914d\u7f6e\u4e0e\u626b\u63cf<\/h3>\n<p>Spring \u5bb9\u5668\u9700\u8981\u77e5\u9053&#xff1a;<\/p>\n<li>\u54ea\u4e9b\u7c7b\u662f\u9700\u8981\u5b83\u7ba1\u7406\u7684 Bean&#xff1f; (&#064;Component, &#064;Service, &#064;Repository, &#064;Controller, &#064;Configuration)<\/li>\n<li>\u5982\u4f55\u521b\u5efa\u5b83\u4eec&#xff1f; (\u9ed8\u8ba4\u65e0\u53c2\u6784\u9020\u5668&#xff0c;\u53ef\u901a\u8fc7\u5de5\u5382\u65b9\u6cd5\u7b49\u914d\u7f6e)<\/li>\n<li>\u5b83\u4eec\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u662f\u4ec0\u4e48&#xff1f; (&#064;Autowired, &#064;Inject, &#064;Resource)<\/li>\n<li>&#xff08;\u53ef\u9009&#xff09;Bean \u7684\u5176\u4ed6\u5c5e\u6027\u548c\u884c\u4e3a&#xff08;\u4f5c\u7528\u57df\u3001\u521d\u59cb\u5316\/\u9500\u6bc1\u65b9\u6cd5\u7b49&#xff09;\u3002<\/li>\n<p>Spring \u901a\u8fc7\u4e24\u79cd\u4e3b\u8981\u65b9\u5f0f\u83b7\u53d6\u8fd9\u4e9b\u4fe1\u606f&#xff1a;<\/p>\n<li>\n<p>\u57fa\u4e8e\u6ce8\u89e3\u7684\u914d\u7f6e (Annotation-based Configuration &#8211; \u73b0\u4ee3\u4e3b\u6d41\u65b9\u5f0f):<\/p>\n<ul>\n<li>\u7ec4\u4ef6\u626b\u63cf (&#064;ComponentScan): \u5728\u914d\u7f6e\u7c7b&#xff08;\u6807\u6ce8 &#064;Configuration&#xff09;\u4e0a\u4f7f\u7528 &#064;ComponentScan(&#034;\u5305\u8def\u5f84&#034;)&#xff0c;\u544a\u8bc9\u5bb9\u5668\u53bb\u626b\u63cf\u6307\u5b9a\u5305\u53ca\u5176\u5b50\u5305\u4e0b\u6240\u6709\u6807\u6ce8\u4e86 &#064;Component, &#064;Service, &#064;Repository, &#064;Controller \u7684\u7c7b&#xff0c;\u5c06\u5b83\u4eec\u81ea\u52a8\u6ce8\u518c\u4e3a Bean\u3002<\/li>\n<li>\u81ea\u52a8\u88c5\u914d (&#064;Autowired \u7b49): \u5728\u6784\u9020\u5668\u3001setter \u65b9\u6cd5\u6216\u5b57\u6bb5\u4e0a\u4f7f\u7528 &#064;Autowired \u6ce8\u89e3&#xff0c;\u5bb9\u5668\u4f1a\u81ea\u52a8\u67e5\u627e\u5339\u914d\u7c7b\u578b\u7684 Bean \u8fdb\u884c\u6ce8\u5165&#xff08;\u6309\u7c7b\u578b\u4f18\u5148&#xff09;\u3002\n<ul>\n<li>\u67e5\u627e\u89c4\u5219 (\u6309\u987a\u5e8f):\n<li>\u6309\u7c7b\u578b\u67e5\u627e&#xff08;PaymentService&#xff09;\u3002<\/li>\n<li>\u5982\u679c\u627e\u5230\u591a\u4e2a\u8be5\u7c7b\u578b\u7684 Bean&#xff08;\u6bd4\u5982\u6709 CreditCardPaymentService \u548c PayPalPaymentService \u90fd\u5b9e\u73b0\u4e86 PaymentService&#xff09;&#xff0c;\u5219\u6309\u540d\u79f0\u5339\u914d&#xff08;\u53d8\u91cf\u540d\/\u53c2\u6570\u540d\u9700\u8981\u4e0e\u5176\u4e2d\u4e00\u4e2a Bean \u7684\u540d\u5b57\u5339\u914d&#xff09;\u3002<\/li>\n<li>\u6216\u8005\u4f7f\u7528 &#064;Qualifier(&#034;beanName&#034;) \u660e\u786e\u6307\u5b9a\u8981\u6ce8\u5165\u54ea\u4e2a\u7279\u5b9a\u540d\u79f0\u7684 Bean\u3002<\/li>\n<\/li>\n<\/ul>\n<\/li>\n<li>Java \u914d\u7f6e (&#064;Bean): \u5728 &#064;Configuration \u7c7b\u4e2d&#xff0c;\u901a\u8fc7 &#064;Bean \u6807\u6ce8\u7684\u65b9\u6cd5\u663e\u5f0f\u5b9a\u4e49 Bean\u3002\u53ef\u4ee5\u5728\u65b9\u6cd5\u53c2\u6570\u4e2d\u58f0\u660e\u4f9d\u8d56&#xff0c;\u5bb9\u5668\u4f1a\u6ce8\u5165\u5339\u914d\u7c7b\u578b\u7684 Bean\u3002<\/li>\n<\/ul>\n<p> <span class=\"token annotation punctuation\">&#064;Configuration<\/span><br \/>\n<span class=\"token annotation punctuation\">&#064;ComponentScan<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;com.example.services&#034;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token comment\">\/\/ \u626b\u63cf\u5305<\/span><br \/>\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">AppConfig<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u53ef\u9009&#xff1a;\u663e\u5f0f\u5b9a\u4e49\u4e00\u4e2aBean&#xff0c;\u65b9\u6cd5\u540d\u9ed8\u8ba4\u662fBean\u7684id<\/span><br \/>\n    <span class=\"token annotation punctuation\">&#064;Bean<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token class-name\">SpecialService<\/span> <span class=\"token function\">specialService<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">PaymentService<\/span> paymentService<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/\/ \u81ea\u52a8\u6ce8\u5165<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">SpecialService<\/span><span class=\"token punctuation\">(<\/span>paymentService<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span>\n <\/li>\n<li>\n<p>\u57fa\u4e8e XML \u7684\u914d\u7f6e (XML-based Configuration &#8211; \u8f83\u8001\u65b9\u5f0f&#xff0c;\u9010\u6b65\u88ab\u6ce8\u89e3\u66ff\u4ee3):<\/p>\n<ul>\n<li>\u5728 XML \u6587\u4ef6\u4e2d\u663e\u5f0f\u5b9a\u4e49 \u53ca\u5176<\/li>\n<li>\u4f7f\u7528 , , &#096;&#096; \u5143\u7d20\u624b\u52a8\u914d\u7f6e\u4f9d\u8d56\u3002<\/li>\n<li>\u73b0\u5728\u65b0\u5efa\u9879\u76ee\u8f83\u5c11\u63a8\u8350\u7eaf XML \u914d\u7f6e&#xff0c;\u901a\u5e38\u4e0e\u6ce8\u89e3\u7ed3\u5408\u6216\u53ea\u7528\u6ce8\u89e3\u3002<\/li>\n<\/ul>\n<\/li>\n<h2>Spring IoC\/DI \u5e26\u6765\u7684\u5de8\u5927\u597d\u5904<\/h2>\n<li>\u677e\u8026\u5408 (Loose Coupling): \u8fd9\u662f\u6700\u5927\u7684\u4f18\u52bf\u3002\u7c7b\u53ea\u4f9d\u8d56\u4e8e\u63a5\u53e3\/\u62bd\u8c61&#xff0c;\u4e0d\u4f9d\u8d56\u4e8e\u5177\u4f53\u5b9e\u73b0\u3002\u66f4\u6362\u5b9e\u73b0&#xff08;\u6bd4\u5982\u4ece CreditCardPaymentService \u6362\u6210 PayPalPaymentService&#xff09;\u53d8\u5f97\u5f02\u5e38\u5bb9\u6613&#xff0c;\u901a\u5e38\u53ea\u9700\u4fee\u6539\u914d\u7f6e&#xff08;&#064;Qualifier \u6216\u8005\u6362\u4e00\u4e2a &#064;Bean \u5b9a\u4e49&#xff09;&#xff0c;\u800c\u4e0d\u9700\u8981\u4fee\u6539\u4efb\u4f55\u4f9d\u8d56\u5b83\u7684\u7c7b\u7684\u6e90\u4ee3\u7801&#xff08;\u5982 OrderService&#xff09;\u3002<\/li>\n<li>\u589e\u5f3a\u53ef\u6d4b\u8bd5\u6027 (Improved Testability): \u53ef\u4ee5\u8f7b\u677e\u5730\u4e3a\u4f9d\u8d56\u9879\u521b\u5efa\u6a21\u62df\u5bf9\u8c61 (Mock) \u6216\u6869\u5bf9\u8c61 (Stub)\u3002\u5728\u6d4b\u8bd5 OrderService \u65f6&#xff0c;\u6ce8\u5165\u4e00\u4e2a\u6a21\u62df\u7684 PaymentService&#xff0c;\u9a8c\u8bc1 processOrder \u662f\u5426\u8c03\u7528\u4e86\u6b63\u786e\u7684\u652f\u4ed8\u65b9\u6cd5&#xff0c;\u800c\u4e0d\u6d89\u53ca\u771f\u5b9e\u7684\u652f\u4ed8\u903b\u8f91\u3002\u8fd9\u4f7f\u5f97\u5355\u5143\u6d4b\u8bd5\u771f\u6b63\u72ec\u7acb\u3001\u5feb\u901f\u4e14\u53ef\u9760\u3002<\/li>\n<li>\u63d0\u5347\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027 (Maintainability &amp; Extensibility): \u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316&#xff0c;\u804c\u8d23\u6e05\u6670\u3002\u6dfb\u52a0\u65b0\u529f\u80fd\u3001\u5f15\u5165\u65b0\u7b56\u7565&#xff08;\u5982\u65b0\u7684\u652f\u4ed8\u65b9\u5f0f&#xff09;\u901a\u5e38\u53ea\u9700\u6dfb\u52a0\u65b0\u7684\u5b9e\u73b0\u7c7b\u5e76\u8fdb\u884c\u7b80\u5355\u914d\u7f6e&#xff0c;\u5bf9\u73b0\u6709\u4ee3\u7801\u7684\u4fee\u6539\u6700\u5c0f\u5316\u751a\u81f3\u4e3a\u96f6\u3002<\/li>\n<li>\u7b80\u5316\u4ee3\u7801 (Simplified Code): \u5bf9\u8c61\u7684\u521b\u5efa\u3001\u4f9d\u8d56\u5173\u7cfb\u7684\u7ec4\u88c5\u548c\u590d\u6742\u521d\u59cb\u5316\u4ee3\u7801\u4ece\u4e1a\u52a1\u903b\u8f91\u4e2d\u79fb\u9664\u4e86\u3002\u4e1a\u52a1\u7c7b\u53d8\u5f97\u66f4\u7b80\u6d01\u3001\u66f4\u4e13\u6ce8\u4e8e\u6838\u5fc3\u804c\u8d23\u3002<\/li>\n<li>\u914d\u7f6e\u7ba1\u7406 (Configuration Management): \u96c6\u4e2d\u7ba1\u7406 Bean \u7684\u521b\u5efa\u548c\u4f9d\u8d56\u5173\u7cfb&#xff0c;\u65b9\u4fbf\u7edf\u4e00\u4fee\u6539&#xff08;\u5982\u5207\u6362\u4e0d\u540c\u73af\u5883 profile &#064;Profile&#xff09;\u3002<\/li>\n<li>\u751f\u547d\u5468\u671f\u7ba1\u7406 (Lifecycle Management): \u5bb9\u5668\u8d1f\u8d23\u7ba1\u7406 Bean \u7684\u6574\u4e2a\u751f\u547d\u5468\u671f&#xff08;\u521b\u5efa\u3001\u521d\u59cb\u5316\u3001\u9500\u6bc1&#xff09;&#xff0c;\u901a\u8fc7\u56de\u8c03&#xff08;\u5982 &#064;PostConstruct, &#064;PreDestroy&#xff09;\u5141\u8bb8\u5f00\u53d1\u8005\u63d2\u5165\u81ea\u5b9a\u4e49\u903b\u8f91\u3002<\/li>\n<h2>Spring IoC \u5bb9\u5668\u7684\u9ad8\u7ea7\u7279\u6027<\/h2>\n<ul>\n<li>Bean \u4f5c\u7528\u57df (Scope): Singleton&#xff08;\u9ed8\u8ba4&#xff0c;\u4e00\u4e2a\u5bb9\u5668\u4e00\u4e2a\u5b9e\u4f8b&#xff09;&#xff0c;Prototype&#xff08;\u6bcf\u6b21\u8bf7\u6c42\u521b\u5efa\u65b0\u5b9e\u4f8b&#xff09;&#xff0c;Request&#xff08;HTTP \u8bf7\u6c42&#xff09;&#xff0c;Session&#xff08;HTTP Session&#xff09;&#xff0c;Application&#xff08;ServletContext&#xff09;&#xff0c;WebSocket&#xff08;WebSocket Session&#xff09;\u3002<\/li>\n<li>\u6761\u4ef6\u5316\u88c5\u914d (Conditional Bean Registration): \u4f7f\u7528 &#064;Profile \u6216 &#064;Conditional&#xff08;\u53ca\u5176\u81ea\u5b9a\u4e49\u5b9e\u73b0&#xff09;\u6839\u636e\u7279\u5b9a\u6761\u4ef6&#xff08;\u5982\u73af\u5883\u53d8\u91cf\u3001\u7cfb\u7edf\u5c5e\u6027\u3001\u7c7b\u8def\u5f84\u662f\u5426\u5b58\u5728\u7b49&#xff09;\u51b3\u5b9a\u662f\u5426\u6ce8\u518c\u6216\u6fc0\u6d3b\u67d0\u4e2a Bean\u3002<\/li>\n<li>\u5ef6\u8fdf\u521d\u59cb\u5316 (Lazy Initialization): &#064;Lazy \u4f7f\u5f97 Bean \u53ea\u5728\u7b2c\u4e00\u6b21\u88ab\u8bf7\u6c42\u4f7f\u7528\u65f6\u624d\u521b\u5efa\u3002<\/li>\n<li>\u4e8b\u4ef6\u76d1\u542c (Application Events): \u5bb9\u5668\u652f\u6301\u53d1\u5e03\u548c\u76d1\u542c\u81ea\u5b9a\u4e49\u5e94\u7528\u4e8b\u4ef6&#xff0c;\u5b9e\u73b0\u4e86 Bean \u4e4b\u95f4\u7684\u4e00\u79cd\u677e\u8026\u5408\u901a\u4fe1\u673a\u5236\u3002<\/li>\n<\/ul>\n<h2>\u603b\u7ed3<\/h2>\n<ul>\n<li>\u63a7\u5236\u53cd\u8f6c (IoC) \u662f\u4e00\u4e2a\u6838\u5fc3\u8bbe\u8ba1\u539f\u5219&#xff1a;\u5c06\u521b\u5efa\u548c\u534f\u8c03\u4f9d\u8d56\u5bf9\u8c61\u7684\u8d23\u4efb\u53cd\u8f6c\u7ed9\u5916\u90e8\u5bb9\u5668\u3002\u5b83\u5b9e\u73b0\u4e86\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\u4e4b\u95f4\u7684\u89e3\u8026\u3002<\/li>\n<li>\u4f9d\u8d56\u6ce8\u5165 (DI) \u662f\u5b9e\u73b0 IoC \u539f\u5219\u7684\u4e3b\u8981\u8bbe\u8ba1\u6a21\u5f0f&#xff1a;\u5bb9\u5668\u5728\u521b\u5efa\u5bf9\u8c61\u65f6&#xff0c;\u5c06\u5176\u6240\u4f9d\u8d56\u7684\u5176\u4ed6\u5bf9\u8c61\u7684\u5f15\u7528\u6ce8\u5165\u8fdb\u53bb\u3002Spring \u63d0\u4f9b\u4e86\u6784\u9020\u5668\u6ce8\u5165&#xff08;\u6700\u4f73\u5b9e\u8df5&#xff09;\u3001setter \u6ce8\u5165\u548c\u5b57\u6bb5\u6ce8\u5165&#xff08;\u907f\u514d\u4f7f\u7528&#xff09;\u7b49\u65b9\u5f0f\u3002<\/li>\n<li>Spring IoC \u5bb9\u5668 (ApplicationContext) \u662f IoC\/DI \u7684\u6838\u5fc3\u673a\u5236&#xff1a;\u5b83\u8d1f\u8d23 Bean \u7684\u751f\u547d\u5468\u671f\u7ba1\u7406&#xff08;\u521b\u5efa\u3001\u914d\u7f6e\u3001\u88c5\u914d\u3001\u9500\u6bc1&#xff09;\u3002<\/li>\n<li>\u4e3b\u8981\u914d\u7f6e\u65b9\u5f0f&#xff1a; \u73b0\u4ee3 Spring \u9879\u76ee\u4e3b\u8981\u901a\u8fc7\u7ec4\u4ef6\u626b\u63cf (&#064;ComponentScan)\u3001\u81ea\u52a8\u88c5\u914d (&#064;Autowired) \u548c\u5728 &#064;Configuration \u7c7b\u4e2d\u4f7f\u7528 &#064;Bean \u65b9\u6cd5\u6765\u5b9a\u4e49\u548c\u7ec4\u88c5 Bean\u3002<\/li>\n<li>\u6838\u5fc3\u4f18\u52bf&#xff1a; \u677e\u8026\u5408\u3001\u53ef\u6d4b\u8bd5\u6027\u589e\u5f3a\u3001\u4ee3\u7801\u7b80\u5316\u3001\u6613\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55\u3002<\/li>\n<\/ul>\n<p>\u7406\u89e3 IoC \u548c DI \u662f\u638c\u63e1 Spring \u6846\u67b6\u7cbe\u9ad3\u7684\u5173\u952e\u7b2c\u4e00\u6b65\u3002 \u5b83\u4eec\u662f Spring \u80fd\u591f\u63d0\u4f9b\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\u3001AOP\u3001Spring MVC\u3001Spring Boot \u81ea\u52a8\u914d\u7f6e\u7b49\u8bf8\u591a\u5f3a\u5927\u529f\u80fd\u7684\u57fa\u7840\u67b6\u6784\u652f\u6491\u3002\u91c7\u7528 IoC\/DI \u6a21\u5f0f\u7f16\u5199\u7684\u5e94\u7528\u7a0b\u5e8f&#xff0c;\u5176\u7ed3\u6784\u66f4\u52a0\u4f18\u96c5\u3001\u5065\u58ee\u4e14\u6613\u4e8e\u6f14\u53d8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb450\u6b21\uff0c\u70b9\u8d5e15\u6b21\uff0c\u6536\u85cf14\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u63a7\u5236\u53cd\u8f6c(IoC)\u548c\u4f9d\u8d56\u6ce8\u5165(DI)\u7684\u6838\u5fc3\u6982\u5ff5\u53ca\u5176\u5728Spring\u6846\u67b6\u4e2d\u7684\u5b9e\u73b0\u3002IoC\u5c06\u4f9d\u8d56\u5bf9\u8c61\u7684\u521b\u5efa\u63a7\u5236\u6743\u4ece\u5e94\u7528\u7a0b\u5e8f\u8f6c\u79fb\u5230\u5916\u90e8\u5bb9\u5668\uff0c\u5b9e\u73b0\u89e3\u8026\u3002DI\u4f5c\u4e3aIoC\u7684\u4e3b\u8981\u5b9e\u73b0\u65b9\u5f0f\uff0cSpring\u63d0\u4f9b\u4e86\u4e09\u79cd\u6ce8\u5165\u65b9\u5f0f\uff1a\u6784\u9020\u5668\u6ce8\u5165\uff08\u63a8\u8350\uff09\u3001Setter\u65b9\u6cd5\u548c\u5b57\u6bb5\u6ce8\u5165\uff08\u4e0d\u63a8\u8350\uff09\u3002Spring IoC\u5bb9\u5668\u901a\u8fc7\u7ec4\u4ef6\u626b\u63cf\u548c\u6ce8\u89e3\u914d\u7f6e\u7ba1\u7406Bean\u7684\u751f\u547d\u5468\u671f\u53ca\u4f9d\u8d56\u5173\u7cfb\uff0c\u652f\u6301\u677e\u8026\u5408\u3001\u6613\u6d4b\u8bd5\u7684\u5e94\u7528\u7a0b\u5e8f\u8bbe\u8ba1\u3002\u6587\u7ae0\u901a\u8fc7\u652f\u4ed8\u670d\u52a1\u793a\u4f8b\uff0c\u5bf9\u6bd4\u4e86\u4f20\u7edf\u7d27\u8026\u5408\u65b9\u5f0f\u4e0eSpring DI\u65b9\u5f0f\u7684\u5dee\u5f02\uff0c\u7a81\u51fa\u4e86IoC\/DI\u5728\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027\u65b9\u9762\u7684\u4f18\u52bf\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,101,292],"topic":[],"class_list":["post-55140","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","tag-spring","tag-292"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010\u540e\u7aef\u3011Spring\u6846\u67b6\u63a7\u5236\u53cd\u8f6c\uff08IoC\uff09\u4e0e\u4f9d\u8d56\u6ce8\u5165\uff08DI\uff09\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/55140.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010\u540e\u7aef\u3011Spring\u6846\u67b6\u63a7\u5236\u53cd\u8f6c\uff08IoC\uff09\u4e0e\u4f9d\u8d56\u6ce8\u5165\uff08DI\uff09\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb450\u6b21\uff0c\u70b9\u8d5e15\u6b21\uff0c\u6536\u85cf14\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u63a7\u5236\u53cd\u8f6c(IoC)\u548c\u4f9d\u8d56\u6ce8\u5165(DI)\u7684\u6838\u5fc3\u6982\u5ff5\u53ca\u5176\u5728Spring\u6846\u67b6\u4e2d\u7684\u5b9e\u73b0\u3002IoC\u5c06\u4f9d\u8d56\u5bf9\u8c61\u7684\u521b\u5efa\u63a7\u5236\u6743\u4ece\u5e94\u7528\u7a0b\u5e8f\u8f6c\u79fb\u5230\u5916\u90e8\u5bb9\u5668\uff0c\u5b9e\u73b0\u89e3\u8026\u3002DI\u4f5c\u4e3aIoC\u7684\u4e3b\u8981\u5b9e\u73b0\u65b9\u5f0f\uff0cSpring\u63d0\u4f9b\u4e86\u4e09\u79cd\u6ce8\u5165\u65b9\u5f0f\uff1a\u6784\u9020\u5668\u6ce8\u5165\uff08\u63a8\u8350\uff09\u3001Setter\u65b9\u6cd5\u548c\u5b57\u6bb5\u6ce8\u5165\uff08\u4e0d\u63a8\u8350\uff09\u3002Spring IoC\u5bb9\u5668\u901a\u8fc7\u7ec4\u4ef6\u626b\u63cf\u548c\u6ce8\u89e3\u914d\u7f6e\u7ba1\u7406Bean\u7684\u751f\u547d\u5468\u671f\u53ca\u4f9d\u8d56\u5173\u7cfb\uff0c\u652f\u6301\u677e\u8026\u5408\u3001\u6613\u6d4b\u8bd5\u7684\u5e94\u7528\u7a0b\u5e8f\u8bbe\u8ba1\u3002\u6587\u7ae0\u901a\u8fc7\u652f\u4ed8\u670d\u52a1\u793a\u4f8b\uff0c\u5bf9\u6bd4\u4e86\u4f20\u7edf\u7d27\u8026\u5408\u65b9\u5f0f\u4e0eSpring DI\u65b9\u5f0f\u7684\u5dee\u5f02\uff0c\u7a81\u51fa\u4e86IoC\/DI\u5728\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027\u65b9\u9762\u7684\u4f18\u52bf\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/55140.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-13T09:39:52+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/55140.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/55140.html\",\"name\":\"\u3010\u540e\u7aef\u3011Spring\u6846\u67b6\u63a7\u5236\u53cd\u8f6c\uff08IoC\uff09\u4e0e\u4f9d\u8d56\u6ce8\u5165\uff08DI\uff09\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-13T09:39:52+00:00\",\"dateModified\":\"2025-08-13T09:39:52+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/55140.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/55140.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/55140.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010\u540e\u7aef\u3011Spring\u6846\u67b6\u63a7\u5236\u53cd\u8f6c\uff08IoC\uff09\u4e0e\u4f9d\u8d56\u6ce8\u5165\uff08DI\uff09\u89e3\u6790\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010\u540e\u7aef\u3011Spring\u6846\u67b6\u63a7\u5236\u53cd\u8f6c\uff08IoC\uff09\u4e0e\u4f9d\u8d56\u6ce8\u5165\uff08DI\uff09\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/55140.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010\u540e\u7aef\u3011Spring\u6846\u67b6\u63a7\u5236\u53cd\u8f6c\uff08IoC\uff09\u4e0e\u4f9d\u8d56\u6ce8\u5165\uff08DI\uff09\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb450\u6b21\uff0c\u70b9\u8d5e15\u6b21\uff0c\u6536\u85cf14\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u63a7\u5236\u53cd\u8f6c(IoC)\u548c\u4f9d\u8d56\u6ce8\u5165(DI)\u7684\u6838\u5fc3\u6982\u5ff5\u53ca\u5176\u5728Spring\u6846\u67b6\u4e2d\u7684\u5b9e\u73b0\u3002IoC\u5c06\u4f9d\u8d56\u5bf9\u8c61\u7684\u521b\u5efa\u63a7\u5236\u6743\u4ece\u5e94\u7528\u7a0b\u5e8f\u8f6c\u79fb\u5230\u5916\u90e8\u5bb9\u5668\uff0c\u5b9e\u73b0\u89e3\u8026\u3002DI\u4f5c\u4e3aIoC\u7684\u4e3b\u8981\u5b9e\u73b0\u65b9\u5f0f\uff0cSpring\u63d0\u4f9b\u4e86\u4e09\u79cd\u6ce8\u5165\u65b9\u5f0f\uff1a\u6784\u9020\u5668\u6ce8\u5165\uff08\u63a8\u8350\uff09\u3001Setter\u65b9\u6cd5\u548c\u5b57\u6bb5\u6ce8\u5165\uff08\u4e0d\u63a8\u8350\uff09\u3002Spring IoC\u5bb9\u5668\u901a\u8fc7\u7ec4\u4ef6\u626b\u63cf\u548c\u6ce8\u89e3\u914d\u7f6e\u7ba1\u7406Bean\u7684\u751f\u547d\u5468\u671f\u53ca\u4f9d\u8d56\u5173\u7cfb\uff0c\u652f\u6301\u677e\u8026\u5408\u3001\u6613\u6d4b\u8bd5\u7684\u5e94\u7528\u7a0b\u5e8f\u8bbe\u8ba1\u3002\u6587\u7ae0\u901a\u8fc7\u652f\u4ed8\u670d\u52a1\u793a\u4f8b\uff0c\u5bf9\u6bd4\u4e86\u4f20\u7edf\u7d27\u8026\u5408\u65b9\u5f0f\u4e0eSpring DI\u65b9\u5f0f\u7684\u5dee\u5f02\uff0c\u7a81\u51fa\u4e86IoC\/DI\u5728\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027\u65b9\u9762\u7684\u4f18\u52bf\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/55140.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-13T09:39:52+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/55140.html","url":"https:\/\/www.wsisp.com\/helps\/55140.html","name":"\u3010\u540e\u7aef\u3011Spring\u6846\u67b6\u63a7\u5236\u53cd\u8f6c\uff08IoC\uff09\u4e0e\u4f9d\u8d56\u6ce8\u5165\uff08DI\uff09\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-13T09:39:52+00:00","dateModified":"2025-08-13T09:39:52+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/55140.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/55140.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/55140.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010\u540e\u7aef\u3011Spring\u6846\u67b6\u63a7\u5236\u53cd\u8f6c\uff08IoC\uff09\u4e0e\u4f9d\u8d56\u6ce8\u5165\uff08DI\uff09\u89e3\u6790"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/55140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=55140"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/55140\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=55140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=55140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=55140"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=55140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}