import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigValue;
import org.junit.Test;

import java.util.Map;

/**
 * @author by lei zhou on 2017/11/10 15:10.
 */
public class ConfigTest {
    @Test
    public void test() {

        Config conf = ConfigFactory.load();

        for (Config config : 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 entry : conf.getConfig("res.android").entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue().valueType() + " " + entry.getValue().unwrapped());

        }

        for (Map.Entry 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依赖:


        
        
            com.typesafe
            config
            1.3.2
        
    


配置文件:

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
14820 NUMBER 961
14774 NUMBER 961
12250 NUMBER 750
14824 NUMBER 961
13363 NUMBER 906
test2 STRING 04ed7c30a5c5686a2d7c3ec67a34f46c
test3 STRING 118FC61E-18FC-4DCA-B28E-A9AAF44430DA
test1 STRING A40FE952-6DFB-489D-B5F3-846D7B570754
127.0.0.1
127.0.0.1
9900
9901