{"id":10006441,"date":"2022-09-08T18:36:37","date_gmt":"2022-09-08T10:36:37","guid":{"rendered":"https:\/\/ityet.com\/?p=10006441"},"modified":"2022-09-08T18:36:37","modified_gmt":"2022-09-08T10:36:37","slug":"%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9aautowired-resource-inject-%e4%b8%89%e4%b8%aa%e6%b3%a8%e8%a7%a3%e7%9a%84%e5%8c%ba%e5%88%ab%ef%bc%9f%e4%b8%80%e4%b8%8b%e6%87%b5%e4%ba%86%e3%80%82%e3%80%82","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006441","title":{"rendered":"\u9762\u8bd5\u5b98\uff1a@Autowired, @Resource,  @Inject \u4e09\u4e2a\u6ce8\u89e3\u7684\u533a\u522b\uff1f\u4e00\u4e0b\u61f5\u4e86\u3002\u3002\u3002"},"content":{"rendered":"<p>\u4f5c\u8005\uff1aRichard_Yi\/<br \/>\u6765\u6e90\uff1ajuejin.cn\/post\/6844904056230690824<\/p>\n<h2><strong>\u524d\u8a00<\/strong><\/h2>\n<p>\u672c\u7ae0\u7684\u5185\u5bb9\u4e3b\u8981\u662f\u60f3\u63a2\u8ba8\u6211\u4eec\u5728\u8fdb\u884cSpring \u5f00\u53d1\u8fc7\u7a0b\u5f53\u4e2d\uff0c\u5173\u4e8e\u4f9d\u8d56\u6ce8\u5165\u7684\u51e0\u4e2a\u77e5\u8bc6\u70b9\u3002\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u5148\u770b\u4e0b\u4ee5\u4e0b\u95ee\u9898\uff1a<\/p>\n<ul>\n<li><code>@Autowired<\/code>, <code>@Resource<\/code>,  <code>@Inject<\/code> \u4e09\u4e2a\u6ce8\u89e3\u7684\u533a\u522b<\/li>\n<li>\u5f53\u4f60\u5728\u4f7f\u7528<code>@Autowired<\/code>\u65f6\uff0c\u662f\u5426\u6709\u51fa\u73b0\u8fc7<code>Field injection is not recommended<\/code>\u7684\u8b66\u544a\uff1f\u4f60\u77e5\u9053\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5417\uff1f<\/li>\n<li>Spring \u4f9d\u8d56\u6ce8\u5165\u6709\u54ea\u51e0\u79cd\u65b9\u5f0f\uff1f\u5b98\u65b9\u662f\u600e\u4e48\u5efa\u8bae\u4f7f\u7528\u7684\u5462\uff1f<\/li>\n<\/ul>\n<hr>\n<p>\u5982\u679c\u4f60\u5bf9\u4e0a\u8ff0\u95ee\u9898\u90fd\u4e86\u89e3\uff0c\u90a3\u6211\u4e2a\u4eba\u89c9\u5f97\u4f60\u7684\u5f00\u53d1\u7ecf\u9a8c\u5e94\u8be5\u662f\u4e0d\u9519\u7684\ud83d\udc4d\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u5c31\u4f9d\u6b21\u5bf9\u4e0a\u8ff0\u95ee\u9898\u8fdb\u884c\u89e3\u7b54\uff0c\u5e76\u4e14\u603b\u7ed3\u77e5\u8bc6\u70b9\u3002<\/p>\n<h2><strong><code>@Autowired<\/code>, <code>@Resource<\/code>,  <code>@Inject<\/code> \u4e09\u4e2a\u6ce8\u89e3\u7684\u533a\u522b<\/strong><\/h2>\n<p>Spring \u652f\u6301\u4f7f\u7528<code>@Autowired<\/code>, <code>@Resource<\/code>,  <code>@Inject<\/code> \u4e09\u4e2a\u6ce8\u89e3\u8fdb\u884c\u4f9d\u8d56\u6ce8\u5165\u3002\u4e0b\u9762\u6765\u4ecb\u7ecd\u4e00\u4e0b\u8fd9\u4e09\u4e2a\u6ce8\u89e3\u6709\u4ec0\u4e48\u533a\u522b\u3002<\/p>\n<h3>@Autowired<\/h3>\n<p><code>@Autowired<\/code>\u4e3aSpring \u6846\u67b6\u63d0\u4f9b\u7684\u6ce8\u89e3\uff0c\u9700\u8981\u5bfc\u5165\u5305<code>org.springframework.beans.factory.annotation.Autowired<\/code>\u3002<\/p>\n<p>\u8fd9\u91cc\u5148\u7ed9\u51fa\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u65b9\u4fbf\u8bb2\u89e3\u8bf4\u660e\uff1a<\/p>\n<pre><code>public interface Svc {\n\n    void sayHello();\n}\n\n@Service\npublic class SvcA implements Svc {\n\n    @Override\n    public void sayHello() {\n        System.out.println(&quot;hello, this is service A&quot;);\n    }\n\n}\n\n@Service\npublic class SvcB implements Svc {\n\n    @Override\n    public void sayHello() {\n        System.out.println(&quot;hello, this is service B&quot;);\n    }\n\n}\n\n@Service\npublic class SvcC implements Svc {\n\n    @Override\n    public void sayHello() {\n        System.out.println(&quot;hello, this is service C&quot;);\n    }\n}<\/code><\/pre>\n<p>\u6d4b\u8bd5\u7c7b\uff1a<\/p>\n<pre><code>@SpringBootTest\npublic class SimpleTest {\n\n    @Autowired\n    \/\/ @Qualifier(&quot;svcA&quot;)\n    Svc svc;\n\n    @Test\n    void rc() {\n        Assertions.assertNotNull(svc);\n        svc.sayHello();\n    }\n\n}<\/code><\/pre>\n<p><strong>\u88c5\u914d\u987a\u5e8f\uff1a<\/strong><\/p>\n<p>1.\u6309\u7167<code>type<\/code>\u5728\u4e0a\u4e0b\u6587\u4e2d\u67e5\u627e\u5339\u914d\u7684bean\uff0c<code>\u67e5\u627etype\u4e3aSvc\u7684bean<\/code><\/p>\n<p>2.\u5982\u679c\u6709\u591a\u4e2abean\uff0c\u5219\u6309\u7167<code>name<\/code>\u8fdb\u884c\u5339\u914d<\/p>\n<ul>\n<li>\u5982\u679c\u6709<code>@Qualifier<\/code>\u6ce8\u89e3\uff0c\u5219\u6309\u7167<code>@Qualifier<\/code>\u6307\u5b9a\u7684<code>name<\/code>\u8fdb\u884c\u5339\u914d\uff0c<code>\u67e5\u627ename\u4e3asvcA\u7684bean<\/code><\/li>\n<li>\u5982\u679c\u6ca1\u6709\uff0c\u5219\u6309\u7167\u53d8\u91cf\u540d\u8fdb\u884c\u5339\u914d\uff0c<code>\u67e5\u627ename\u4e3asvcA\u7684bean<\/code><\/li>\n<\/ul>\n<p>3.\u5339\u914d\u4e0d\u5230\uff0c\u5219\u62a5\u9519\u3002\uff08<code>@Autowired(required=false)<\/code>\uff0c\u5982\u679c\u8bbe\u7f6e<code>required<\/code>\u4e3a<code>false<\/code>(\u9ed8\u8ba4\u4e3a<code>true<\/code>)\uff0c\u5219\u6ce8\u5165\u5931\u8d25\u65f6\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\uff09<\/p>\n<h3>@Inject<\/h3>\n<p>\u5728Spring \u7684\u73af\u5883\u4e0b\uff0c<code>@Inject<\/code>\u548c<code>@Autowired<\/code> \u662f\u76f8\u540c\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u7684\u4f9d\u8d56\u6ce8\u5165\u90fd\u662f\u4f7f\u7528<code>AutowiredAnnotationBeanPostProcessor<\/code>\u6765\u5904\u7406\u7684\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVcVDc7\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong><code>@Inject<\/code>\u662f JSR-330 \u5b9a\u4e49\u7684\u89c4\u8303<\/strong>\uff0c\u5982\u679c\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5207\u6362\u5230<code>Guice<\/code>\u4e5f\u662f\u53ef\u4ee5\u7684\u3002<\/p>\n<blockquote><p>Guice \u662f google \u5f00\u6e90\u7684\u8f7b\u91cf\u7ea7 DI \u6846\u67b6<\/p><\/blockquote>\n<p>\u5982\u679c\u786c\u8981\u8bf4\u4e24\u4e2a\u7684\u533a\u522b\uff0c\u9996\u5148<code>@Inject<\/code>\u662fJava EE\u5305\u91cc\u7684\uff0c\u5728SE\u73af\u5883\u9700\u8981\u5355\u72ec\u5f15\u5165\u3002\u53e6\u4e00\u4e2a\u533a\u522b\u5728\u4e8e<code>@Autowired<\/code>\u53ef\u4ee5\u8bbe\u7f6e<code>required=false<\/code>\u800c<code>@Inject<\/code>\u5e76\u6ca1\u6709\u8fd9\u4e2a\u5c5e\u6027\u3002<\/p>\n<h3>@Resource<\/h3>\n<p><code>@Resource<\/code>\u662fJSR-250\u5b9a\u4e49\u7684\u6ce8\u89e3\u3002Spring \u5728 <code>CommonAnnotationBeanPostProcessor<\/code>\u5b9e\u73b0\u4e86\u5bf9<code>JSR-250<\/code>\u7684\u6ce8\u89e3\u7684\u5904\u7406\uff0c\u5176\u4e2d\u5c31\u5305\u62ec<code>@Resource<\/code>\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVcVDc8\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><code>@Resource<\/code>\u6709\u4e24\u4e2a\u91cd\u8981\u7684\u5c5e\u6027\uff1a<code>name<\/code>\u548c<code>type<\/code>\uff0c\u800cSpring \u5c06<code>@Resource<\/code>\u6ce8\u89e3\u7684<code>name<\/code>\u5c5e\u6027\u89e3\u6790\u4e3abean\u7684\u540d\u5b57\uff0c\u800c<code>type<\/code>\u5c5e\u6027\u5219\u89e3\u6790\u4e3abean\u7684\u7c7b\u578b\u3002<\/p>\n<p><strong>\u88c5\u914d\u987a\u5e8f\uff1a<\/strong><\/p>\n<ol>\n<li>\u5982\u679c\u540c\u65f6\u6307\u5b9a\u4e86<code>name<\/code>\u548c<code>type<\/code>\uff0c\u5219\u4eceSpring\u4e0a\u4e0b\u6587\u4e2d\u627e\u5230\u552f\u4e00\u5339\u914d\u7684bean\u8fdb\u884c\u88c5\u914d\uff0c\u627e\u4e0d\u5230\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<li>\u5982\u679c\u6307\u5b9a\u4e86<code>name<\/code>\uff0c\u5219\u4ece\u4e0a\u4e0b\u6587\u4e2d\u67e5\u627e\u540d\u79f0\uff08id\uff09\u5339\u914d\u7684bean\u8fdb\u884c\u88c5\u914d\uff0c\u627e\u4e0d\u5230\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<li>\u5982\u679c\u6307\u5b9a\u4e86<code>type<\/code>\uff0c\u5219\u4ece\u4e0a\u4e0b\u6587\u4e2d\u627e\u5230\u7c7b\u578b\u5339\u914d\u7684\u552f\u4e00bean\u8fdb\u884c\u88c5\u914d\uff0c\u627e\u4e0d\u5230\u6216\u662f\u627e\u5230\u591a\u4e2a\uff0c\u90fd\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<li>\u5982\u679c\u65e2\u6ca1\u6709\u6307\u5b9a<code>name<\/code>\uff0c\u53c8\u6ca1\u6709\u6307\u5b9a<code>type<\/code>\uff0c\u5219\u9ed8\u8ba4\u6309\u7167<code>byName<\/code>\u65b9\u5f0f\u8fdb\u884c\u88c5\u914d\uff1b\u5982\u679c\u6ca1\u6709\u5339\u914d\uff0c\u6309\u7167<code>byType<\/code>h\u8fdb\u884c\u88c5\u914d\u3002<\/li>\n<\/ol>\n<h2><strong>IDEA \u63d0\u793a <code>Field injection is not recommended<\/code><\/strong><\/h2>\n<p>\u5728\u4f7f\u7528IDEA \u8fdb\u884cSpring \u5f00\u53d1\u7684\u65f6\u5019\uff0c\u5f53\u4f60\u5728\u5b57\u6bb5\u4e0a\u9762\u4f7f\u7528<code>@Autowired<\/code>\u6ce8\u89e3\u7684\u65f6\u5019\uff0c\u4f60\u4f1a\u53d1\u73b0IDEA \u4f1a\u6709\u8b66\u544a\u63d0\u793a\uff1a<\/p>\n<blockquote>\n<p>Field injection is not recommended<\/p>\n<p>Inspection info: Spring Team Recommends: &#8220;Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies&#8221;.<\/p>\n<\/blockquote>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVcVG3O\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u7ffb\u8bd1\u8fc7\u6765\u5c31\u662f\u8fd9\u4e2a\u610f\u601d\uff1a<\/p>\n<blockquote>\n<p>\u4e0d\u5efa\u8bae\u4f7f\u7528\u57fa\u4e8e field \u7684\u6ce8\u5165\u65b9\u5f0f\u3002<\/p>\n<p>Spring \u5f00\u53d1\u56e2\u961f\u5efa\u8bae\uff1a\u5728\u4f60\u7684Spring Bean \u6c38\u8fdc\u4f7f\u7528\u57fa\u4e8econstructor \u7684\u65b9\u5f0f\u8fdb\u884c\u4f9d\u8d56\u6ce8\u5165\u3002\u5bf9\u4e8e\u5fc5\u987b\u7684\u4f9d\u8d56\uff0c\u6c38\u8fdc\u4f7f\u7528\u65ad\u8a00\u6765\u786e\u8ba4\u3002<\/p>\n<\/blockquote>\n<p>\u6bd4\u5982\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code>@Service\npublic class HelpService {\n    @Autowired\n    @Qualifier(&quot;svcB&quot;)\n    private Svc svc;\n\n    public void sayHello() {\n        svc.sayHello();\n    }\n}\n\npublic interface Svc {\n    void sayHello();\n}\n\n@Service\npublic class SvcB implements Svc {\n    @Override\n    public void sayHello() {\n        System.out.println(&quot;hello, this is service B&quot;);\n    }\n}<\/code><\/pre>\n<p>\u5c06\u5149\u6807\u653e\u5230<code>@Autowired<\/code>\u5904\uff0c\u4f7f\u7528<code>Alt + Enter<\/code> \u5feb\u6377\u8fdb\u884c\u4fee\u6539\u4e4b\u540e\uff0c\u4ee3\u7801\u5c31\u4f1a\u53d8\u6210\u57fa\u4e8eConstructor\u7684\u6ce8\u5165\u65b9\u5f0f\uff0c<strong>\u4fee\u6539\u4e4b\u540e<\/strong>\uff1a<\/p>\n<pre><code>@Service\npublic class HelpService {\n    private final Svc svc;\n\n    @Autowired\n    public HelpService(@Qualifier(&quot;svcB&quot;) Svc svc) {\n        \/\/ Assert.notNull(svc, &quot;svc must not be null&quot;);\n        this.svc = svc;\n    }\n\n    public void sayHello() {\n        svc.sayHello();\n    }\n}<\/code><\/pre>\n<p>\u5982\u679c\u6309\u7167Spring \u56e2\u961f\u7684\u5efa\u8bae\uff0c\u5982\u679c<code>svc<\/code>\u662f\u5fc5\u987b\u7684\u4f9d\u8d56\uff0c\u5e94\u8be5\u4f7f\u7528<code>Assert.notNull(svc, &quot;svc must not be null&quot;)<\/code>\u6765\u786e\u8ba4\u3002<\/p>\n<p>\u4fee\u6b63\u8fd9\u4e2a\u8b66\u544a\u63d0\u793a\u56fa\u7136\u7b80\u5355\uff0c\u4f46\u662f\u6211\u89c9\u5f97\u66f4\u91cd\u8981\u662f\u53bb\u7406\u89e3\u4e3a\u4ec0\u4e48Spring \u56e2\u961f\u4f1a\u63d0\u51fa\u8fd9\u6837\u7684\u5efa\u8bae\uff1f\u76f4\u63a5\u4f7f\u7528\u8fd9\u79cd\u57fa\u4e8e field \u7684\u6ce8\u5165\u65b9\u5f0f\u6709\u4ec0\u4e48\u95ee\u9898\uff1f<\/p>\n<p>\u63a8\u8350\u4e00\u4e2a Spring Boot \u57fa\u7840\u6559\u7a0b\u53ca\u5b9e\u6218\u793a\u4f8b\uff1a<\/p>\n<blockquote><p><a href=\"https:\/\/link.segmentfault.com\/?enc=BDYTe4aYLCMj48DRrv%2FWZA%3D%3D.EcyawlBeeyF5kDc5NMmZeIvP75Y9C%2B6wNc%2BlLZBnrWTPfMqtDe3i5q6BXxX1lEh1Xn9VEI3X%2FZSUpMFTXqQmXA%3D%3D\" rel=\"nofollow\">https:\/\/github.com\/javastacks&#8230;<\/a><\/p><\/blockquote>\n<hr>\n<p><strong>\u9996\u5148\u6211\u4eec\u9700\u8981\u77e5\u9053\uff0cSpring \u4e2d\u6709\u8fd9\u4e483\u79cd\u4f9d\u8d56\u6ce8\u5165\u7684\u65b9\u5f0f<\/strong>\uff1a<\/p>\n<ul>\n<li>\u57fa\u4e8e field \u6ce8\u5165\uff08\u5c5e\u6027\u6ce8\u5165\uff09<\/li>\n<li>\u57fa\u4e8e setter \u6ce8\u5165<\/li>\n<li>\u57fa\u4e8e constructor \u6ce8\u5165\uff08\u6784\u9020\u5668\u6ce8\u5165\uff09<\/li>\n<\/ul>\n<h3>1. \u57fa\u4e8e field \u6ce8\u5165<\/h3>\n<p>\u6240\u8c13\u57fa\u4e8e field \u6ce8\u5165\uff0c\u5c31\u662f\u5728bean\u7684\u53d8\u91cf\u4e0a\u4f7f\u7528\u6ce8\u89e3\u8fdb\u884c\u4f9d\u8d56\u6ce8\u5165\u3002\u672c\u8d28\u4e0a\u662f\u901a\u8fc7\u53cd\u5c04\u7684\u65b9\u5f0f\u76f4\u63a5\u6ce8\u5165\u5230field\u3002\u8fd9\u662f\u6211\u5e73\u5e38\u5f00\u53d1\u4e2d\u770b\u7684\u6700\u591a\u4e5f\u662f\u6700\u719f\u6089\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u540c\u65f6\uff0c\u4e5f\u6b63\u662f Spring \u56e2\u961f\u6240\u4e0d\u63a8\u8350\u7684\u65b9\u5f0f\u3002\u6bd4\u5982\uff1a<\/p>\n<pre><code>@Autowired\nprivate Svc svc;<\/code><\/pre>\n<h3>2. \u57fa\u4e8e setter \u65b9\u6cd5\u6ce8\u5165<\/h3>\n<p>\u901a\u8fc7\u5bf9\u5e94\u53d8\u91cf\u7684<code>setXXX()<\/code>\u65b9\u6cd5\u4ee5\u53ca\u5728\u65b9\u6cd5\u4e0a\u9762\u4f7f\u7528\u6ce8\u89e3\uff0c\u6765\u5b8c\u6210\u4f9d\u8d56\u6ce8\u5165\u3002\u6bd4\u5982\uff1a<\/p>\n<pre><code>private Helper helper;\n\n@Autowired\npublic void setHelper(Helper helper) {\n    this.helper = helper;\n}<\/code><\/pre>\n<blockquote><p>\u6ce8\uff1a\u5728 <code>Spring 4.3<\/code> \u53ca\u4ee5\u540e\u7684\u7248\u672c\u4e2d\uff0csetter \u4e0a\u9762\u7684 <code>@Autowired<\/code>\u6ce8\u89e3\u662f\u53ef\u4ee5\u4e0d\u5199\u7684\u3002<\/p><\/blockquote>\n<h3>3. \u57fa\u4e8e constructor \u6ce8\u5165<\/h3>\n<p>\u5c06\u5404\u4e2a\u5fc5\u9700\u7684\u4f9d\u8d56\u5168\u90e8\u653e\u5728\u5e26\u6709\u6ce8\u89e3\u6784\u9020\u65b9\u6cd5\u7684\u53c2\u6570\u4e2d\uff0c\u5e76\u5728\u6784\u9020\u65b9\u6cd5\u4e2d\u5b8c\u6210\u5bf9\u5e94\u53d8\u91cf\u7684\u521d\u59cb\u5316\uff0c\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5c31\u662f\u57fa\u4e8e\u6784\u9020\u65b9\u6cd5\u7684\u6ce8\u5165\u3002\u6bd4\u5982\uff1a<\/p>\n<pre><code>private final Svc svc;\n\n@Autowired\npublic HelpService(@Qualifier(&quot;svcB&quot;) Svc svc) {\n    this.svc = svc;\n}<\/code><\/pre>\n<blockquote><p>\u5728 <code>Spring 4.3<\/code> \u53ca\u4ee5\u540e\u7684\u7248\u672c\u4e2d\uff0c\u5982\u679c\u8fd9\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u6784\u9020\u65b9\u6cd5\uff0c\u90a3\u4e48\u8fd9\u4e2a\u6784\u9020\u65b9\u6cd5\u4e0a\u9762\u4e5f\u53ef\u4ee5\u4e0d\u5199 <code>@Autowired<\/code> \u6ce8\u89e3\u3002<\/p><\/blockquote>\n<h4>\u57fa\u4e8e field \u6ce8\u5165\u7684\u597d\u5904<\/h4>\n<p>\u6b63\u5982\u4f60\u6240\u89c1\uff0c\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u7684\u7b80\u6d01\uff0c\u4ee3\u7801\u770b\u8d77\u6765\u5f88\u7b80\u5355\uff0c\u901a\u4fd7\u6613\u61c2\u3002\u4f60\u7684\u7c7b\u53ef\u4ee5\u4e13\u6ce8\u4e8e\u4e1a\u52a1\u800c\u4e0d\u88ab\u4f9d\u8d56\u6ce8\u5165\u6240\u6c61\u67d3\u3002\u4f60\u53ea\u9700\u8981\u628a<code>@Autowired<\/code>\u6254\u5230\u53d8\u91cf\u4e4b\u4e0a\u5c31\u597d\u4e86\uff0c\u4e0d\u9700\u8981\u7279\u6b8a\u7684\u6784\u9020\u5668\u6216\u8005set\u65b9\u6cd5\uff0c\u4f9d\u8d56\u6ce8\u5165\u5bb9\u5668\u4f1a\u63d0\u4f9b\u4f60\u6240\u9700\u7684\u4f9d\u8d56\u3002<\/p>\n<h4>\u57fa\u4e8e field \u6ce8\u5165\u7684\u574f\u5904<\/h4>\n<blockquote><p>\u6210\u4e5f\u8427\u4f55\u8d25\u4e5f\u8427\u4f55<\/p><\/blockquote>\n<p>\u57fa\u4e8e field \u6ce8\u5165\u867d\u7136\u7b80\u5355\uff0c\u4f46\u662f\u5374\u4f1a\u5f15\u53d1\u5f88\u591a\u7684\u95ee\u9898\u3002\u8fd9\u4e9b\u95ee\u9898\u5728\u6211\u5e73\u5e38\u5f00\u53d1\u9605\u8bfb\u9879\u76ee\u4ee3\u7801\u7684\u65f6\u5019\u5c31\u7ecf\u5e38\u9047\u89c1\u3002<\/p>\n<p><strong>\u5bb9\u6613\u8fdd\u80cc\u4e86\u5355\u4e00\u804c\u8d23\u539f\u5219<\/strong><\/p>\n<p>\u4f7f\u7528\u8fd9\u79cd\u57fa\u4e8e field \u6ce8\u5165\u7684\u65b9\u5f0f\uff0c\u6dfb\u52a0\u4f9d\u8d56\u662f\u5f88\u7b80\u5355\u7684\uff0c\u5c31\u7b97\u4f60\u7684\u7c7b\u4e2d\u6709\u5341\u51e0\u4e2a\u4f9d\u8d56\u4f60\u53ef\u80fd\u90fd\u89c9\u5f97\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\uff0c\u666e\u901a\u7684\u5f00\u53d1\u8005\u5f88\u53ef\u80fd\u4f1a\u65e0\u610f\u8bc6\u5730\u7ed9\u4e00\u4e2a\u7c7b\u6dfb\u52a0\u5f88\u591a\u7684\u4f9d\u8d56\u3002<\/p>\n<p>\u4f46\u662f\u5f53\u4f7f\u7528\u6784\u9020\u5668\u65b9\u5f0f\u6ce8\u5165\uff0c\u5230\u4e86\u67d0\u4e2a\u7279\u5b9a\u7684\u70b9\uff0c\u6784\u9020\u5668\u4e2d\u7684\u53c2\u6570\u53d8\u5f97\u592a\u591a\u4ee5\u81f3\u4e8e\u5f88\u660e\u663e\u5730\u53d1\u73b0something is wrong\u3002\u62e5\u6709\u592a\u591a\u7684\u4f9d\u8d56\u901a\u5e38\u610f\u5473\u7740\u4f60\u7684\u7c7b\u8981\u627f\u62c5\u66f4\u591a\u7684\u8d23\u4efb\uff0c\u660e\u663e\u8fdd\u80cc\u4e86\u5355\u4e00\u804c\u8d23\u539f\u5219\uff08SRP\uff1aSingle responsibility principle\uff09\u3002<\/p>\n<blockquote><p>\u8fd9\u4e2a\u95ee\u9898\u5728\u6211\u53f8\u7684\u9879\u76ee\u4ee3\u7801\u771f\u7684\u5f88\u5e38\u89c1\u3002<\/p><\/blockquote>\n<p><strong>\u4f9d\u8d56\u6ce8\u5165\u4e0e\u5bb9\u5668\u672c\u8eab\u8026\u5408<\/strong><\/p>\n<p>\u4f9d\u8d56\u6ce8\u5165\u6846\u67b6\u7684\u6838\u5fc3\u601d\u60f3\u4e4b\u4e00\u5c31\u662f\u53d7\u5bb9\u5668\u7ba1\u7406\u7684\u7c7b\u4e0d\u5e94\u8be5\u53bb\u4f9d\u8d56\u5bb9\u5668\u6240\u4f7f\u7528\u7684\u4f9d\u8d56\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u8fd9\u4e2a\u7c7b\u5e94\u8be5\u662f\u4e00\u4e2a\u7b80\u5355\u7684POJO(Plain Ordinary Java Object)\u80fd\u591f\u88ab\u5355\u72ec\u5b9e\u4f8b\u5316\u5e76\u4e14\u4f60\u4e5f\u80fd\u4e3a\u5b83\u63d0\u4f9b\u5b83\u6240\u9700\u7684\u4f9d\u8d56\u3002<\/p>\n<p>\u8fd9\u4e2a\u95ee\u9898\u5177\u4f53\u53ef\u4ee5\u8868\u73b0\u5728\uff1a<\/p>\n<ul>\n<li>\u4f60\u7684\u7c7b\u548c\u4f9d\u8d56\u5bb9\u5668\u5f3a\u8026\u5408\uff0c\u4e0d\u80fd\u5728\u5bb9\u5668\u5916\u4f7f\u7528<\/li>\n<li>\u4f60\u7684\u7c7b\u4e0d\u80fd\u7ed5\u8fc7\u53cd\u5c04\uff08\u4f8b\u5982\u5355\u5143\u6d4b\u8bd5\u7684\u65f6\u5019\uff09\u8fdb\u884c\u5b9e\u4f8b\u5316\uff0c\u5fc5\u987b\u901a\u8fc7\u4f9d\u8d56\u5bb9\u5668\u624d\u80fd\u5b9e\u4f8b\u5316\uff0c\u8fd9\u66f4\u50cf\u662f\u96c6\u6210\u6d4b\u8bd5<\/li>\n<\/ul>\n<p><strong>\u4e0d\u80fd\u4f7f\u7528\u5c5e\u6027\u6ce8\u5165\u7684\u65b9\u5f0f\u6784\u5efa\u4e0d\u53ef\u53d8\u5bf9\u8c61(<code>final<\/code> \u4fee\u9970\u7684\u53d8\u91cf)<\/strong><\/p>\n<h4>Spring \u5f00\u53d1\u56e2\u961f\u7684\u5efa\u8bae<\/h4>\n<blockquote><p>Since you can mix constructor-based and setter-based DI, it is a good rule of thumb to use constructors for mandatory dependencies and setter methods or configuration methods for optional dependencies.<\/p><\/blockquote>\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f<\/p>\n<ul>\n<li>\u5f3a\u5236\u4f9d\u8d56\u5c31\u7528\u6784\u9020\u5668\u65b9\u5f0f<\/li>\n<li>\u53ef\u9009\u3001\u53ef\u53d8\u7684\u4f9d\u8d56\u5c31\u7528setter \u6ce8\u5165<\/li>\n<\/ul>\n<p>\u5f53\u7136\u4f60\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\u3002\u6784\u9020\u5668\u6ce8\u5165\u66f4\u9002\u5408\u5f3a\u5236\u6027\u7684\u6ce8\u5165\u65e8\u5728\u4e0d\u53d8\u6027\uff0cSetter\u6ce8\u5165\u66f4\u9002\u5408\u53ef\u53d8\u6027\u7684\u6ce8\u5165\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u770b\u770bSpring \u8fd9\u6837\u63a8\u8350\u7684\u7406\u7531\uff0c\u9996\u5148\u662f\u57fa\u4e8e\u6784\u9020\u65b9\u6cd5\u6ce8\u5165\uff0c<\/p>\n<blockquote><p>The Spring team generally advocates constructor injection as it enables one to implement application components as immutable objects and to ensure that required dependencies are not null. Furthermore constructor-injected components are always returned to client (calling) code in a fully initialized state. As a side note, a large number of constructor arguments is a bad code smell, implying that the class likely has too many responsibilities and should be refactored to better address proper separation of concerns.<\/p><\/blockquote>\n<p>Spring \u56e2\u961f\u63d0\u5021\u4f7f\u7528\u57fa\u4e8e\u6784\u9020\u65b9\u6cd5\u7684\u6ce8\u5165\uff0c\u56e0\u4e3a\u8fd9\u6837\u4e00\u65b9\u9762\u53ef\u4ee5<strong>\u5c06\u4f9d\u8d56\u6ce8\u5165\u5230\u4e00\u4e2a\u4e0d\u53ef\u53d8\u7684\u53d8\u91cf\u4e2d (\u6ce8\uff1a<code>final<\/code> \u4fee\u9970\u7684\u53d8\u91cf)<\/strong>\uff0c\u53e6\u4e00\u65b9\u9762\u4e5f\u53ef\u4ee5<strong>\u4fdd\u8bc1\u8fd9\u4e9b\u53d8\u91cf\u7684\u503c\u4e0d\u4f1a\u662f null<\/strong>\u3002\u6b64\u5916\uff0c\u7ecf\u8fc7\u6784\u9020\u65b9\u6cd5\u5b8c\u6210\u4f9d\u8d56\u6ce8\u5165\u7684\u7ec4\u4ef6 (\u6ce8\uff1a\u6bd4\u5982\u5404\u4e2a <code>service<\/code>)\uff0c\u5728\u88ab\u8c03\u7528\u65f6\u53ef\u4ee5<strong>\u4fdd\u8bc1\u5b83\u4eec\u90fd\u5b8c\u5168\u51c6\u5907\u597d\u4e86<\/strong>\u3002\u4e0e\u6b64\u540c\u65f6\uff0c\u4ece\u4ee3\u7801\u8d28\u91cf\u7684\u89d2\u5ea6\u6765\u770b\uff0c<strong>\u4e00\u4e2a\u5de8\u5927\u7684\u6784\u9020\u65b9\u6cd5\u901a\u5e38\u4ee3\u8868\u7740\u51fa\u73b0\u4e86\u4ee3\u7801\u5f02\u5473\uff0c\u8fd9\u4e2a\u7c7b\u53ef\u80fd\u627f\u62c5\u4e86\u8fc7\u591a\u7684\u8d23\u4efb<\/strong>\u3002<\/p>\n<p>\u800c\u5bf9\u4e8e\u57fa\u4e8e setter \u7684\u6ce8\u5165\uff0c\u4ed6\u4eec\u662f\u8fd9\u4e48\u8bf4\u7684\uff1a<\/p>\n<blockquote><p>Setter injection should primarily only be used for optional dependencies that can be assigned reasonable default values within the class. Otherwise, not-null checks must be performed everywhere the code uses the dependency. One benefit of setter injection is that setter methods make objects of that class amenable to reconfiguration or re-injection later.<\/p><\/blockquote>\n<p>\u57fa\u4e8e setter \u7684\u6ce8\u5165\uff0c\u5219\u53ea\u5e94\u8be5\u88ab\u7528\u4e8e\u6ce8\u5165\u975e\u5fc5\u9700\u7684\u4f9d\u8d56\uff0c\u540c\u65f6\u5728\u7c7b\u4e2d\u5e94\u8be5\u5bf9\u8fd9\u4e2a\u4f9d\u8d56\u63d0\u4f9b\u4e00\u4e2a\u5408\u7406\u7684\u9ed8\u8ba4\u503c\u3002\u5982\u679c\u4f7f\u7528 setter \u6ce8\u5165\u5fc5\u9700\u7684\u4f9d\u8d56\uff0c\u90a3\u4e48\u5c06\u4f1a\u6709\u8fc7\u591a\u7684 null \u68c0\u67e5\u5145\u65a5\u5728\u4ee3\u7801\u4e2d\u3002<strong>\u4f7f\u7528 setter \u6ce8\u5165\u7684\u4e00\u4e2a\u4f18\u70b9\u662f\uff0c\u8fd9\u4e2a\u4f9d\u8d56\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u88ab\u6539\u53d8\u6216\u8005\u91cd\u65b0\u6ce8\u5165<\/strong>\u3002<\/p>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a href=\"https:\/\/link.segmentfault.com\/?enc=L3EBdnaQmC8N5l1eqglcag%3D%3D.N8XU0lZICpQU2Bp2EZDVnij6NhD%2B4YGQqaAT9w%2F62qQ%3D\" rel=\"nofollow\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a href=\"https:\/\/link.segmentfault.com\/?enc=D8xk3tFyLlCfEPTlSYQfaw%3D%3D.qEDtppMxlMGg0d5WyAJ%2B5QzPlezGnoUl9e3RW%2FLxyyvdEkUuSubpcGRqT6L1Huj%2BeyWVMozzElZdkmJ8arEf9A%3D%3D\" rel=\"nofollow\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a href=\"https:\/\/link.segmentfault.com\/?enc=l7Cxz3d90iytm8XJ%2Fmw9ag%3D%3D.H177kmb%2BXugwp4smMt4WMJH7Z87xRpXDag%2BUgdw8hkcSx1NrhHodU1DM0N%2FHBV8J\" rel=\"nofollow\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a href=\"https:\/\/link.segmentfault.com\/?enc=J42BROVCOVzOg9FJSDRogA%3D%3D.lk4sdauHCdVR9sLlSeLbij9sp%2Brur%2FXKvFSM83LPkmt0dLj59qpnHt2wHPC9f8TZOOrH5c1m26xx4AfBi4k%2BPA%3D%3D\" rel=\"nofollow\">\u522b\u518d\u5199\u6ee1\u5c4f\u7684\u7206\u7206\u7206\u70b8\u7c7b\u4e86\uff0c\u8bd5\u8bd5\u88c5\u9970\u5668\u6a21\u5f0f\uff0c\u8fd9\u624d\u662f\u4f18\u96c5\u7684\u65b9\u5f0f\uff01\uff01<\/a><\/p>\n<p>5.<a href=\"https:\/\/link.segmentfault.com\/?enc=OY%2BCEZq0nOM2TAedHVMNsg%3D%3D.oRItgdj%2BWiJHm4YhnbI46qyeVoeabp6S2Vs40twiWb0XvDig%2Fju3RrIXJMnPmo5x2%2FvX2arDPfN%2BvcTvu4FoIkLNfIB9q1avblk6U8e%2BmQM%3D\" rel=\"nofollow\">\u300aJava\u5f00\u53d1\u624b\u518c\uff08\u5d69\u5c71\u7248\uff09\u300b\u6700\u65b0\u53d1\u5e03\uff0c\u901f\u901f\u4e0b\u8f7d\uff01<\/a><\/p>\n<p>\u89c9\u5f97\u4e0d\u9519\uff0c\u522b\u5fd8\u4e86\u968f\u624b\u70b9\u8d5e+\u8f6c\u53d1\u54e6\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042451725\">https:\/\/segmentfault.com\/a\/1190000042451725<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1aRichard_Yi\/\u6765\u6e90\uff1ajuejin.cn\/post\/6844904056230690824 \u524d\u8a00  [&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-10006441","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\/10006441","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=10006441"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006441\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}