{"id":6182,"date":"2022-07-06T17:38:57","date_gmt":"2022-07-06T09:38:57","guid":{"rendered":"http:\/\/123.57.164.21\/?p=6182"},"modified":"2022-07-06T17:38:57","modified_gmt":"2022-07-06T09:38:57","slug":"spring%e4%b8%adautowired%e5%92%8cresource%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=6182","title":{"rendered":"Spring\u4e2d@Autowired\u548c@Resource\u7684\u533a\u522b"},"content":{"rendered":"\n<p><strong>\u4e00\u3001\u5b9a\u4e49<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>@Autowired<\/li><\/ul>\n\n\n\n<p>\u5bf9\u7c7b\u6210\u5458\u53d8\u91cf\u3001\u65b9\u6cd5\u53ca\u6784\u9020\u51fd\u6570\u8fdb\u884c\u6807\u6ce8\uff0c\u5b8c\u6210\u81ea\u52a8\u88c5\u914d\u7684\u5de5\u4f5c\u3002<\/p>\n\n\n\n<ul><li>@Resource<\/li><\/ul>\n\n\n\n<p>\u5728\u8bed\u4e49\u4e0a\u88ab\u5b9a\u4e49\u4e3a\u901a\u8fc7\u5176\u552f\u4e00\u7684\u540d\u79f0\u6765\u6807\u8bc6\u7279\u5b9a\u7684\u76ee\u6807\u7ec4\u4ef6\uff0c\u5176\u4e2d\u58f0\u660e\u7684\u7c7b\u578b\u4e0e\u5339\u914d\u8fc7\u7a0b\u65e0\u5173\u3002<\/p>\n\n\n\n<p><strong>\u5982\u679c\u6ca1\u6709\u660e\u786e\u6307\u5b9a\u540d\u79f0\uff0c\u5219\u9ed8\u8ba4\u540d\u79f0\u662f\u4ece\u5b57\u6bb5\u540d\u79f0\u6216\u8bbe\u7f6e\u65b9\u6cd5\uff08get\u3001set\u65b9\u6cd5\uff09\u6d3e\u751f\u7684\u3002 \u5982\u679c\u7528\u5728\u5b57\u6bb5\u4e0a\uff0c\u5219\u91c7\u7528\u5b57\u6bb5\u540d\u79f0; \u5982\u679c\u7528\u5728\u5728setter\u65b9\u6cd5\uff0c\u5b83\u91c7\u7528\u5176\u5c5e\u6027\u540d\u79f0\uff08\u4f8b\u5982setProperty()\u65b9\u6cd5\uff0c\u53d6property\u505a\u4e3a\u5c5e\u6027\u540d\u79f0)\u3002<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>\u4e8c\u3001\u533a\u522b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5728Spring\u6846\u67b6\u4e2d\uff0c\u5982\u679c\u5728Service\u5c42\u4e2d\u9700\u8981\u6ce8\u5165\u5176\u4ed6\u4f9d\u8d56\u7684\u5bf9\u8c61\uff0c\u901a\u5e38\u6211\u4eec\u90fd\u4f1a\u4f7f\u7528@Autowired\u6216\u8005@Resource\u6ce8\u89e3\uff0c\u4f46\u662f\u5b83\u4eec\u662f\u6709\u533a\u522b\u7684\uff0c\u6bd4\u5982@Autowired\u8ddfSpring\u6846\u67b6\u5f3a\u8026\u5408\u4e86\uff0c \u5982\u679c\u6362\u6210\u5176\u4ed6\u6846\u67b6\uff0c@Autowired\u5c31\u6ca1\u4f5c\u7528\u4e86\u3002\u800c@Resource\u662fJSR-250\u63d0\u4f9b\u7684\uff0c\u5b83\u662fJava\u6807\u51c6\uff0c\u7edd\u5927\u90e8\u5206\u6846\u67b6\u90fd\u652f\u6301\u3002<strong>@Autowired<\/strong>\u662fspring\u63d0\u4f9b\u7684\u6ce8\u91ca,<strong>@Resource<\/strong>\u662fJAVA2EE\u63d0\u4f9b\u7684,  @Resource\u6ce8\u89e3\u5728\u5b57\u6bb5\u4e0a\uff0c\u8fd9\u6837\u5c31\u4e0d\u7528\u5199setter\u65b9\u6cd5\u4e86\uff0c\u5e76\u4e14\u8fd9\u4e2a\u6ce8\u89e3\u662f\u5c5e\u4e8eJ2EE\u7684\uff0c\u51cf\u5c11\u4e86\u4e0espring\u7684\u8026\u5408\u3002\u8fd9\u6837\u4ee3\u7801\u770b\u8d77\u5c31\u6bd4\u8f83\u4f18\u96c5<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u603b\u7ed3\u4e00\u4e0b@Autowired\u548c@Resource\u7684\u533a\u522b\uff1a<\/p>\n\n\n\n<ul><li><strong>\u5305\u542b\u7684\u5c5e\u6027\u4e0d\u540c<\/strong><\/li><\/ul>\n\n\n\n<p>@Autowired\u53ea\u5305\u542b\u4e00\u4e2a\u53c2\u6570\uff1arequired\uff0c\u8868\u793a\u662f\u5426\u5f00\u542f\u81ea\u52a8\u6ce8\u5165\uff0c\u9ed8\u8ba4\u662ftrue\u3002\u800c@Resource\u5305\u542b\u4e03\u4e2a\u53c2\u6570\uff0c\u5176\u4e2d\u6700\u91cd\u8981\u7684\u4e24\u4e2a\u53c2\u6570\u662f\uff1aname \u548c type\u3002\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public @interface Autowired {\n \n    \/**\n     * \u662f\u5426\u5f00\u542f\u81ea\u52a8\u6ce8\u5165,\u6709\u4e9b\u65f6\u5019\u6211\u4eec\u4e0d\u60f3\u4f7f\u7528\u81ea\u52a8\u88c5\u914d\u529f\u80fd\uff0c\u53ef\u4ee5\u5c06\u8be5\u53c2\u6570\u8bbe\u7f6e\u6210false\u3002\n     *\/\n\tboolean required() default true;\n \n}\n \n \npublic @interface Resource {\n    \/**\n     * bean\u7684\u540d\u79f0\n     *\/\n    String name() default \"\";\n \n    String lookup() default \"\";\n \n    \/**\n     * Java\u7c7b,\u88ab\u89e3\u6790\u4e3abean\u7684\u7c7b\u578b\n     *\/\n    Class&lt;?> type() default java.lang.Object.class;\n \n    enum AuthenticationType {\n            CONTAINER,\n            APPLICATION\n    }\n \n    \/**\n     * \u8eab\u4efd\u9a8c\u8bc1\u7c7b\u578b\n     *\/\n    AuthenticationType authenticationType() default AuthenticationType.CONTAINER;\n \n    \/**\n     * \u7ec4\u4ef6\u662f\u5426\u53ef\u4ee5\u4e0e\u5176\u4ed6\u7ec4\u4ef6\u4e4b\u95f4\u5171\u4eab\n     *\/\n    boolean shareable() default true;\n \n    String mappedName() default \"\";\n \n    \/**\n     * \u63cf\u8ff0\n     *\/\n    String description() default \"\";\n}\n<\/pre>\n\n\n\n<ul><li><strong>@Autowired\u9ed8\u8ba4\u6309byType\u81ea\u52a8\u88c5\u914d\uff0c\u800c@Resource\u9ed8\u8ba4byName\u81ea\u52a8\u88c5\u914d\u3002<\/strong><\/li><\/ul>\n\n\n\n<p>@Autowired\u5982\u679c\u8981\u4f7f\u7528byName\uff0c\u9700\u8981\u4f7f\u7528@Qualifier\u4e00\u8d77\u914d\u5408\u3002\u800c@Resource\u5982\u679c\u6307\u5b9a\u4e86name\uff0c\u5219\u7528byName\u81ea\u52a8\u88c5\u914d\uff0c\u5982\u679c\u6307\u5b9a\u4e86type\uff0c\u5219\u7528byType\u81ea\u52a8\u88c5\u914d\u3002<\/p>\n\n\n\n<ul><li><strong>\u6ce8\u89e3\u5e94\u7528\u7684\u5730\u65b9\u4e0d\u540c<\/strong><\/li><\/ul>\n\n\n\n<p>@Autowired\u80fd\u591f\u7528\u5728\uff1a\u6784\u9020\u5668\u3001\u65b9\u6cd5\u3001\u53c2\u6570\u3001\u6210\u5458\u53d8\u91cf\u548c\u6ce8\u89e3\u4e0a\uff0c\u800c@Resource\u80fd\u7528\u5728\uff1a\u7c7b\u3001\u6210\u5458\u53d8\u91cf\u548c\u65b9\u6cd5\u4e0a\u3002<\/p>\n\n\n\n<ul><li><strong>\u51fa\u5904\u4e0d\u540c<\/strong><\/li><\/ul>\n\n\n\n<p>@Autowired\u662fSpring\u5b9a\u4e49\u7684\u6ce8\u89e3\uff0c\u800c@Resource\u662fJSR-250\u5b9a\u4e49\u7684\u6ce8\u89e3\u3002\u6240\u4ee5@Autowired\u53ea\u80fd\u5728Spring\u6846\u67b6\u4e0b\u4f7f\u7528\uff0c\u800c@Resource\u5219\u53ef\u4ee5\u4e0e\u5176\u4ed6\u6846\u67b6\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n\n\n\n<ul><li><strong>\u88c5\u914d\u987a\u5e8f\u4e0d\u540c<\/strong><\/li><\/ul>\n\n\n\n<p>@Autowired\u7684\u88c5\u914d\u987a\u5e8f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>@Autowired\u9ed8\u8ba4\u5148\u6309byType\u8fdb\u884c\u5339\u914d\uff0c\u5982\u679c\u53d1\u73b0\u627e\u5230\u591a\u4e2abean\uff0c\u5219\u53c8\u6309\u7167byName\u65b9\u5f0f\u8fdb\u884c\u5339\u914d\uff0c\u5982\u679c\u8fd8\u6709\u591a\u4e2a\uff0c\u5219\u62a5\u51fa\u5f02\u5e38\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-14-1024x433.png\" alt=\"\" class=\"wp-image-6183\" width=\"628\" height=\"266\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-14-1024x433.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-14-300x127.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-14-768x325.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-14-830x351.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-14-230x97.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-14-350x148.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-14-480x203.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-14.png 1418w\" sizes=\"(max-width: 628px) 100vw, 628px\" \/><\/figure><\/div>\n\n\n\n<p>@Resource\u7684\u88c5\u914d\u987a\u5e8f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>\u5982\u679c\u540c\u65f6\u6307\u5b9a\u4e86name\u548ctype\uff0c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-15-1024x309.png\" alt=\"\" class=\"wp-image-6184\" width=\"642\" height=\"193\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-15-1024x309.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-15-300x90.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-15-768x231.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-15-830x250.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-15-230x69.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-15-350x106.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-15-480x145.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-15.png 1400w\" sizes=\"(max-width: 642px) 100vw, 642px\" \/><\/figure><\/div>\n\n\n\n<p>\u5982\u679c\u6307\u5b9a\u4e86name\uff0c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>\u53ea\u662f\u6307\u5b9a\u4e86@Resource\u6ce8\u89e3\u7684name\uff0c\u5219\u6309name\u540e\u7684\u540d\u5b57\u53bbbean\u5143\u7d20\u91cc\u67e5\u627e\u6709\u4e0e\u4e4b\u76f8\u7b49\u7684name\u5c5e\u6027\u7684bean\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-16-1024x254.png\" alt=\"\" class=\"wp-image-6185\" width=\"628\" height=\"156\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-16-1024x254.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-16-300x74.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-16-768x191.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-16-830x206.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-16-230x57.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-16-350x87.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-16-480x119.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-16.png 1402w\" sizes=\"(max-width: 628px) 100vw, 628px\" \/><\/figure><\/div>\n\n\n\n<p>\u5982\u679c\u6307\u5b9a\u4e86type\uff0c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>\u53ea\u6307\u5b9a@Resource\u6ce8\u89e3\u7684type\u5c5e\u6027\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\u8005\u627e\u5230\u591a\u4e2a\uff0c\u90fd\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-17-1024x234.png\" alt=\"\" class=\"wp-image-6186\" width=\"631\" height=\"143\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-17-1024x234.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-17-300x68.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-17-768x175.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-17-830x189.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-17-230x52.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-17-350x80.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-17-480x110.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-17.png 1420w\" sizes=\"(max-width: 631px) 100vw, 631px\" \/><\/figure><\/div>\n\n\n\n<p>\u5982\u679c\u65e2\u6ca1\u6709\u6307\u5b9aname\uff0c\u4e5f\u6ca1\u6709\u6307\u5b9atype\uff0c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>\u65e2\u4e0d\u6307\u5b9aname\u5c5e\u6027\uff0c\u4e5f\u4e0d\u6307\u5b9atype\u5c5e\u6027\uff0c\u5219\u81ea\u52a8\u6309byName\u65b9\u5f0f\u8fdb\u884c\u67e5\u627e\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230\u7b26\u5408\u7684bean\uff0c\u5219\u56de\u9000\u4e3a\u4e00\u4e2a\u539f\u59cb\u7c7b\u578b\u8fdb\u884c\u8fdb\u884c\u67e5\u627e\uff0c\u5982\u679c\u627e\u5230\u5c31\u6ce8\u5165\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-18.png\" alt=\"\" class=\"wp-image-6187\" width=\"299\" height=\"385\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-18.png 574w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-18-233x300.png 233w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-18-230x297.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-18-350x451.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-18-480x619.png 480w\" sizes=\"(max-width: 299px) 100vw, 299px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5b9a\u4e49 @Autowired \u5bf9\u7c7b\u6210\u5458\u53d8\u91cf\u3001\u65b9\u6cd5\u53ca\u6784\u9020\u51fd\u6570\u8fdb\u884c\u6807\u6ce8\uff0c\u5b8c\u6210\u81ea\u52a8\u88c5\u914d\u7684\u5de5\u4f5c\u3002 @Resource [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6182"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6182"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6182\/revisions"}],"predecessor-version":[{"id":6188,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6182\/revisions\/6188"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}