scala 中配置文件之 config

Typesafe的Config库,纯Java写成、零外部依赖、代码精简、功能灵活、API友好。支持Java properties、JSON、JSON超集格式HOCON以及环境变量。它也是Akka的配置管理库.

Overview

纯java实现,无任何依赖
充分的测试
支持: Java properties, JSON, and a human-friendly JSON superset
可以合并各种格式的配置文件
可以通过文件、urls、classpath加载配置
支持多层嵌套的配置方式
识别Java system properties, 如java -Dmyapp.foo.bar=10
可以转换长短,大小等单位。如配置文件中timeout=10s,则可以转换成任意的毫秒或者
类型转换,比如yes可以转换为boolean类型的true
JSON superset features:
    comments
    includes
    substitutions ("foo" : ${bar}, "foo" : Hello ${who})
    properties-like notation (a.b=c)
    less noisy, more lenient syntax
    substitute environment variables (logdir=${HOME}/logs)

example
默认加载classpath下的application.conf,application.json和application.properties文件。通过ConfigFactory.load()加载。

# these are our own config values defined by the app
simple-app {
    answer=42
}
# Here we override some values used by a library
simple-lib.foo="This value comes from simple-app's application.conf"
simple-lib.whatever = "This value comes from simple-app's application.conf"
public class SimpleLibContext {
    private Config config;

    //指定配置文件
    public SimpleLibContext(Config config) {
        this.config = config;
        config.checkValid(ConfigFactory.defaultReference(), "simple-lib");
    }

    // 默认加载classpath下的application.*
    public SimpleLibContext() {
        this(ConfigFactory.load());
    }

    //打印
    public void printSetting(String path) {
        System.out.println("The setting '" + path + "' is: " + config.getString(path));
    }

    public static void main(String[] args) {
        SimpleLibContext s = new SimpleLibContext();
        s.printSetting("simple-app.answer");
    }
}

config 文件的操作
配置内容即可以是层级关系,也可以用”.”号分隔写成一行
如:

akka {
 host = "0.0.0.0"
 port = 9999
} 

用“.” 写成一行,如:

akka.host = "0.0.0.0"
akka.port = 9999 

当有多个配置文件时,可以使用include 引入,如

calculator {
 include "common"
 akka {
 remote.netty.port = 2552
 }
}

另外:
akka2使用Typesafe Config库,可以使用ConfigFactory.load()加载配置文件,默认加载classpath下的application.conf, application.json and application.properties文件。ActorSystem将会把这些配置和reference.conf合并(merge)起来。

如果要写akka应用,将配置写在classpath根目录下的application.conf文件中。
如果要写基于akka的lib包,将配置写在jar包内的根目录下的reference.conf文件中.

如果多个config 文件有冲突时,解决方案有
1. a.withFallback(b)? //a和b合并,如果有相同的key,以a为准
2. a.withOnlyPath(String path)? //只取a里的path下的配置
3. a.withoutPath(String path) //只取a里出path外的配置

大家可以参考一下,conifg 的api 描述

    /**
     * Obtains the default reference configuration, which is currently created
     * by merging all resources "reference.conf" found on the classpath and
     * overriding the result with system properties. The returned reference
     * configuration will already have substitutions resolved.
     * 
     * 

* Libraries and frameworks should ship with a "reference.conf" in their * jar. * *

* The reference config must be looked up in the class loader that contains * the libraries that you want to use with this config, so the * "reference.conf" for each library can be found. Use * {@link #defaultReference(ClassLoader)} if the context class loader is not * suitable. * *

* The {@link #load()} methods merge this configuration for you * automatically. * *

* Future versions may look for reference configuration in more places. It * is not guaranteed that this method only looks at * "reference.conf". * * @return the default reference config for context class loader */ public static Config defaultReference() { return defaultReference(checkedContextClassLoader("defaultReference")); }

你可能感兴趣的:(scala)