1.启动类加注解
@EnableAsync
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2.需要异步处理的方法加注解
@Service
public class AysSercive {
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据处理中...");
}
}
controller
@RestController
public class AysController {
@Autowired
private AysSercive aysSercive;
@RequestMapping("/hello")
public String getHeool(){
aysSercive.hello();
return "ok";
}
}