跳到主要内容

配置文件管理

返回文档导航


使用 JSONMap 处理嵌套配置的读取和合并。

基本用法

JSONMap config = new JSONMap(readFile("config.json"));

int port = config.getInt("server.port", 8080);
String host = config.getStr("server.host", "localhost");
List<String> whitelist = config.getList("security.whitelist", String.class);

多环境配置合并

JSONMap baseConfig = new JSONMap(readFile("config-base.json"));

String env = System.getProperty("env", "dev");
JSONMap envConfig = new JSONMap(readFile("config-" + env + ".json"));

// 环境配置覆盖基础配置
baseConfig.putAll(envConfig);
int port = baseConfig.getInt("server.port");

动态配置

JSONMap config = new JSONMap(configCenter.getConfig("app"));

configCenter.addListener(newConfig -> {
config.clear();
config.putAll(new JSONMap(newConfig));
reloadApp(config);
});

上一节:表单数据处理 | 返回文档导航 | 下一节:数据库JSON字段 →