Typesafe的Config库,纯Java写成、零外部依赖、代码精简、功能灵活、API友好。支持Java properties、JSON、JSON超集格式HOCON以及环境变量。它也是Akka的配置管理库。
特性:
- 纯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)
目前config只支持配置文件,如果想从数据库获取配置文件,需要自己定义。 config库很擅长合并配置。
Example
默认加载classpath下的application.conf,application.json和application.properties文件。通过ConfigFactory.load()加载。
application.conf:
conf.foo="This value comes from simple-app's application.conf"
conf.whatever = "This value comes from simple-app's application.conf"
application.properties:
properties.foo="This value comes from simple-app's application.properties"
properties.whatever = "This value comes from simple-app's application.properties"
application.json:
{
"id":1,
"label":"person",
"outE":{
"uses":[
{
"id":16,
"inV":11,
"properties":{
"skill":5
}
},
{
"id":15,
"inV":10,
"properties":{
"skill":4
}
}
],
"develops":[
{
"id":13,
"inV":10,
"properties":{
"since":2009
}
},
{
"id":14,
"inV":11,
"properties":{
"since":2010
}
}
]
},
"properties":{
"name":[
{
"id":0,
"value":"marko"
}
],
"location":[
{
"id":6,
"value":"san diego",
"properties":{
"startTime":1997,
"endTime":2001
}
},
{
"id":7,
"value":"santa cruz",
"properties":{
"startTime":2001,
"endTime":2004
}
},
{
"id":8,
"value":"brussels",
"properties":{
"startTime":2004,
"endTime":2005
}
},
{
"id":9,
"value":"santa fe",
"properties":{
"startTime":2005
}
}
]
}
}
public class SimpleTypesafeConfig {
private Config config;
public SimpleTypesafeConfig(Config config) {
this.config = config;
config.checkValid(ConfigFactory.defaultReference(), "conf");
}
public SimpleTypesafeConfig() {
this(ConfigFactory.load());
}
public void printSetting(String path) {
System.out.println("The setting '" + path + "' is: " + config.getString(path));
}
public static void main(String[] args) {
SimpleTypesafeConfig s = new SimpleTypesafeConfig();
s.printSetting("properties.whatever");//application.properties
//output: The setting 'properties.whatever' is: "This value comes from simple-app's application.properties"
s.printSetting("conf.whatever");//application.conf
//output:The setting 'conf.whatever' is: This value comes from simple-app's application.conf
s.printSetting("label");//application.conf
//output: person
}
}
其中核心加载代码:
config.checkValid(ConfigFactory.defaultReference(), "conf");
底层调用:
private static ClassLoader checkedContextClassLoader(String methodName) {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if(loader == null) {
throw new BugOrBroken("Context class loader is not set for the current thread; if Thread.currentThread().getContextClassLoader() returns null, you must pass a ClassLoader explicitly to ConfigFactory." + methodName);
} else {
return loader;
}
}