Spring boot SpringBoot将参数传递给CommandLineRunner

Spring boot SpringBoot将参数传递给CommandLineRunner,spring-boot,command-line-arguments,Spring Boot,Command Line Arguments,我使用的是SpringBoot 2.1.3,我试图通过命令行传递一些参数 以下是主要课程: 我试图用以下内容填充这些变量: 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=+ 或与: spri

我使用的是SpringBoot 2.1.3,我试图通过命令行传递一些参数

以下是主要课程:

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

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=+
如果在参数列表的开头和结尾添加引号,则会出现转换错误

你能帮我吗

非常感谢

谢谢你的回复

如果我删除所有参数eccept the first并启动应用程序,则:

 spring-boot:run -Dspring-boot.run.arguments=--priceOne.pound=5
很好。您提到的其他方式包括:

 spring-boot:run --priceOne.pound=5
使用一个参数和多个参数检索解析异常。我有点困惑

写整个应用程序比写那该死的命令行启动花的时间要少。我无法将所有参数作为单个字符串获取。。我不明白为什么只有一个参数有效而两个参数无效

谢谢你误用了。它用于注入外部化属性

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

除此之外,在

@Override
public void run(String... args) {
字符串。。。args是varargs形式的命令行参数

例如,若您执行下一行,您将拥有varargs数组,该数组包含单个元素first_arg_string,该字符串由索引获取

java -jar yourjar.jar "first_arg_string"
正如@impobable提到的,-Dspring-boot.run.arguments用于将参数传递给main方法

要设置@Value属性,请直接使用-syntax:

弹簧靴:run-priceOne.pound=5-priceOne.shilling=5-priceOne.pences=5-priceTwo.pound=5-priceTwo.shilling=5-priceTwo.pences=5-factor=0-op=+
请查看周围的

阅读资料,尤其是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或使用maven launch configurator在Eclipse内部启动具有多个参数的应用程序,请参见下图

必须使用的字符串是:

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=+
上面的字符串将根据运行方法内部的代码生成以下输出:

5 5 5 4 4 4 5 +
例如,如果使用Jar打包构建应用程序,并希望从Windows中的Terminal 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中使用以下字符串:

从cmd:

注意:这些解决方案适用于SpringBoot版本>=2.x

希望有帮助

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=+
spring-boot:run -Dspring-boot.run.arguments=--priceOne.pound=5
java -jar [package_name].jar --priceOne.pound=5