Akka系列之Configration

概述

akka 使用 Typesafe Config Library來實現Configration, 一樣地能夠在本身的應用使用這個jar. maven地址git

<dependency>
    <groupId>com.typesafe</groupId>
    <artifactId>config</artifactId>
    <version>1.3.1</version>
</dependency>

示例

在resources添加配置文件application.conf

simple-app {
	answer = 42
}

simple-lib.foo = "this is a test"
simple-lib.whatever = "i'm whatever"

代碼

import com.typesafe.config.ConfigFactory

Config conf = ConfigFactory.load();
System.out.println("The foo is :" + conf.getString("simple-lib.foo"));

注: ConfigFactory默認會去尋找application.xx的文件做爲配置github

讀取其餘文件名配置

app2.confjson

simple-lib.con = "Yes!"

simple-lib {
	how = 77
}
// 文件名省略後輟
Config conf1 = ConfigFactory.load("app2");
System.out.println("The con is :" + conf1.getString("simple-lib.con"));
System.out.println("how is : " + conf1.getInt("simple-lib.how"));

支持json格式

app3.jsonapp

{
"root" : "root~~",
"name" : 22
}
Config conf2 = ConfigFactory.load("app3");
System.out.println("read json file. root:" + conf2.getString("root"));
System.out.println("read json file. name:" + conf2.getString("name"));
相關文章
相關標籤/搜索