SpringBoot异步任务

1、代码

重点是开启 @EnableAsync在service的方法标注@Async

@EnableAsync
@SpringBootApplication
public class SpringbootTask {

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

}
@Controller
public class AsyncController {

    @Autowired
    AsyncService asyncService;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        asyncService.hello();
        return "ok";
    }
}
@Service
public class AsyncService {
    @Async
    public void hello() {
        try {
            Thread.sleep(1000 * 3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("数据正在处理");
    }
}
posted @ 2021-08-09 20:28  一只桔子2233  阅读(56)  评论(0编辑  收藏  举报