springboot_执行特定代码

在项目启动时,有时需要加载一些特定的静态文件或者执行某些特定的方法,springboot为我们提供了两种开机启动的接口

  1. CommandLineRunner
  2. ApplicationRunner

CommandLineRunner实现

@Component
public class MyCommand implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("执行我的代码。。。");
    }
}

执行结果

image.png

ApplicationRunner实现

@Component
public class MyCommand implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("执行application。。。");
    }
}

执行结果


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容