【问题标题】:SpringBoot Passing parameter to CommandLineRunnerSpring Boot 将参数传递给 CommandLineRunner
【发布时间】:2020-05-26 02:49:26
【问题描述】:

我正在使用SpringBoot 2.1.3,我正在尝试通过命令行传递一些参数。

这是main 类:

@SpringBootApplication
@Slf4j
public class Application implements CommandLineRunner {

@Value("${priceOne.pound}")
private Integer p1pound;

@Value("${priceOne.shilling}")
private Integer p1shilling;

@Value("${priceOne.pences}")
private Integer p1pences;

@Value("${priceTwo.pound}")
private Integer p2pound;

@Value("${priceTwo.shilling}")
private Integer p2shilling;

@Value("${priceTwo.pences}")
private Integer p2pences;

@Value("${factor}")
private Integer factor;

@Value("${op}")
private String op;

public static void main(String[] args) {
    SpringApplication.run(EurisApplication.class, args);
}

@Override
public void run(String... args) {
    Price priceOne = new Price(p1pound, p1shilling, p1pences);
    Price priceTwo = new Price(p2pound, p2shilling, p2pences);
    ....
    ....
}

我尝试用以下内容填充这些变量:

spring-boot:run -Drun.arguments=--priceOne.pound=5, --priceOne.shilling=5, --priceOne.pences=5, --priceTwo.pound=5, --priceTwo.shilling=5, --priceTwo.pences=5, --factor=0, --op=+

或与:

spring-boot:run -Dspring-boot.run.arguments=--priceOne.pound=5, --priceOne.shilling=5, --priceOne.pences=5, --priceTwo.pound=5, --priceTwo.shilling=5, --priceTwo.pences=5, --factor=0, --op=+

如果我在参数列表的开头和结尾添加引号" ",则会出现转换错误..

你能帮帮我吗?

非常感谢。


感谢您的回复!

如果我删除除第一个参数之外的所有参数,然后启动应用程序:

 spring-boot:run -Dspring-boot.run.arguments=--priceOne.pound=5

工作正常。您提到的其他方式:

 spring-boot:run --priceOne.pound=5

用一个参数和多个参数检索解析异常。我有点糊涂了。。

编写整个应用程序花费的时间比编写该死的commandLine 启动要少。我无法将所有参数作为单个 String.. 我不明白为什么只有一个参数有效而两个参数无效!

谢谢

【问题讨论】:

    标签: spring-boot command-line-arguments


    【解决方案1】:

    正如@improbable 提到的,-Dspring-boot.run.arguments 用于将参数传递给main() 方法。

    为了设置@Value属性,直接使用--语法:

    spring-boot:run --priceOne.pound=5 --priceOne.shilling=5 --priceOne.pences=5, --priceTwo.pound=5 --priceTwo.shilling=5 --priceTwo.pences=5 --factor=0 --op=+
    

    请查看relevant documentation

    【讨论】:

      【解决方案2】:

      您误用了@Value。它用于注入外部属性。

      如果您没有对应的外部化属性 - 所有这些字段都将为空。

      除此之外,在

      @Override
      public void run(String... args) {
      

      String... args 是可变参数形式的命令行参数。

      例如,如果您执行下一行,您将拥有包含单个元素“first_arg_string”的可变参数数组,该元素由索引抓取。

      java -jar yourjar.jar "first_arg_string"
      

      【讨论】:

        【解决方案3】:

        阅读(尤其是 baldeung 文档)我找到了解决问题的方法我自己(最近 85% 的情况都会发生这种情况,也许论坛已成为太大了..)。

        然后我发布解决方案,希望它可以节省其他人的时间。现在,考虑上面的代码(为了清楚起见,我再次提出):

        @SpringBootApplication
        @Slf4j
        public class Application implements CommandLineRunner {
        
        @Value("${priceOne.pound}")
        private Integer p1pound;
        
        @Value("${priceOne.shilling}")
        private Integer p1shilling;
        
        @Value("${priceOne.pences}")
        private Integer p1pences;
        
        @Value("${priceTwo.pound}")
        private Integer p2pound;
        
        @Value("${priceTwo.shilling}")
        private Integer p2shilling;
        
        @Value("${priceTwo.pences}")
        private Integer p2pences;
        
        @Value("${factor}")
        private Integer factor;
        
        @Value("${op}")
        private String op;
        
        public static void main(String[] args) {
           SpringApplication.run(EurisApplication.class, args);
        }
        
        @Override
        public void run(String... args) {
         System.out.println(p1pound + " " + p1shilling + " " + p1pences + " " +
                            p2pound + " " + p2shilling + " " + p2pences + " " +
                            factor + " " + op;
        }
        

        如果您想使用maven 或在Eclipse 内部使用maven launch configurator 启动具有多个参数的应用程序(见下图)

        你必须使用的字符串是:

        spring-boot:run -Dspring-boot.run.arguments=--spring.main.banner-mode=off,--priceOne.pound=5,--priceOne.shilling=5,--priceOne.pences=5,--priceTwo.pound=4,--priceTwo.shilling=4,--priceTwo.pences=4,--factor=5,--op=+
        

        上面的String会根据run()方法里面的代码产生如下输出:

        5 5 5 4 4 4 5 +
        

        如果您构建应用程序(使用Jar Packaging)并希望从Terminal(例如Windows 中的cmd)启动它,您应该使用以下字符串:

        java -jar [package_name].jar --priceOne.pound=5 --priceOne.shilling=5 --priceOne.pences=5 --priceTwo.pound=4 --priceTwo.shilling=4 --priceTwo.pences=4 --factor=5 --op=+
        

        它会产生相同的结果。

        如果你只想传递一个参数,你应该使用下面的字符串和maven

        spring-boot:run -Dspring-boot.run.arguments=--priceOne.pound=5
        

        来自cmd

        java -jar [package_name].jar --priceOne.pound=5
        

        P.S.:这些解决方案适用于 SpringBoot ver >= 2.x

        希望有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-01-19
          • 2020-03-08
          • 2018-05-27
          • 2015-11-26
          • 2015-05-23
          • 2017-02-02
          • 2020-02-09
          • 1970-01-01
          相关资源
          最近更新 更多