1. 程式人生 > >spring boot 執行定時任務

spring boot 執行定時任務

在spring boot專案的啟動類中新增@EnableScheduling註解,表示開啟定時任務,如

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

然後只需要將@Scheduled新增到定時任務的方法上,如

@Scheduled(cron = "0 0 0 * * ?")
public void testCron() {
    log.info("每日清除資料字典快取開始*****************************");
    CacheUtil.cleanDictionaryCache();
    log.info("每日清除資料字典快取結束*****************************");
}

當spring boot專案啟動後就會執行定時任務