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