Springboot中的异步任务

建立AsyncService

@Service
public class AsyncService {
    //怎么能告诉Spring这是个异步方法
    @Async
    public void Hello(){
        try {
            Thread.sleep(3000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("异步任务");
    }
}

在用Controller调用Service

@Controller
public class HelloController {
    @Autowired
    AsyncService asyncService;
    @ResponseBody
    @RequestMapping("/Hello")
    public String Hello(){
        asyncService.Hello();
        return "OK";
    }
}

你可能感兴趣的:(java)