SpringBoot 自定义初始化任务 Runner

在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等。可以通过实现Runner接口完成以上工作。

两者只是参数上的区别

方式一 实现 CommandLineRunner 接口

@Component
public class VipSoftServerRunner implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(JmxhphServerRunner.class);

    @Override
    public void run(String... args) throws Exception {
        System.out.println("项目启动,执行 CommandLineRunner 实现类的方法");
    }
}

方式二 实现 ApplicationRunner 接口

@Component
public class VipSoftServerRunner implements ApplicationRunner{
    
    private static final Logger logger = LoggerFactory.getLogger(JmxhphServerRunner.class);

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("项目启动,执行 ApplicationRunner 实现类的方法");
    }
}

 

posted @ 2022-07-04 15:40  VipSoft  阅读(76)  评论(0编辑  收藏  举报