properties屬性文件工具類,properties工具類
package xxx.business.utils;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by windwant on 2016/7/19.
*/
public class ConfigUtils {
private static final Logger logger = LoggerFactory.getLogger(ConfigUtils.class);
private static PropertiesConfiguration config = null;
static {
try {
if (config == null) {
config = new PropertiesConfiguration("config.properties");
//自動重新加載
config.setReloadingStrategy(new FileChangedReloadingStrategy());
//自動保存
config.setAutoSave(true);
}
} catch (ConfigurationException e) {
logger.error("load config.properties error", e);
throw new RuntimeException(e);
}
}
private ConfigUtils(){}
public static String get(String key) {
if (config != null) {
String value = config.getString(key);
return value == null ? "" : value;
}
return "";
}
public static Integer getInteger(String key) {
int result = -1;
if (config != null) {
try {
result = Integer.parseInt(config.getString(key));
} catch (NumberFormatException e) {
result = -1;
}
}
return result;
}
}