用 類名.class.getClassLoader().getResourceAsStream來獲得config目錄下的properties配置文件


public final class ConfigUtil {

private static Logger log = LoggerFactory.getLogger(ConfigUtil.class);

/**
* 屬性對象
*/
private static Properties props;

/** 私有的構造函數 */
public ConfigUtil() {

props = null;
}

/**
* 根據屬性key得到對應的屬性值
*
* @param key
* 鍵名稱
* @return
*/
public static String getPropsValueByKey(String filePath, String key) {
// if (props == null) {
props = new Properties();
InputStream resourceStream = ConfigUtil.class.getClassLoader().getResourceAsStream(filePath);
try {
props.load(resourceStream);
} catch (IOException e) {
String error = "配置文件工具類-根據屬性key得到對應的屬性值方法出現異常!";
log.error(error, e);
}
// }
return props.getProperty(key);
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM