spring boot 开启异步调用
1、启动类上添加@EnableAsync注解,表示启动异步
2、在具体实现异步的方法上添加@Async注解
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.example.demo;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
* @Description: TODO
* @author: sl
* @date: 2024年05月30日 21:55
*/
@Component
public class DemoController {
/**
* Async相当于是方法级别的线程,本身没有自定义线程池更加灵活
* 相当于是每进来一个请求就开启一个线程,超过核心线程数小于最大线程数放入队列&