如何通过环境变量在名称中设置带下划线的Spring Boot属性? [英] How to set a Spring Boot property with an underscore in its name via Environment Variables?
问题描述
我想在Spring Boot应用程序中设置 hibernate.format_sql
。我想使用环境变量来设置它。
I want to set hibernate.format_sql
in a Spring Boot app. I want to set it using environment variables.
Spring Boot非常方便地转换所有环境变量,例如 FOO_BAR_BAZ
在Spring上下文中名为 foo.bar.baz
的属性。
Spring Boot rather handily converts all environment variables from, for example, FOO_BAR_BAZ
to properties called foo.bar.baz
inside the Spring context.
如何设置属性在Spring Boot中使用环境变量的目标名称中的下划线?据推测 HIBERNATE_FORMAT_SQL
将被翻译为 hibernate.format.sql
?
How can I set a property that has an underscore in the target name, in Spring Boot, using environment variables? Presumably HIBERNATE_FORMAT_SQL
will be translated to hibernate.format.sql
?
推荐答案
这是一个老问题,但我会回答它,以防其他人(像我一样)最终在这里寻找这些信息。
This is an old question but I'll answer it in case sombebody else (like me) ends up here looking for this information.
HIBERNATE_FORMAT_SQL应该可以做到这一点
HIBERNATE_FORMAT_SQL should do the trick
实际上,它不是翻译的OS环境变量,而是Spring属性名称。
Actually it is not the OS environment variable that is "translated" but rather the Spring property name that is.
该名称以多种方式翻译,并针对可用的环境变量进行查找。例如。 hibernate.format.sql被查找为:
The name is translated in several ways and looked up against available environment variables. E.g. "hibernate.format.sql" is looked up as:
- hibernate.format.sql(原样)
- hibernate_format_sql(点用下划线替换)
- hibernate_format_sql(用下划线代替破折号,在你的情况下也一样)
- hibernate_format_sql(破折号和放大器) ;点数替换为下划线,在您的情况下相同)
- hibernate.format.sql (as is)
- hibernate_format_sql (dots replaced with underscores)
- hibernate_format_sql (dashes replaced with underscores, the same in your case)
- hibernate_format_sql (dashes & dots replaced with underscores, the same in your case)
然后与大写相同:
- HIBERNATE.FORMAT.SQL(按原样)
- HIBERNATE_FORMAT_SQL(用下划线代替点)
- HIBERNATE_FORMAT_SQL(破折号替换为下划线,再次相同)
- HIBERNATE_FORMAT_SQL(短划线和点替换为下划线,再次相同)
- HIBERNATE.FORMAT.SQL (as is)
- HIBERNATE_FORMAT_SQL (dots replaced with underscores)
- HIBERNATE_FORMAT_SQL (dashes replaced with underscores, the same again)
- HIBERNATE_FORMAT_SQL (dashes & dots replaced with underscores, the same again)
虽然您无法使用set或export命令在名称中设置带点的环境变量,但可以使用env命令。我推迟判断这是否是个好主意:
Although you cannot set an environment variable with a dot in the name with the set or export commands it is however possible with the env command. I defer judgement whether this is a good idea or not:
env "my.dotted.name=\"a value\"" the-command-you-want-to-run
看看 SystemEnvironmentPropertySource.java
了解详情。我链接到特定版本,但您应该确保查看您正在使用的版本。
Have a look at SystemEnvironmentPropertySource.java for details. I link to a specific version but you should make sure to look at the version you are using.
要解决生产环境中的这些问题,您可以尝试打开调试属性解析代码的日志记录:
To troubleshoot these kinds of problems in a production environment you could try turning on debug logging for the property resolving code:
logging:
level:
org.springframework.core.env: DEBUG
...或通过设置适当的环境变量:)
... or by setting the appropriate environment variable :)
这篇关于如何通过环境变量在名称中设置带下划线的Spring Boot属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!