typesafe的配置库工具类Config试用

2017-11-15 11:38:27来源:http://zl1030.blog.51cto.com/274507/1980846作者:人点击

分享

importcom.typesafe.config.Config;
importcom.typesafe.config.ConfigFactory;
importcom.typesafe.config.ConfigValue;
importorg.junit.Test;

importjava.util.Map;

/**
*@authorbyleizhouon2017/11/1015:10.
*/
publicclassConfigTest{
@Test
publicvoidtest(){

Configconf=ConfigFactory.load();

for(Configconfig:conf.getConfigList("battleserver")){
System.out.println(config.getInt("id"));
System.out.println(config.getString("ip"));
System.out.println(config.getInt("port"));
}

System.out.println(conf.getString("login.db.driver"));
System.out.println(conf.getString("login.db.url"));
System.out.println(conf.getString("login.db.activeConn"));

for(Map.Entry<String,ConfigValue>entry:conf.getConfig("res.android").entrySet()){
System.out.println(entry.getKey()+""+entry.getValue().valueType()+""+entry.getValue().unwrapped());

}

for(Map.Entry<String,ConfigValue>entry:conf.getConfig("whitelist.member").entrySet()){
System.out.println(entry.getKey()+""+entry.getValue().valueType()+""+entry.getValue().unwrapped());

}

System.out.println(conf.getString("company_ip"));

System.out.println(conf.getString("lobbyserver.ip"));
System.out.println(conf.getString("lobbyserver.port"));
System.out.println(conf.getString("lobbyserver.rpc"));

}
}

Maven依赖:

<dependencies>
<!--https://mvnrepository.com/artifact/com.typesafe/config-->
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>

配置文件:

application.conf

globalIP=127.0.0.1

include"logindb"

include"battleserver"

include"common"

logindb.conf

#账号服数据库
login.db.driver=com.mysql.jdbc.Driver
login.db.url="jdbc:mysql://"${globalIP}":3306/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8"
login.db.username=root
login.db.password=test
login.db.activeConn=100

battleserver.conf

#战斗服信息
battleserver=
[
{
id=1
ip=${globalIP}
port=9010
rpc=9011
}

{
id=2
ip=${globalIP}
port=9010
rpc=9011
}

{
id=3
ip=${globalIP}
port=9010
rpc=9011
}
]

common.conf

company_ip=${globalIP}

lan_res_url="http://"${globalIP}":80/Resource/AssetBundle/"
wan_res_url="http://"${globalIP}":18649/Resource/AssetBundle/"

lobbyserver={
ip=${globalIP}
port=9900
rpc=9901
}

res{
android{
12250:750
13363:906
14774:961
14820:961
14824:961
}

ios{
12250:719
12275:727
13363:876
14774:930
14820:930
14824:930
}

pc{
111:1120
}
}

whitelist={

active=true

member{
test1:A40FE952-6DFB-489D-B5F3-846D7B570754
test2:04ed7c30a5c5686a2d7c3ec67a34f46c
test3:118FC61E-18FC-4DCA-B28E-A9AAF44430DA
}
}

输出结果:

1
127.0.0.1
9010
2
127.0.0.1
9010
3
127.0.0.1
9010
com.mysql.jdbc.Driver
jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
100
14820NUMBER961
14774NUMBER961
12250NUMBER750
14824NUMBER961
13363NUMBER906
test2STRING04ed7c30a5c5686a2d7c3ec67a34f46c
test3STRING118FC61E-18FC-4DCA-B28E-A9AAF44430DA
test1STRINGA40FE952-6DFB-489D-B5F3-846D7B570754
127.0.0.1
127.0.0.1
9900
9901

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台