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.
     * 
     * <p>
     * Libraries and frameworks should ship with a "reference.conf" in their
     * jar.
     * 
     * <p>
     * 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.
     * 
     * <p>
     * The {@link #load()} methods merge this configuration for you
     * automatically.
     * 
     * <p>
     * Future versions may look for reference configuration in more places. It
     * is not guaranteed that this method <em>only</em> looks at
     * "reference.conf".
     * 
     * @return the default reference config for context class loader
     */
    public static Config defaultReference() {
        return defaultReference(checkedContextClassLoader("defaultReference"));
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值