文章目录
说明:
在添加开启异步处理业务的时候,controller层调用service层返回数据为null;
springboot处理异步任务:
- 在异步方法上面添加一个@Async注解
- 在启动类上面天剑一个@EnableAsync开启异步功能
正常处理异步任务:
HelloService
package cn.itcast.springbootasyncmeathod.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* @ClassName: Hello
* @Auther: GZ
* @Date: 2021/10/23 21:12
* @Description:
*/
@Service
public class HelloService {
public String getHello() {
try {
Thread.sleep(3000);//线程沉睡三秒
} catch (InterruptedException e) {
e.printStackTrace();
}
return "HELLO";//向前端返回字符串
}
}
HelloController
package cn.itcast.springbootasyncmeathod.controller;
import cn.itcast.springbootasyncmeathod.service.HelloService;
import org.springframework