Akka Config Lib的简单使用

Akka Config Lib的简单使用


akka框架中使用该库配置akka,该项目地址为https://github.com/typesafehub/config


首先在classpath下定义文件为:

complex1.conf

complex-app {
  something = "hello world"

  # here we want a simple-lib-context unique to our app
  # which can be custom-configured. In code, we have to
  # pull out this subtree and pass it to simple-lib.
  simple-lib-context = {
    simple-lib {
      foo = "hello world complex1 foo"
      whatever = "hello world complex1 whatever"
    }
  }
}

使用如下方式解析这个文件,

package com.usoft6;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.usoft3.SystemSettingDemo;


/**
 * Created by liyanxin on 2015/1/13.
 */
public class TypesafeConfigDemo {

    public static void main(String args[]) {
        // "config1" is just an example of using a file other than
        // application.conf
        // config1是在classpath路径下的配置文件名,该文件类型是conf文件类型
        Config config1 = ConfigFactory.load("complex1");

        // use the config ourselves
        System.out.println(config1.getString("complex-app.something"));
        System.out.println(config1.getString("complex-app.simple-lib-context.simple-lib.foo"));
        System.out.println(config1.getString("complex-app.simple-lib-context.simple-lib.whatever"));


        // ConfigFactory.parseString使用parseString直接解析
        Config config2 = ConfigFactory.parseString("akka.loggers = \"akka.testkit.TestEventListener\"");
        System.out.println(config2.getString("akka.loggers"));


        // 使用parseString 直接解析json字符串
        Config config3 = ConfigFactory.parseString("{\"a\":\"b\", \"c\":\"d\"}");
        System.out.println(config3.getString("a"));
        System.out.println(config3.getString("c"));
    }
}


如上,还可以在定义json字符串,直接用typesafe config lib 解析为config对象。


withFallback合并两个配置树

如下,在classpath下定义两个配置文件,

complex1.conf

complex-app {
  something = "hello world"

  # here we want a simple-lib-context unique to our app
  # which can be custom-configured. In code, we have to
  # pull out this subtree and pass it to simple-lib.
  simple-lib-context = {
    simple-lib {
      foo = "hello world complex1 foo"
      whatever = "hello world complex1 whatever"
    }
  }
}

complex2.conf

complex-app {
  something = "hello world typesafe conf"
}

simple-lib.foo = "hello world foo"
simple-lib.whatever = "hello world whatever"

如何合并这两个配置文件,如下,

package com.usoft6;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

/**
 * Created by liyanxin on 2015/1/13.
 */
public class WithFallBackDemo {

    public static void main(String args[]) {

        Config firstConfig = ConfigFactory.load("complex1.conf");
        Config secondConfig = ConfigFactory.load("complex2.conf");
        //a.withFallback(b)  a和b合并,如果有相同的key,以a为准
        Config finalConfig = firstConfig.withFallback(secondConfig);
        System.out.println(finalConfig.getString("complex-app.something")); //hello world
        System.out.println("simple-lib.foo"); //这个配置项是在complex2.conf中的
    }
}

运行结果,

hello world

simple-lib.foo


withOnlyPath取指定Path下的配置项

同上定义的两个conf配置文件,如下代码示例,

package com.usoft6;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

/**
 * Created by liyanxin on 2015/1/13.
 */
public class WithOnlyPathDemo {

    public static void main(String args[]) {

        Config firstConfig = ConfigFactory.load("complex1.conf");
        Config secondConfig = ConfigFactory.load("complex2.conf");
        //a.withFallback(b)  a和b合并,如果有相同的key,以a为准
        Config finalConfig = firstConfig.withOnlyPath("complex-app.simple-lib-context").withFallback(secondConfig);
        System.out.println(finalConfig.getString("complex-app.something")); //hello world
        System.out.println("simple-lib.foo"); //这个配置项是在complex2.conf中的
    }
}

运行结果,

hello world typesafe conf

simple-lib.foo

其他api请参照文档

==============END==============

转载于:https://my.oschina.net/xinxingegeya/blog/367328

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值