service
public interface AsyncService {
public void sandMessage();
}
service实现类
@Service
public class AsyncServiceImpl implements AsyncService {
@Override
@Async //表示这个方法可以开启异步任务
public void sandMessage() {
System.out.println("正在发送短信");
}
}
controller层
@RestController
@EnableAsync //这个注解表示所有标注过@Async的方法全部异步进行
public class TestPoolTask {
@Autowired
private AsyncService asyncService;
@GetMapping("/pool")
public String demo(){
//异步执行
System.out.println("异步执行发短信");
asyncService.sandMessage();
System.out.println("end");
return "ok";
}
}