如何通过环境变量设置名称中带有下划线的 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 somebody else (like me) ends up here looking for this information.
HIBERNATE_FORMAT_SQL 应该可以解决问题
HIBERNATE_FORMAT_SQL should do the trick
实际上,翻译"的并不是操作系统环境变量.而是 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(原样)
- HIBERNATE_FORMAT_SQL(点替换为下划线)
- HIBERNATE_FORMAT_SQL(破折号替换为下划线,再次相同)
- HIBERNATE_FORMAT_SQL(破折号和点替换为下划线,再次相同)
尽管您不能使用 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 文档主题:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config
I highly recommend being familiar with the relevant Spring Boot documentation topic: https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config
对于这里评论中更棘手的例子,例如spring.jpa.properties.hibernate.criteria.literal_handling_mode
,根据您启动应用程序的方式,可能有不同的解决方案.
For the more tricky examples in the comments here, e.g. spring.jpa.properties.hibernate.criteria.literal_handling_mode
, there might be different solutions available depending on how you launch you application.
您可以将变量设置为 JSON,嵌入到环境变量中.
You could set the variable as JSON, embedded in an environment variable.
env SPRING_APPLICATION_JSON='{"spring":{"jpa":{"properties":{"hibernate":{"criteria":{"literal_handling_mode":"BIND"}}}}}}' ./gradlew bootRun
简单地按原样设置变量也可以:
Simply setting the variable as is might work also:
env spring.jpa.properties.hibernate.criteria.literal_handling_mode=BIND ./gradlew bootRun
以上两个都在我的设置中起作用,因为我能够通过这种方式在正在运行的 Spring Boot 应用程序中获取值:
Both of the above worked in my setup in so far as I was able to get the value in the running Spring Boot application this way:
@Value("${spring.jpa.properties.hibernate.criteria.literal_handling_mode}")
private String testSettingThroughEnvVariable;
希望这有帮助!赞一个
这篇关于如何通过环境变量设置名称中带有下划线的 Spring Boot 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!