SpringBoot中提供了两个Runner----->CommandLineRunner、 ApplicationRunner
这两个是接口,接口中的方法在系统启动时会执行,可以用于加载配置文件,数据库设置等。
不过监听器也可以实现。
只需实现接口,并注入容器,SpringBoot在启动时就会调用run方法。
@Order(2)//设置顺序
@Component
public class AppRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
// TODO Auto-generated method stub
System.out.println("启动参数"+args);
}
}
@Order(1)
@Component
public class ComLine implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("命令行...输出"+args);
}
}
命令行...输出[Ljava.lang.String;@4b8cf5e2
启动参数org.springframework.boot.DefaultApplicationArguments@73b454e9
两个接口使用方式一样,方法中参数分别是不定长字符串、字符串数组,可以接收启动时的参数,如args.getNonOptionArgs(arg_name)。
具体调用时间在容器刷新完成之后,略迟与刷新监听器。