其他分享
首页 > 其他分享> > SpringBoot异步任务

SpringBoot异步任务

作者:互联网

1、代码

重点是开启 @EnableAsync在service的方法标注@Async

@EnableAsync
@SpringBootApplication
public class SpringbootTask {

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

}
@Controller
public class AsyncController {

    @Autowired
    AsyncService asyncService;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        asyncService.hello();
        return "ok";
    }
}
@Service
public class AsyncService {
    @Async
    public void hello() {
        try {
            Thread.sleep(1000 * 3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("数据正在处理");
    }
}

标签:异步,SpringbootTask,SpringBoot,AsyncService,class,public,任务,hello,String
来源: https://www.cnblogs.com/kikyoqiang/p/15120453.html