SpringShell应用启动时, 会默认向IOC容器中注入两个ApplicationRunner: ScriptShellApplicationRunner 和 InteractiveShellApplicationRunner, 其中ScriptShellApplicationRunner 的优先级要高于InteractiveShellApplicationRunner.
InteractiveShellApplicationRunner 指定顺序为0
@Order(InteractiveShellApplicationRunner.PRECEDENCE)
public class InteractiveShellApplicationRunner implements ApplicationRunner {
// ...
}
定义时, 指定顺序为InteractiveShellApplicationRunner 的顺序-100, 也就是优先级大于InteractiveShellApplicationRunner, 通过这种方式限制优先注册ScriptShellApplicationRunner.
@Order(InteractiveShellApplicationRunner.PRECEDENCE - 100) // Runs before InteractiveShellApplicationRunner
public class ScriptShellApplicationRunner implements ApplicationRunner {
// ...
}
配置类JLineShellAutoConfiguration中注入 interactiveApplicationRunner 和 scriptApplicationRunner 组件, 有条件注入, 默认为true.
@Configuration
public class JLineShellAutoConfiguration {
@Bean
@ConditionalOnProperty(prefix = SPRING_SHELL_INTERACTIVE, value = InteractiveShellApplicationRunner.ENABLED, havingValue = "true", matchIfMissing = true)
public ApplicationRunner interactiveApplicationRunner(Parser parser, Environment environment) {
return new InteractiveShellApplicationRunner(lineReader(), promptProvider, parser, shell, environment);
}
@Bean
@ConditionalOnProperty(prefix = SPRING_SHELL_SCRIPT, value = ScriptShellApplicationRunner.ENABLED, havingValue = "true", matchIfMissing = true)
public ApplicationRunner scriptApplicationRunner(Parser parser, ConfigurableEnvironment environment) {
return new ScriptShellApplicationRunner(parser, shell, environment);
}
}
本文只设计ApplicationRunner的运行流程, 不涉及bean 初始化等其它流程.
// 源码: org.springframework.boot.SpringApplication#callRunners
private void callRunners(ApplicationContext context, ApplicationArguments args) {
// 获取容器中定义的所有ApplicationRunner
List<Object> runners = new ArrayList();
runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
AnnotationAwareOrderComparator.sort(runners);
Iterator var4 = (new LinkedHashSet(runners)).iterator();
// 遍历所有ApplicationRunner, 执行其run方法.
while(var4.hasNext()) {
Object runner = var4.next();
if (runner instanceof ApplicationRunner) {
// callRunner()方法里就是执行runner.run(args) 方法
this.callRunner((ApplicationRunner)runner, args);
}
if (runner instanceof CommandLineRunner) {
this.callRunner((CommandLineRunner)runner, args);
}
}
}
// org.springframework.shell.jline.ScriptShellApplicationRunner#run**
@Override
public void run(ApplicationArguments args) throws Exception {
# 1.获取启动参数中所有以@开头的参数, 为每个脚本参数创建一个File对象, 组成File列表
List<File> scriptsToRun = args.getNonOptionArgs().stream()
.filter(s -> s.startsWith("@"))
.map(s -> new File(s.substring(1)))
.collect(Collectors.toList());
boolean batchEnabled = environment.getProperty(SPRING_SHELL_SCRIPT_ENABLED, boolean.class, true);
# 2.如果脚本参数不为空
if (!scriptsToRun.isEmpty() && batchEnabled) {
// 3.设置关闭交互式方式, 保证运行为脚本的所有命令之后, 直接关闭程序
InteractiveShellApplicationRunner.disable(environment);
// 4.遍历每个脚本, 为每个脚本创建一个命令提供源, 依次执行shell的run方法.
for (File file : scriptsToRun) {
try (Reader reader = new FileReader(file);
FileInputProvider inputProvider = new FileInputProvider(reader, parser)) {
shell.run(inputProvider);
}
}
}
}
// 源码: org.springframework.shell.jline.InteractiveShellApplicationRunner#run
@Override
public void run(ApplicationArguments args) throws Exception {
// 获取交互方式是否打开, 如果ScriptShellApplicationRunner 中执行了第三步, 此处获取即为false.
boolean interactive = isEnabled();
// 如果交互方式打开(为true), 则创建控制台输入源, 执行shell 的run方法
if (interactive) {
InputProvider inputProvider = new JLineInputProvider(lineReader, promptProvider);
shell.run(inputProvider);
}
}
// 源码: org.springframework.shell.Shell#run
public void run(InputProvider inputProvider) throws IOException {
// 自定义循环退出码
Object result = null;
// 无限循环, 知道result为退出嘛
while (!(result instanceof ExitRequest)) {
Input input;
// 从输入源中读取一条输入
try {
input = inputProvider.readInput();
}
catch (Exception e) {
resultHandler.handleResult(e);
continue;
}
// 当读取的输入为null时, 跳出循环, 结束此shell的运行
if (input == null) {
break;
}
// 执行命令, 相应结果
result = evaluate(input);
// 处理结果, 回显终端
if (result != NO_INPUT && !(result instanceof ExitRequest)) {
resultHandler.handleResult(result);
}
}
}