概述
akka 使用 Typesafe Config Library来实现Configration, 同样地可以在自己的应用使用这个jar. maven地址
<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的文件作为配置
读取其他文件名配置
app2.conf
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.json
{
"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"));