最近在项目中一直使用定时任务完成一些业务逻辑,比如天气接口的数据获取,定时发送短信,邮件。以及商城中每天用户的限额,定时自动收货等等。定时器在项目中是我们常常会使用到的一个手段,今天我们就来看下在SpringBoot中如何集成定时任务。
定时任务在Spring Boot中的集成
在启动类中加入开启定时任务的注解:
在SpringBoot中使用定时任务相当的简单。首先,我们在启动类中加入@EnableScheduling来开启定时任务。
/**
* 启动类
*/
@RequestMapping(value = "/")
@RestController
@SpringBootApplication
@MapperScan(basePackages = "com.zzp.dao")
@Configuration
@EnableSwagger2
@EnableScheduling
public class Round1Application {
@RequestMapping(value = "/",method = RequestMethod.GET)
public String helloWorld(){
return "Hello World";
}
public static void main(String[] args) {