Springboot中异步任务的示例分析
作者:小确幸
时间:2023-05-13
浏览:1
异步任务启动类@MapperScan("com.topcheer.*.*.dao")@SpringBootApplication@EnableCaching@EnableRabbit@EnableAsyncpublicclassOss6Application{publicstaticvoidmain(String[]args){SpringApplication.run(Oss6Application.class,args);}}Controller层/***@authorWGR*@create2019/
本文目录
异步任务
启动类
@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秒显示处理数据中...
作者最新文章
漫蛙漫画下载免费安卓最新版本
2026-10-13 13:18
漫蛙下载最新版安装包
2026-10-13 13:18
漫蛙manwaapp安卓最新版下载
2026-10-13 13:18
漫蛙动漫APP下载安装最新版本
2026-10-13 13:17
漫蛙漫画APP官方下载最新版本
2026-10-13 13:17
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多

































