如何将空字符串作为命令行 属性 传递给 Spring?

How to pass empty string as a command line property to Spring?

仅供参考:我正在使用 Intellij 的 'Run/Debug Configurations' 工具。

我想通过命令行将一个空字符串作为 属性 传递给我的应用程序:

--spring.ldap.username=

我试过:

--spring.ldap.username= # results in parse error
--spring.ldap.username=''
--spring.ldap.username="" # results in parse error
--spring.ldap.username=\"\"

实际解析成功的尝试产生了不正确的值,正如我尝试打印 'empty' 字符串时所证明的那样:

System.out.println(environment.getProperty("spring.ldap.username"));

// returns:
// ''
// ""

在 application.properties 文件中将相同的 属性 设置为空字符串效果很好:

spring.ldap.username=

相同的打印语句:

// returns:
//
// ^^ totally empty string

有没有我遗漏的技巧?

试试这个:--spring.ldap.username(如果您保留 =,它将解析为 null)

事实证明,这可以通过使用常规 JVM 参数来完成。

而不是

--my.property=

使用

-Dmy.property=

我猜本机 JVM 参数能够接受空字符串。

在 IntelliJ 中,您可以将它们添加到 Run/Debug 配置 window 的相应字段中,如下所示:

This answer 另一个问题也有一些关于将 JVM 参数传递给 Spring 应用程序的更多信息。