什么是OWNER
OWNER 是一个开源项目,旨在解决Java配置文件的解析问题。
例如配置文件 /data/config.properties 包含下列内容:
server.port=80
server.hostname=foobar.com
server.max.threads=100
为了解析这个配置文件,我们首先定义一个Java接口:
import org.aeonbits.owner.Config;
@Sources({ //定义配置文件的路径
"file:/data/config.properties",
"classpath: config.properties"
})
public interface ServerConfig extends Config {
@Key("server.port")
int port();
@Key("server.hostname")
String hostname();
@Key("server.max.threads");
@DefaultValue("42")
int maxThreads();
}
加载配置文件:
ServerConfig cfg = ConfigFactory.create(ServerConfig.class);
System.out.println("Server " + cfg.hostname() + ":" + c