首页 > 解决方案 > 带有命令行运行器界面的 Spring shell

问题描述

我在运行 Spring Boot Spring Shell 应用程序时遇到问题。我已经实现了 commandlinerunner,但它从未在应用程序启动期间调用过。

@Component
public class ParamReader implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        if(args.length>0) {
            System.out.println(Arrays.asList(args).stream().collect(Collectors.joining(", ")));
        }
    }
}

当我从 shell 运行 exit 命令时,它调用 run 方法,然后程序终止。我尝试在 Spring 应用程序主类上实现 commandlinerunner,但结果是一样的。

标签: javaspringspring-bootcommand-line-argumentsspring-shell

解决方案


Spring shell 使用ApplicationRunnerwith@Order注释和precedence 0. 您可以尝试以下方法:

@Order(-1)
@Component
public class ParamReader implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        if(args.length>0) {
            System.out.println(Arrays.asList(args).stream().collect(Collectors.joining(", ")));
        }
    }
}

推荐阅读