有一些特殊的任务需要在系统启动时执行,例如配置文件加载、数据库初始化等操作。
Spring Boot对此提供了两种解决方案:CommandLineRunner和ApplicationRunner。
CommandLineRunner和ApplicationRunner基本一致,差别主要体现在参数上。
1. CommandLineRunner
1. 实现CommandLineRunner接口
SpringBoot项目在启动时会遍历所有CommandLineRunner的实现类并调用其中的run方法,如果整个系统中有多个CommandLineRunner的实现类,那么可以使用@Order注解对这些实现类的调用顺序进行排序。
import org.springframework.boot.Comm