服务方法如何定义支持异步执行

提问 1 447
邮箱用户_su9rp
邮箱用户_su9rp LV5 2021年9月7日 08:57 发表
点击群号免费加入社区交流群:367346704
<p>服务方法如何定义支持异步执行</p>
收藏(0)  分享
相关标签: 笔记 讨论
1个回复
  • 站长
    2021年9月7日 14:57
    使用 @EnableAsync 和 @Async 注解。 在启动类或者配置类加上 @EnableAsync 注解: [pre] import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; @EnableAsync @SpringBootApplication public class AsyncApplication { public static void main(String[] args) { SpringApplication.run(AsyncApplication.class, args); } } [/pre] 给方法加上 @Async 注解: [pre] import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @Service public class AsyncServiceImpl implements AsyncService { @Async @Override public void asyncMethod(String arg) { System.out.println("arg:" + arg); System.out.println("=====" + Thread.currentThread().getName() + "========="); } } [/pre]
    1 0