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只支持配置文件,如果想从数据库获取配置文件,需要自己DIV
。config库很擅长合并配置。
例子
引入maven
<!-- 配置管理 -->
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.2.1</version>
</dependency>
application.conf
# these are our own config values defined by the app
simple-app {
answer=42
}
# Here we override some values used by a library
simple-lib.foo="This value comes from simple-app's application.conf"
simple-lib.whatever = "This value comes from simple-app's application.conf"
默认加载classpath下的application.conf
,application.json
和application.properties
文件。通过ConfigFactory.load()
加载。
CommonConfig类
package org.dreams.transaction.config;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
public class CommonConfig {
private Config config;
//指定配置文件
public CommonConfig(Config config) {
this.config = config;
config.checkValid(ConfigFactory.defaultReference(), "simple-lib");
}
// 默认加载classpath下的application.*
public CommonConfig() {
this(ConfigFactory.load());
}
//打印
public void printSetting(String path) {
System.out.println("The setting '" + path + "' is: " + config.getString(path));
}
public static void main(String[] args) {
CommonConfig s = new CommonConfig();
s.printSetting("simple-app.answer");
}
}
ConfigFactory.load()
会加载配置文件,默认加载classpath下的application.conf
,application.json
和application.properties
文件。
也可以调用ConfigFactory.load(confFileName)
加载指定的配置文件。
配置内容即可以是层级关系,也可以用”.”号分隔写成一行:
host{
ip = 127.0.0.1
port = 2282
}
或则
host.ip = 127.0.0.1
host.port = 2282
即json格式和properties格式。(貌似.json只能是json格式,.properties只能是properties格式,而*.conf可以是两者混合,而且配置文件只能是以上三种后缀名)
冲突
如果多个config 文件有冲突时,解决方案有:
a.withFallback(b)
//a和b合并,如果有相同的key,以a为准a.withOnlyPath(String path)
//只取a里的path下的配置a.withoutPath(String path)
//只取a里除path外的配置
例如:
Config firstConfig = ConfigFactory.load("test1.conf");
Config secondConfig = ConfigFactory.load("test2.conf");
//a.withFallback(b) a和b合并,如果有相同的key,以a为准
Config finalConfig = firstConfig.withOnlyPath("host").withFallback(secondConfig);
finalConfig中的配置是test1.conf中的host节点的所有配置与test2.conf所有节点的配置合集。
来个例子
package org.dreams.transaction.config;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
public class CommonConfig {
private Config config;
//指定配置文件
public CommonConfig(Config config) {
this.config = config;
}
// 默认加载classpath下的application.*
public CommonConfig() {
this(ConfigFactory.load());
}
//打印
public void printSetting(String path) {
System.out.println("The setting '" + path + "' is: " + config.getString(path));
}
public static void main(String[] args) {
Config javaSystemProperties = ConfigFactory.load("no-such-resource-only-system-props");
CommonConfig s = new CommonConfig(javaSystemProperties);
Config appConfig = ConfigFactory.parseResources("application.conf");
Config testConfig = ConfigFactory.parseResources("test.conf");
s.config = javaSystemProperties
.withFallback(testConfig)
.withFallback(appConfig)
.resolve(); // substitute variables in config if any
s.printSetting("simple-app.answer");
}
}
增加test.conf
simple-app {
answer=422
}
simple-lib.foo="This value comes from simple-app's test.conf"
simple-lib.whatever = "This value comes from simple-app's test.conf"
application.conf
simple-app {
answer=42
}
simple-lib.foo="This value comes from simple-app's application.conf"
simple-lib.whatever = "This value comes from simple-app's application.conf"
输出结果
The setting 'simple-app.answer' is: 422