Spring 提供了两个在程序完成初始化后执行定制任务的接口,它们分别是 CommandLineRunner 和 ApplicationRunner。以下是两个接口的源代码:
@FunctionalInterface
public interface CommandLineRunner {
void run(String... args) throws Exception;
}
@FunctionalInterface
public interface ApplicationRunner {
void run(ApplicationArguments args) throws Exception;
}
可以看到两个接口都是 Functional Interface,也就是说我们不需要使用额外的代码来实现这些接口,只要使用 Lambda 或者匿名类就可以直接把业务逻辑代码写进程序了。CommanLineRunner 回调函数参数是字符串数组,而 ApplicationRunner 的参数是 ApplicationArguments ,这两个参数的区别是 ApplicationRunner 做了预处理,可以支持可选参数功能。也就是在启动程序的时候命令行有诸如 --option1=o1 这样的参数,它会解析为 option1 和它对应的值 o1,我们就不用自己另写程序处理了。
我们来看个例子:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
CommandLineRunner showProperties() {
CommandLineRunner runner = (args -> {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < args.length; i++) {
sb.append(args[i]);
if (i != args.length - 1)
sb.append(",");
}
System.out.println(String.format("CommandLineRunner: %s", sb.toString()));
});
return runner;
}
@Bean
ApplicationRunner testAppRunner() {
return (args -> {
System.out.println(String.format("ApplicationRunner: 可选变量数: %d, 总变量数: %d",
args.getOptionNames().size(), args.getNonOptionArgs().size()));
StringBuilder sb = new StringBuilder();
for (String arg: args.getOptionNames()
) {
sb.append(arg);
sb.append(':');
sb.append(args.getOptionValues(arg));
sb.append(", ");
}
System.out.println(String.format("不可选变量:%s", args.getNonOptionArgs()));
System.out.println(String.format("可选变量列表: %s", sb.toString()));
});
}
}
上面这段程序有两个类工厂,一个是 CommandLineRunner 的,一个是 ApplicationRunner 的,当程序启动完毕,调用这两个接口的回调函数也就是 run 方法的时候,程序会打出对应的命令行参数列表。
我们使用下面的命令行参数来启动上面的程序。可以看到,我在命令行上的参数是:a1 a2 --option1=o1 --option2=o2 --debug=logfile.txt
下面是程序的运行结果:
大家可以看到,Spring Boot 首先执行 ApplicationRunner,它发现了 3 个可选变量和 2 个不可选变量,不可选变量是 a1 和 a2,可选变量是:debug:[logfile.txt], option1:[o1], option2:[o2],而 CommandLineRunner 把 5 个变量不加区别地一起显示了出来,没做任何加工。
所以如果我们的程序想用可选变量功能,那么我们可以使用 ApplicationRunner 来帮助我们直接拿到可选变量的键值列表,否则我们可以直接使用CommandLineRunner。
总结
本文介绍了 CommandLineRunner 和 ApplicationRunner 的异同和使用方法。这两个接口的 run 函数都是在 Spring 应用初始化完成后被调用。前者直接给出了所有的命令行参数,而后者会做一些命令行参数的预处理。这两个接口都是 Functional 接口,本文给出了使用 Lambda 表达式来实现类工厂的示例,并给出了运行结果示例。
参考链接
本文程序的源代码
ApplicationArguments
Running Setup Logic on Startup in Spring