springboot 异步任务

main 方法上 加这个注解
@EnableAsync

asyncservice
@Service
public class AsyncService {

    //告诉spring这是个异步任务@Async
    @Async
    public void hello(){

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("数据正在处理请稍后");
    }
}

  controller

@RestController
public class AsyncController {
    @Autowired
    AsyncService asyncService;
    @RequestMapping("/hello")
    public String hello(){
        asyncService.hello();
        return "hello";
    }
}

  


posted @ 2021-06-02 14:39  川上富江  阅读(45)  评论(0)    收藏  举报