{"id":203360,"date":"2025-05-22T08:56:26","date_gmt":"2025-05-22T00:56:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203360\/"},"modified":"2025-05-22T08:56:26","modified_gmt":"2025-05-22T00:56:26","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:\/\/server.hk\/cnblog\/203360\/","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><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u89e3\u51b3 Java \u7ebf\u7a0b\u7c7b\u65e0\u6cd5\u4f7f\u7528 @Autowired \u6ce8\u89e3\u6ce8\u5165\u4f9d\u8d56\u7684\u95ee\u9898\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300a\u5982\u4f55\u89e3\u51b3 Java \u7ebf\u7a0b\u7c7b\u65e0\u6cd5\u4f7f\u7528 @Autowired \u6ce8\u89e3\u6ce8\u5165\u4f9d\u8d56\u7684\u95ee\u9898\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241106\/1730867806672af25e6bd99.jpg\" class=\"aligncenter\"><\/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<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><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><strong>3. \u6ce8\u5165\u914d\u7f6e<\/strong><\/p>\n<p>\u5728 spring \u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528 &lt;bean&gt; \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>\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>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u89e3\u51b3 Java \u7ebf\u7a0b\u7c7b\u65e0\u6cd5\u4f7f\u7528 @Autowired \u6ce8\u89e3\u6ce8\u5165\u4f9d\u8d56\u7684\u95ee\u9898\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u89e3\u51b3 Java \u7ebf\u7a0b\u7c7b\u65e0\u6cd5\u4f7f&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203360","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203360"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203360\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}