1 java配置工具typesafe config
- 以纯Java实现,没有依赖项
- 支持三种格式的文件:Java属性,JSON和人类友好的JSON超集
- 合并所有格式的多个文件
- 可以从文件,URL或类路径加载
- 对“嵌套”的良好支持(将配置的任何子树与整个配置相同)
- 用户可以使用Java系统属性java -Dmyapp.foo.bar = 10覆盖配置。
- 支持通过单个文件(例如application.conf)配置带有其框架和库的应用程
- 解析持续时间和大小设置,“ 512k”或“ 10秒”
- 转换类型,因此,如果您要求布尔值并且值是字符串“ yes”,或者您要求浮点数并且值是int值,它将进行计算。
- JSON超集功能:
注释
包括
替换(“ foo”:$ {bar},“ foo”:Hello $ {who})
类似属性的符号(a.b = c)
噪音少,语法宽松
替代环境变量(logdir = $ {HOME} / logs) - 基于不可变Config实例的API,以确保线程安全和易于进行有关配置转换的推理
- 广泛的测试范围
该库将自身限制为配置文件。 如果要从数据库或其他内容加载配置,则需要编写一些自定义代码。 该库对合并配置提供了很好的支持,因此,如果您从自定义源构建一个,则很容易将其合并。
2 仓库依赖
如下所示是maven依赖样例:
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.4.0</version>
</dependency>
如下所示是sbt依赖:
libraryDependencies += "com.typesafe" % "config" % "1.4.0"
链接地址:
https://repo1.maven.org/maven2/com/typesafe/config/
3 代码案例
java使用如下所示:
import com.typesafe.config.ConfigFactory
Config conf = ConfigFactory.load();
int bar1 = conf.getInt("foo.bar");
Config foo = conf.getConfig("foo");
int bar2 = foo.getInt("bar");
In Scala, a Settings class might look like:
class Settings(config: Config) {
// validate vs. reference.conf
config.checkValid(ConfigFactory.defaultReference(), "simple-lib")
// non-lazy fields, we want all exceptions at construct time
val foo = config.getString("simple-lib.foo")
val bar = config.getInt("simple-lib.bar")
}
4 标准加载路径:
The convenience method ConfigFactory.load()
loads the following (first-listed are higher priority):
- system properties
application.conf
(all resources on classpath with this name)application.json
(all resources on classpath with this name)application.properties
(all resources on classpath with this name)reference.conf
(all resources on classpath with this name)
The idea is that libraries and frameworks should ship with a reference.conf
in their jar. Applications should provide an application.conf
, or if they want to create multiple configurations in a single JVM, they could use ConfigFactory.load("myapp")
to load their own myapp.conf
.
5 更多链接
Guice integration
- Typesafe Config Guice https://github.com/racc/typesafeconfig-guice
Java (yep!) wrappers for the Java library
- tscfg https://github.com/carueda/tscfg
Scala wrappers for the Java library
- Ficus https://github.com/ceedubs/ficus
- configz https://github.com/arosien/configz
- configs https://github.com/kxbmap/configs
- config-annotation https://github.com/zhongl/config-annotation
- PureConfig https://github.com/pureconfig/pureconfig
- Simple Scala Config https://github.com/ElderResearch/ssc
- konfig https://github.com/vpon/konfig
- ScalaConfig https://github.com/andr83/scalaconfig
- static-config https://github.com/Krever/static-config
- validated-config https://github.com/carlpulley/validated-config
- Cedi Config https://github.com/ccadllc/cedi-config
- Cfg https://github.com/carueda/cfg
- circe-config https://github.com/circe/circe-config
- args4c https://github.com/aaronp/args4c