TypeSafe-Configuration library for JVM languages

349 阅读1分钟

Configuration library for JVM languages using HOCON files

        <!--typesafe-->
        <dependency>
            <groupId>com.typesafe</groupId>
            <artifactId>config</artifactId>
            <version>1.3.3</version>
        </dependency>

my.conf

service {
  url="http://127/0.0.1:9090",
  name: "file.conf"
}

此文档格式是HOCON (Human-Optimized Config Object Notation), The primary goal is: keep the semantics (tree structure; set of types; encoding/escaping) from JSON, but make it more convenient as a human-editable config file format.

        //Config config = ConfigFactory.load();
        Config config = ConfigFactory.load("my.conf");
        String url = config.getString("service.url");
        Config service = config.getConfig("service");

ConfigFactory.load()会加载如下文件列表 (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)

参考文档:

HOCON