{"id":8494,"date":"2023-11-28T20:29:44","date_gmt":"2023-11-28T12:29:44","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=8494"},"modified":"2023-12-06T10:37:18","modified_gmt":"2023-12-06T02:37:18","slug":"springboot-zhongasync-yi-bu-diao-yong-fang-fa","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-zhongasync-yi-bu-diao-yong-fang-fa\/","title":{"rendered":"SpringBoot\u4e2dAsync\u5f02\u6b65\u8c03\u7528\u65b9\u6cd5\uff1f"},"content":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>@Async<\/code> \u6ce8\u89e3\u6765\u5b9e\u73b0\u5f02\u6b65\u8c03\u7528\u65b9\u6cd5\u3002\u5f02\u6b65\u65b9\u6cd5\u7684\u6267\u884c\u5c06\u5728\u72ec\u7acb\u7684\u7ebf\u7a0b\u4e2d\u8fdb\u884c\uff0c\u800c\u4e0d\u4f1a\u963b\u585e\u8c03\u7528\u8005\u7684\u7ebf\u7a0b\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 <code>@Async<\/code> \u6ce8\u89e3\u7684\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\n<strong>\u5728\u4e3b\u7c7b\u4e0a\u6dfb\u52a0 <code>@EnableAsync<\/code> \u6ce8\u89e3\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u4f60\u7684\u4e3b\u7c7b\uff08\u5305\u542b <code>main<\/code> \u65b9\u6cd5\u7684\u7c7b\uff09\u4e0a\u6dfb\u52a0 <code>@EnableAsync<\/code> \u6ce8\u89e3\uff0c\u4ee5\u542f\u7528\u5f02\u6b65\u65b9\u6cd5\u7684\u652f\u6301\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.scheduling.annotation.EnableAsync;\n\n@SpringBootApplication\n@EnableAsync\npublic class MyApplication {\n\n    public static void main(String[] args) {\n        SpringApplication.run(MyApplication.class, args);\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u5728\u5f02\u6b65\u65b9\u6cd5\u4e0a\u6dfb\u52a0 <code>@Async<\/code> \u6ce8\u89e3\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u5e0c\u671b\u5f02\u6b65\u6267\u884c\u7684\u65b9\u6cd5\u4e0a\u6dfb\u52a0 <code>@Async<\/code> \u6ce8\u89e3\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.scheduling.annotation.Async;\nimport org.springframework.stereotype.Service;\n\n@Service\npublic class MyService {\n\n    @Async\n    public void asyncMethod() {\n        \/\/ \u5f02\u6b65\u6267\u884c\u7684\u903b\u8f91\n    }\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u7684 <code>asyncMethod<\/code> \u5c31\u662f\u4e00\u4e2a\u5f02\u6b65\u65b9\u6cd5\u3002\n<\/li>\n<li>\n<strong>\u8c03\u7528\u5f02\u6b65\u65b9\u6cd5\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u5176\u4ed6\u7ec4\u4ef6\u4e2d\u8c03\u7528\u5f02\u6b65\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Controller;\nimport org.springframework.web.bind.annotation.GetMapping;\n\n@Controller\npublic class MyController {\n\n    @Autowired\n    private MyService myService;\n\n    @GetMapping(&quot;\/invokeAsyncMethod&quot;)\n    public String invokeAsyncMethod() {\n        myService.asyncMethod();\n        return &quot;Async method invoked.&quot;;\n    }\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>invokeAsyncMethod<\/code> \u65b9\u6cd5\u8c03\u7528\u4e86 <code>MyService<\/code> \u4e2d\u7684 <code>asyncMethod<\/code>\uff0c\u800c <code>asyncMethod<\/code> \u5c06\u5728\u5f02\u6b65\u7684\u7ebf\u7a0b\u4e2d\u6267\u884c\u3002\n<\/li>\n<\/ol>\n<p>\u8bf7\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>\u5f02\u6b65\u65b9\u6cd5\u901a\u5e38\u5e94\u8be5\u8fd4\u56de <code>void<\/code>\uff0c\u56e0\u4e3a\u8c03\u7528\u8005\u65e0\u6cd5\u7b49\u5f85\u5f02\u6b65\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u3002<\/li>\n<li>\u5f02\u6b65\u65b9\u6cd5\u901a\u5e38\u9700\u8981\u5728\u542f\u52a8\u7c7b\u4e0a\u4f7f\u7528 <code>@EnableAsync<\/code> \u542f\u7528\u5f02\u6b65\u652f\u6301\u3002<\/li>\n<li>\u5f02\u6b65\u65b9\u6cd5\u901a\u5e38\u9700\u8981\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u914d\u7f6e\u4e2d\u914d\u7f6e\u4e00\u4e2a <code>TaskExecutor<\/code> bean\uff0c\u4ee5\u6307\u5b9a\u5f02\u6b65\u65b9\u6cd5\u5728\u54ea\u4e2a\u7ebf\u7a0b\u6c60\u4e2d\u6267\u884c\u3002\u5982\u679c\u6ca1\u6709\u914d\u7f6e\uff0c\u9ed8\u8ba4\u5c06\u4f7f\u7528\u4e00\u4e2a\u5408\u9002\u7684\u7ebf\u7a0b\u6c60\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.core.task.SimpleAsyncTaskExecutor;\nimport org.springframework.core.task.TaskExecutor;\n\n@Configuration\npublic class AsyncConfig {\n\n    @Bean\n    public TaskExecutor taskExecutor() {\n        return new SimpleAsyncTaskExecutor();\n    }\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>taskExecutor<\/code> \u65b9\u6cd5\u914d\u7f6e\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u7ebf\u7a0b\u6c60\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u914d\u7f6e\u66f4\u590d\u6742\u7684\u7ebf\u7a0b\u6c60\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 @Async \u6ce8\u89e3\u6765\u5b9e\u73b0\u5f02\u6b65\u8c03\u7528\u65b9\u6cd5\u3002\u5f02\u6b65\u65b9\u6cd5\u7684\u6267\u884c\u5c06\u5728\u72ec\u7acb\u7684\u7ebf\u7a0b [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"class_list":["post-8494","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=8494"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8494\/revisions"}],"predecessor-version":[{"id":16622,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8494\/revisions\/16622"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=8494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=8494"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=8494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}