在项目启动后执行某段功能代码
需求:springBoot项目启动之后直接执行某一段代码。
1. 方式一 实现ApplicationRunner接口,重写Run方法
@Component
@Order(1) //如果有多个runner需要指定一些顺序
public class SimosApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(111);
}
}
2. 方式二 实现CommandLineRunner接口,重写run方法,和上述差不多
二者区别
ApplicationRunner中run方法的参数为ApplicationArguments,而CommandLineRunner接口中run方法的参数为String数组。
这两个接口实现方式一样,它们都只提供了一个 run 方法,实现上述接口的类加入IOC容器即可生效。
4. IDEA启动时传入参数
参考:
https://www.cnblogs.com/fernfei/p/12090763.html
https://www.cnblogs.com/fernfei/p/12090764.html