异步任务
启动类
@MapperScan("com.topcheer.*.*.dao") @SpringBootApplication @EnableCaching @EnableRabbit @EnableAsync public class Oss6Application { public static void main(String[] args) { SpringApplication.run(Oss6Application.class, args); } }
Controller层
/** * @author WGR * @create 2019/10/12 -- 21:53 */ @RestController public class AsynController { @Autowired AsynService asyncService; @GetMapping("/hello") public String hello(){ asyncService.hello(); return "success"; } }
Service层
/** * @author WGR * @create 2019/10/12 -- 21:52 */ @Service public class AsynService { //告诉Spring这是一个异步方法 @Async public void hello() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("处理数据中..."); } }
测试结果:
页面直接显示success,控制台过3秒显示处理数据中...
以上是Springboot中异步任务的示例分析的详细内容。更多信息请关注PHP中文网其他相关文章!