Skip to content

springboot项目启动加载

springboot项目在启动的时候预加载一些配置,有两种方式可以实现,一种是实现CommandLineRunner 接口,一种是实现ApplicationRunner 对象并且重写run()方法

一、实现CommandLineRunner

@Component
//通过order值的大小来决定启动的顺序,数字越小级别越高
@Order(2)
public class StartRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
       System.out.println("加载配置");
    }

}

二、实现ApplicationRunner

@Component
//通过order值的大小来决定启动的顺序,数字越小级别越高
@Order(3)
public class StartRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
         System.out.println("加载配置");
    }

}