EnableAsync注解的意思是可以异步执行,就是开启多线程的意思。可以标注在方法、类上。

spring boot   @EnableAsync 异步调用
@Component
public class Task {

    @Async
    public void doTaskOne() throws Exception {
        // 同上内容,省略
    }

    @Async
    public void doTaskTwo() throws Exception {
        // 同上内容,省略
    }

    @Async
    public void doTaskThree() throws Exception {
        // 同上内容,省略
    }

}
spring boot   @EnableAsync 异步调用

为了让@Async注解能够生效,还需要在Spring Boot的主程序中配置@EnableAsync,如下所示:

spring boot   @EnableAsync 异步调用
@SpringBootApplication
@EnableAsync
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
spring boot   @EnableAsync 异步调用

注: @Async所修饰的函数不要定义为static类型,这样异步调用不会生效

 

https://www.cnblogs.com/huanghuanghui/p/12932012.html

相关文章:

  • 2021-10-24
  • 2021-07-27
  • 2022-01-07
  • 2021-12-01
  • 2021-12-18
  • 2021-08-25
  • 2021-10-02
猜你喜欢
  • 2021-08-27
  • 2022-02-08
  • 2021-12-04
  • 2021-08-07
  • 2021-07-15
  • 2021-11-18
相关资源
相似解决方案