springboot_执行特定代码
2022-03-26 本文已影响0人
一个老程序员
在项目启动时,有时需要加载一些特定的静态文件或者执行某些特定的方法,springboot为我们提供了两种开机启动的接口
- CommandLineRunner
- ApplicationRunner
CommandLineRunner实现
@Component
public class MyCommand implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("执行我的代码。。。");
}
}
执行结果
![](https://img.haomeiwen.com/i9767387/2138a8b54438a0d5.png)
ApplicationRunner实现
@Component
public class MyCommand implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("执行application。。。");
}
}
执行结果
![](https://img.haomeiwen.com/i9767387/56a262618cc598aa.png)