在开发中,经常需要在程序启动前做一些操作,比如:初始化数据,读取配置文件,数据库链接等需求;
Spring Boot 提供了两个接口,解决这种问题。分别是ApplicationRunner 和 CommandLineRunner,这两个接口的工作方式相同,都提供了run方法,都在 SpringApplication.run()完成之前调用;
如果使用了多个这样的实现,可以使用 org.springframework.core.annotation.Order 注解进行执行顺序指定;
实现:
第一个:
@Order(1)
@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public