配置文件位置
新建如下加载配置文件的工具类,在其他需要用的配置信息的地方调用工具类方法获取配置参数即可
public class LoadPropertiesUtil { private static final Logger logger = Logger.getLogger(LoadPropertiesUtil.class); /** * 加载文件 * @param fileName 为项目根路径下路径 * @return */ public static Properties loadProperties(String fileName) { Properties properties = new Properties(); InputStream is = null; try { String path = Thread.currentThread().getContextClassLoader().getResource("").getPath(); is = new FileInputStream(path + fileName); properties.load(is); } catch (IOException e) { logger.error(e.getMessage(), e); }finally { if(is != null){ try { is.close(); } catch (IOException e) { logger.error(e.getMessage(), e); } } } return properties; } public static String getKey(String fileName, String key){ String result = null; try { Properties properties = loadProperties(fileName); result = (String) properties.get(key); } catch (Exception e) { logger.info("加载配置文件" + fileName + "失败"); logger.error(e.getMessage(), e); } return result; } }
项目中使用