异步任务
AsyncController
@RestController
public class AsyncController {
@Autowired
AsyncService service;
@GetMapping("/hello")
public String hello(){
service.hello();
return "OK";
}
}
AsyncService
package com.yuan.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("操作成功");
}
}
DemoApplication启动类
package com.yuan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@EnableAsync
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}