启动类
@mapperscan("com.topcheer.*.*.dao")@springbootapplication@enablecaching@enablerabbit@enableasyncpublic class oss6application { public static void main(string[] args) { springapplication.run(oss6application.class, args); }}
controller层
/** * @author wgr * @create 2019/10/12 -- 21:53 */@restcontrollerpublic class asyncontroller { @autowired asynservice asyncservice; @getmapping("/hello") public string hello(){ asyncservice.hello(); return "success"; }}
service层
/** * @author wgr * @create 2019/10/12 -- 21:52 */@servicepublic class asynservice { //告诉spring这是一个异步方法 @async public void hello() { try { thread.sleep(3000); } catch (interruptedexception e) { e.printstacktrace(); } system.out.println("处理数据中..."); }}
测试结果:
页面直接显示success,控制台过3秒显示处理数据中...
以上就是springboot中异步任务的示例分析的详细内容。