SpringBoot添加定时器

【定时任务】
1.给start方法添加定时总开关
@EnableScheduling加在@SpringBootApplication注解的start入口处,表示启动总开关

@SpringBootApplication
@EnableScheduling
public class start {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(start.class, args);
    }
}

2.再单独给方法加配置
@Scheduled(cron="0/5 * * * * ?")每5秒打印当前时间

    @Scheduled(cron="0/5 * * * * ?")
    public void reportCurrentTime() {
        log.info("The time is now {}", dateFormat.format(new Date()));
    }

备注:cron表达式,秒分时天等等,具体用到可以再百度

posted @ 2018-07-17 15:21  李凯伦  阅读(2873)  评论(0编辑  收藏  举报