{"id":216497,"date":"2021-04-28T09:09:56","date_gmt":"2021-04-28T01:09:56","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=216497"},"modified":"2021-04-21T11:10:16","modified_gmt":"2021-04-21T03:10:16","slug":"dependency-injection-methods","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/dependency-injection-methods.html","title":{"rendered":"spring\u5e38\u7528\u7684\u4e09\u79cd\u4f9d\u8d56\u6ce8\u5165\u65b9\u5f0f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5e73\u5e38\u7684java\u5f00\u53d1\u4e2d\uff0c\u7a0b\u5e8f\u5458\u5728\u67d0\u4e2a\u7c7b\u4e2d\u9700\u8981\u4f9d\u8d56\u5176\u5b83\u7c7b\u7684\u65b9\u6cd5\uff0c\u5219\u901a\u5e38\u662fnew\u4e00\u4e2a\u4f9d\u8d56\u7c7b\u518d\u8c03\u7528\u7c7b\u5b9e\u4f8b\u7684\u65b9\u6cd5\uff0c\u8fd9\u79cd\u5f00\u53d1\u5b58\u5728\u7684\u95ee\u9898\u662fnew\u7684\u7c7b\u5b9e\u4f8b\u4e0d\u597d\u7edf\u4e00\u7ba1\u7406\uff0cspring\u63d0\u51fa\u4e86\u4f9d\u8d56\u6ce8\u5165\u7684\u601d\u60f3\uff0c\u5373\u4f9d\u8d56\u7c7b\u4e0d\u7531\u7a0b\u5e8f\u5458\u5b9e\u4f8b\u5316\uff0c\u800c\u662f\u901a\u8fc7spring\u5bb9\u5668\u5e2e\u6211\u4eecnew\u6307\u5b9a\u5b9e\u4f8b\u5e76\u4e14\u5c06\u5b9e\u4f8b\u6ce8\u5165\u5230\u9700\u8981\u8be5\u5bf9\u8c61\u7684\u7c7b\u4e2d\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u4f9d\u8d56\u6ce8\u5165\u7684\u53e6\u4e00\u79cd\u8bf4\u6cd5\u662f\u201c\u63a7\u5236\u53cd\u8f6c\u201d\uff0c\u901a\u4fd7\u7684\u7406\u89e3\u662f\uff1a\u5e73\u5e38\u6211\u4eecnew\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u8fd9\u4e2a\u5b9e\u4f8b\u7684\u63a7\u5236\u6743\u662f\u6211\u4eec\u7a0b\u5e8f\u5458\uff0c\u800c\u63a7\u5236\u53cd\u8f6c\u662f\u6307new\u5b9e\u4f8b\u5de5\u4f5c\u4e0d\u7531\u6211\u4eec\u7a0b\u5e8f\u5458\u6765\u505a\u800c\u662f\u4ea4\u7ed9spring\u5bb9\u5668\u6765\u505a\u3002Spring\u901a\u8fc7DI\uff08\u4f9d\u8d56\u6ce8\u5165\uff09\u5b9e\u73b0IOC\uff08\u63a7\u5236\u53cd\u8f6c\uff09\uff0c\u5e38\u7528\u7684\u6ce8\u5165\u65b9\u5f0f\u4e3b\u8981\u6709\u4e09\u79cd\uff1a\u6784\u9020\u65b9\u6cd5\u6ce8\u5165\uff0cset\u65b9\u6cd5\u53c2\u6570\u6ce8\u5165\uff0c\u63a5\u53e3\u6ce8\u5165\u3002<\/p>\n

\u4e00.\u76ee\u524d\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684 @Autowired\uff1a\u81ea\u52a8\u88c5\u914d<\/strong><\/div>\n

\u57fa\u4e8e@Autowired\u7684\u81ea\u52a8\u88c5\u914d\uff0c\u9ed8\u8ba4\u662f\u6839\u636e\u7c7b\u578b\u6ce8\u5165\uff0c\u53ef\u4ee5\u7528\u4e8e\u6784\u9020\u5668\u3001\u63a5\u53e3\u3001\u65b9\u6cd5\u6ce8\u5165\uff0c\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n

\r\n@Autowired\r\n\u6784\u9020\u65b9\u6cd5\u3001\u65b9\u6cd5\u3001\u63a5\u53e3\r\n<\/pre>\n

@Autowired\u9ed8\u8ba4\u662f\u6839\u636e\u53c2\u6570\u7c7b\u578b\u8fdb\u884c\u81ea\u52a8\u88c5\u914d\uff0c\u4e14\u5fc5\u987b\u6709\u4e00\u4e2aBean\u5019\u9009\u8005\u6ce8\u5165\u9ed8\u8ba4required=true\uff0c\u5982\u679c\u5141\u8bb8\u51fa\u73b00\u4e2aBean\u5019\u9009\u8005\u9700\u8981\u8bbe\u7f6e\u5c5e\u6027\u201crequired=false\u201d\uff0c\u201crequired\u201d\u5c5e\u6027\u542b\u4e49\u548c@Required\u4e00\u6837\uff0c\u53ea\u662f@Required\u53ea\u9002\u7528\u4e8e\u57fa\u4e8eXML\u914d\u7f6e\u7684setter\u6ce8\u5165\u65b9\u5f0f,\u53ea\u80fd\u6253\u5728setting\u65b9\u6cd5\u4e0a\u3002<\/p>\n

\u6784\u9020\u5668\u6ce8\u5165<\/strong><\/span><\/div>\n

\u901a\u8fc7\u5c06@Autowired\u6ce8\u89e3\u653e\u5728\u6784\u9020\u5668\u4e0a\u6765\u5b8c\u6210\u6784\u9020\u5668\u6ce8\u5165\uff0c\u9ed8\u8ba4\u6784\u9020\u5668\u53c2\u6570\u901a\u8fc7\u7c7b\u578b\u81ea\u52a8\u88c5\u914d\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

\r\n public class Test1 {  \r\n    private MessageInterface message;  \r\n    @Autowired \/\/\u6784\u9020\u5668\u6ce8\u5165  \r\n    private Test1(MessageInterface message) {  \r\n        this.message = message;  \r\n    } \r\n    \/\/\u7701\u7565getter\u548csetter  \r\n  }  \r\n<\/pre>\n
\u63a5\u53e3\u6ce8\u5165<\/strong><\/span><\/div>\n

\u901a\u8fc7\u5c06@Autowired\u6ce8\u89e3\u653e\u5728\u6784\u9020\u5668\u4e0a\u6765\u5b8c\u6210\u63a5\u53e3\u6ce8\u5165\u3002<\/p>\n

\r\n public class Test2 {  \r\n    @Autowired \/\/\u63a5\u53e3\u6ce8\u5165  \r\n    private MessageInterface messageInterface;  \r\n    \/\/\u7701\u7565getter\u548csetter  \r\n} \r\n<\/pre>\n
\u65b9\u6cd5\u53c2\u6570\u6ce8\u5165<\/strong><\/span><\/div>\n

\u901a\u8fc7\u5c06@Autowired\u6ce8\u89e3\u653e\u5728\u65b9\u6cd5\u4e0a\u6765\u5b8c\u6210\u65b9\u6cd5\u53c2\u6570\u6ce8\u5165\u3002<\/p>\n

\r\n public class Test3 {  \r\n    private MessageInterface message;  \r\n \r\n    @Autowired \/\/setter\u65b9\u6cd5\u6ce8\u5165  \r\n    public void setMessage(MessageInterface message) {  \r\n        this.message = message;  \r\n    }  \r\n    public String getMessage() {  \r\n        return message;  \r\n    }  \r\n}\r\n public class Test4 {  \r\n    private MessageInterface message;  \/\/\r\n    private List list;  \r\n    @Autowired(required = true) \/\/\u4efb\u610f\u4e00\u4e2a\u6216\u591a\u4e2a\u53c2\u6570\u65b9\u6cd5\u6ce8\u5165  \r\n    private void initMessage(MessageInterface message, ArrayList<\/string> list) {  \r\n        this.message = message;  \r\n        this.list = list;  \r\n    }  \r\n    \/\/\u7701\u7565getter\u548csetter  \r\n} \r\n<\/string>\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u4f9d\u8d56\u6ce8\u5165\u7684\u53e6\u4e00\u79cd\u8bf4\u6cd5\u662f\u201c\u63a7\u5236\u53cd\u8f6c\u201d\uff0c\u901a\u4fd7\u7684\u7406\u89e3\u662f\uff1a\u5e73\u5e38\u6211\u4eecnew\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u8fd9\u4e2a\u5b9e\u4f8b\u7684\u63a7\u5236\u6743\u662f\u6211\u4eec\u7a0b\u5e8f\u5458\uff0c\u800c\u63a7\u5236 […]<\/p>\n","protected":false},"author":1903,"featured_media":163508,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-216497","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/216497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=216497"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/216497\/revisions"}],"predecessor-version":[{"id":216500,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/216497\/revisions\/216500"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/163508"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=216497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=216497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=216497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}