Controller层
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello() {
asyncService.hello();
return "ok!";
}
}
实现层
//告诉Spring这是一个异步的方法
@Async
public void hello() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据正在处理....");
}
SpringBoot主入口
@EnableAsync //开启异步注解
@SpringBootApplication
public class SpringbootTaskDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTaskDemoApplication.class, args);
}
}
tips:
@EnableAsync //标记在入口函数的类上面,表示开启异步注解
@Async //声明这是一个异步的方法