【发布时间】:2016-06-04 07:19:49
【问题描述】:
我正在使用Apache Commons Configuration 库和PropertiesConfiguration。
我的应用程序在启动后立即加载配置文件,如下所示:
public PropertiesConfiguration loadConfigFile(File configFile) throws ConfigurationNotFoundException {
try {
if (configFile != null && configFile.exists()) {
config.load(configFile);
config.setListDelimiter(';');
config.setAutoSave(true);
config.setReloadingStrategy(new FileChangedReloadingStrategy());
setConfigLoaded(true);
}
else {
throw new ConfigurationNotFoundException("Configuration file not found.");
}
} catch (ConfigurationException e) {
logger.warn(e.getMessage());
setDefaultConfigValues(config);
config.setFile(configFile);
}
return config;
}
我的问题是,如何验证configFile,所以我可以确定该文件中没有任何属性丢失,稍后在我的代码中尝试访问属性时我不会得到NullPointerException,例如:
PropertiesConfiguration config = loadConfig(configFile);
String rootDir = config.getString("paths.download"); // I want to be sure that this property exists right at the app start
我在文档或谷歌中没有找到任何东西,只是关于 XML 验证的内容。
目标是在程序启动时向用户提供配置文件已损坏的反馈。
属性文件没有内置机制?
【问题讨论】:
-
在典型的生产环境中,大多数人使用 puppet 或 chef 来确保所有服务器都符合严格的配置——所有配置文件,而不仅仅是特定于应用程序。
标签: java validation properties-file apache-commons-config