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";
}
}

浙公网安备 33010602011771号