在Spring Boot中访问命令行参数

x33g5p2x  于2022-10-25 转载在 Spring  
字(2.8k)|赞(0)|评价(0)|浏览(813)

让我们学习如何读取Spring Boot应用程序的命令行参数。
与任何其他java程序一样,Spring Boot可以在启动时获取应用程序参数的程序参数。但springboot在参数解析方面做了额外的工作。让我们进入细节。

CommandLineRunner和ApplicationRunner

这两个接口本质上相似,有助于访问命令行参数。

CommandLineRunner读取参数

Spring Boot中的CommandLineRunner接口允许您访问所有原始命令行参数。如果您愿意阅读,我们有一篇关于springboot中命令行运行程序的详细文章。基本上,您可以编写类型为CommandLineRunner的Bean或Component来访问参数。例如,让我们按照此处所示编写跑步者。

@Component
public class StartupPrintRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("args = " + Arrays.deepToString(args));
    }
}
Code language: Java (java)

如果我们用很少的参数运行应用程序,您可以看到应用程序正在打印它们。

java -jar spring-boot-example.jar first-argument second-argument third-argument
Code language: Bash (bash)

进一步,让我们看看CommandLineRunner的一些缺点。
1.参数只能作为字符串列表获取。
1.没有参数分析。例如,如果您发送--some-option=some-value,则它们将作为单个参数(["--some-option=some-value"])。
1.不能将这些参数直接注入其他组件。

用于分析命令行参数的ApplicationRunner

默认情况下,Spring boot提供类型为ApplicationArguments的bean。此bean包含所有已解析和未解析的命令行参数。重要的是,你可以在你想要的任何地方自动连线。

  • ApplicationArguments的重要特性是它们可以将参数解析为键值对。例如,您可以传递–some argument=some value*。然后,该参数将被解析为*{“some argument”:“some value”}*的键值对。
@Component
public class SomeComponent {
    private final ApplicationArguments applicationArguments;

    public SomeComponent(ApplicationArguments applicationArguments) {
        this.applicationArguments = applicationArguments;
    }
    
    //do something with applicationArguments in methods
    
}
Code language: Java (java)

但是,如果您希望获得与CommandLineRunner类似的行为,则应使用ApplicationRunner接口。下面是方法。

@Component
public class StartupApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("args.getOptionNames() = " + args.getOptionNames());
        System.out.println("args.containsOption(\"first-argument\") = " + args.containsOption("first-argument"));
        System.out.println("args.getOptionValues(\"first-argument\") = " + args.getOptionValues("first-argument"));
        System.out.println("args.getNonOptionArgs() = " + args.getNonOptionArgs());
        args.getNonOptionArgs();

    }
}
Code language: Java (java)

请注意,我们使用了ApplicationArguments接口中的各种方法。
1.args.getOptionNames()–返回已解析参数键的列表。
1.args.containsOption()–根据给定选项的存在检查并返回布尔值。
1.args.getOptionValues()–将所有匹配值作为给定选项的数组返回(是的,可以多次添加选项)
1.args.getNonOptionArgs()–返回所有未遵循–option=value格式的孤立参数。
如果您感兴趣,那么可以使用以下命令运行应用程序并自己查看结果。

mvn clean install
java - jar spring-boot-command-line-args-0.0.1-SNAPSHOT.jar --first-argument=first-value --second-argument=second-value third-argument
Code language: Bash (bash)

应用程序参数的Spring Boot PropertySource

Spring Boot还在Spring上下文中注册CommandLinePropertySource。这个属性源允许我们将解析的参数直接注入组件。

@Component
public class SomeComponent {

    @Value("${second-argument}")
    private String secondArgument;
    
    //more autowiring
}
Code language: Java (java)

这种方法等同于如何应用。属性的行为。这意味着您可以使用命令行参数覆盖应用程序属性。

总结

总之,我们学习了在springboot中访问应用程序参数的各种方法。您可以在我们的GitHub repository中找到所有这些示例。

相关文章