其他分享
首页 > 其他分享> > 27-异步任务

27-异步任务

作者:互联网

异步任务

1.创建新项目

2.service层

@Service
public class AsyncService {
    //告诉spring这是一个异步的方法
    @Async
    public void hello(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("数据正在处理...");
    }
}

3.controller层

@RestController
public class AsyncController {
    @Autowired
    private AsyncService asyncService;
    @RequestMapping("/hello")
    public String hello(){
        asyncService.hello();
        return "ok";
    }
}

4.主程序开启异步注解功能

//开启异步注解功能
@EnableAsync
@SpringBootApplication
public class Spring09TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(Spring09TestApplication.class, args);
    }

}

标签:异步,27,String,class,hello,AsyncService,任务,public
来源: https://blog.csdn.net/xixihaha_coder/article/details/120487498