CommandLineRunner實現項目啓動後執行任務

實現在項目啓動後執行的功能,SpringBoot提供的一種簡單的實現方案就是添加一個Component組件,組件類實現CommandLineRunner接口,把項目啓動後要執行的任務的代碼放在run方法中

如果有多個任務,可以使用@Order(value=1)註解決定任務執行的先後順序

@Slf4j
@Component
public class CashValueTableCacheListener implements CommandLineRunner {

   
    @Autowired
    TableDataCache tableDataCache;

    @Override
    public void run(String... args) throws Exception {
        long start = System.currentTimeMillis();
        log.info("[{}][開始]", "加載數據");
        try {
            tableDataCache.loadCashValueTableCache();
          

        } catch (Exception e) {
            log.error("[{}][異常信息:{}][耗時:{}ms][結束]", e, methodMessage, (System.currentTimeMillis() - start));
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章