import java.util.ResourceBundle;
public class ConfigHelper {
private static ConfigHelper instance;
private ConfigHelper() {
}
public static ConfigHelper getInstance() {
if (instance == null) {
synchronized (ConfigHelper.class) {
if (instance == null) {
instance = new ConfigHelper();
}
}
}
return instance;
}
public ResourceBundle getConfigResource(String propertyName){
ResourceBundle rb = ResourceBundle.getBundle(propertyName.trim());
return rb;
}
}
XXX.properties配置文件可以放在resource目錄下。
XXX.properties配置文件內容為:屬性名=屬性值
如config.properties:
#CPU權重
cpuWeight=0.5
#內存權重
memoryWeight=0.3
#磁盤權重
diskWeight=0.2
讀取某個配置文件中的某個屬性值:
String cpuWeight = ConfigHelper.getInstance().getConfigResource("config").getString("cpuWeight")