java - HOCON替换默认值

标签 java properties typesafe-config hocon

在 HOCON 和 Typesafe Config 中,如何在替换的情况下设置默认值。

它支持这样的东西吗??

${server.host: 'localhost'} -> 如果设置了 server.host(在相同的配置文件中或通过环境设置),它会替代,如果未设置,则选择默认值

最佳答案

来自 substitutions 上的官方文档:

If a substitution with the ${?foo} syntax is undefined:

  • if it is the value of an object field then the field should not be created. If the field would have overridden a previously-set value for the same field, then the previous value remains.

所以这是一种可能的解决方法,使用 object merging :

defaults {
  foo: "default Value"
}

item = ${defaults} {
  foo: ${?bar}
}

或者更简单:

item = {
  foo: "default Value"
  foo: ${?bar}
}

关于java - HOCON替换默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38822992/

相关文章:

java - 一对一注释不会将外键插入表中

java - 使用辅助私有(private)方法初始化最终属性

scala - 主目录中的类型安全配置文件

scala - 命令行参数不适用于 sbt 程序集 jar

javascript - Immutability 和 Singleton 解决的是同一个目的吗?

java - 什么时候考虑 Java 中的代码效率?

Spring Boot 在 SpringApplication.run 之前检查属性

java - java中字符串的字体设置

apache-spark - 带有类型安全配置的 Spark 2.1.1

java - 处理程序和调度程序之间的区别