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));
        }
    }
}

 

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