Springboot中异步任务的示例分析
作者:小确幸
时间:2023-05-13
浏览:0
异步任务启动类@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秒显示处理数据中...
作者最新文章
Photoshop抠图教程详细步骤图解:新手入门常用方法与技巧
2026-09-22 14:38
Windows 10
2026-09-16 17:44
Python安装后怎么打开:使用IDLE或命令行启动解释器
2026-09-16 13:54
Windows系统Python安装教程:下载、勾选PATH及环境变量配置
2026-09-16 13:53
“等灯不计时”落地解析:算法善意如何转化为技术能力与生态协同
2026-09-08 18:03
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多


































