{"id":35153,"date":"2024-11-26T11:08:15","date_gmt":"2024-11-26T03:08:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35153\/"},"modified":"2024-11-26T11:08:15","modified_gmt":"2024-11-26T03:08:15","slug":"%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3-java-%e7%ba%bf%e7%a8%8b%e7%b1%bb%e6%97%a0%e6%b3%95%e4%bd%bf%e7%94%a8-autowired-%e6%b3%a8%e8%a7%a3%e6%b3%a8%e5%85%a5%e4%be%9d%e8%b5%96%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35153\/","title":{"rendered":"\u5982\u4f55\u89e3\u51b3 Java \u7ebf\u7a0b\u7c7b\u65e0\u6cd5\u4f7f\u7528 @Autowired \u6ce8\u89e3\u6ce8\u5165\u4f9d\u8d56\u7684\u95ee\u9898\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173062585650339.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u89e3\u51b3 Java \u7ebf\u7a0b\u7c7b\u65e0\u6cd5\u4f7f\u7528 @Autowired \u6ce8\u89e3\u6ce8\u5165\u4f9d\u8d56\u7684\u95ee\u9898\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u89e3\u51b3 Java \u7ebf\u7a0b\u7c7b\u65e0\u6cd5\u4f7f\u7528 @Autowired \u6ce8\u89e3\u6ce8\u5165\u4f9d\u8d56\u7684\u95ee\u9898\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>java \u7ebf\u7a0b\u7c7b\u65e0\u6cd5\u6ce8\u5165<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3 java \u7ebf\u7a0b\u7c7b\u65e0\u6cd5\u4f7f\u7528 @autowired \u6ce8\u89e3\u6ce8\u5165\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u6784\u9020\u5668\u6ce8\u5165\u7684\u65b9\u6cd5\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p><strong>1. \u5b9a\u4e49\u7ebf\u7a0b\u7c7b<\/strong><\/p>\n<p>\u5c06\u9700\u8981\u6ce8\u5165\u7684\u4f9d\u8d56\u9879\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u7684\u53c2\u6570\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>public class mythread implements runnable {\n\n    private redisclient redisclient;\n    private myservice myservice;\n\n    public mythread(redisclient redisclient, myservice myservice) {\n        this.redisclient = redisclient;\n        this.myservice = myservice;\n    }\n\n    @override\n    public void run() {\n        \/\/ \u4f7f\u7528\u5df2\u6ce8\u5165\u7684\u4f9d\u8d56\u9879...\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u8c03\u7528\u7ebf\u7a0b\u7c7b<\/strong><\/p>\n<p>\u5728\u8c03\u7528\u7ebf\u7a0b\u7c7b\u65f6\uff0c\u901a\u8fc7\u6784\u9020\u51fd\u6570\u4f20\u53c2\u8fdb\u884c\u6ce8\u5165\uff1a<\/p>\n<pre>mythread thread = new mythread(redisclient, myservice);\nthread.start();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u6ce8\u5165\u914d\u7f6e<\/strong><\/p>\n<p>\u5728 spring \u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528 \u6807\u7b7e\u58f0\u660e\u7ebf\u7a0b\u7c7b\u5e76\u8fdb\u884c\u6ce8\u5165\uff1a <\/p>\n<pre>&lt;bean id=\"myThread\" class=\"MyThread\"&gt;\n    &lt;constructor-arg ref=\"redisClient\" \/&gt;\n    &lt;constructor-arg ref=\"myService\" \/&gt;\n&lt;\/bean&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u6784\u9020\u5668\u6ce8\u5165\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6210\u529f\u5c06\u9700\u8981\u7684\u4f9d\u8d56\u9879\u6ce8\u5165\u5230 java \u7ebf\u7a0b\u7c7b\u4e2d\uff0c\u89e3\u51b3\u539f\u5148\u7684\u95ee\u9898\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u89e3\u51b3 Java \u7ebf\u7a0b\u7c7b\u65e0\u6cd5\u4f7f\u7528 @Autowired \u6ce8\u89e3\u6ce8\u5165\u4f9d\u8d56\u7684\u95ee\u9898\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java \u7ebf\u7a0b\u7c7b\u65e0\u6cd5\u6ce8\u5165 \u4e3a\u4e86\u89e3\u51b3 java \u7ebf\u7a0b\u7c7b\u65e0\u6cd5\u4f7f\u7528 @autowired \u6ce8\u89e3\u6ce8\u5165\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u6784\u9020\u5668\u6ce8\u5165\u7684\u65b9\u6cd5\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a 1. \u5b9a\u4e49\u7ebf\u7a0b\u7c7b \u5c06\u9700\u8981\u6ce8\u5165\u7684\u4f9d\u8d56\u9879\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u7684\u53c2\u6570\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b public class mythread implements runnable { private redisclient redisclient; private myservice myservice; public mythread(redisclient redisclient, myservice myservice) { this.redisclient = redisclient; this.myservice = myservice; } @override public void run() { \/\/ \u4f7f\u7528\u5df2\u6ce8\u5165\u7684\u4f9d\u8d56\u9879&#8230; } } \u767b\u5f55\u540e\u590d\u5236 2. \u8c03\u7528\u7ebf\u7a0b\u7c7b \u5728\u8c03\u7528\u7ebf\u7a0b\u7c7b\u65f6\uff0c\u901a\u8fc7\u6784\u9020\u51fd\u6570\u4f20\u53c2\u8fdb\u884c\u6ce8\u5165\uff1a mythread thread = new mythread(redisclient, myservice); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-35153","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=35153"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35153\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}