config格式
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"));
}
}
typesafe下的config包可以用来读取配置文件,支持多种形式。
但是,若使用
ConfigFactory.load()
加载配置文件,只能加载src/main/resources目录下的application.conf文件,不够灵活。
通过研究发现,可以使用
val c = ConfigFactory.parseFile(new File("*****.conf"))
加载任意位置的配置文件