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表达式,秒分时天等等,具体用到可以再百度


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM