小弟有個練手的maven專案,檔案結構如圖,DBPoolConnection需要讀取properties,路徑總寫不對,

static {
Properties properties = loadPropertiesFile(path);
try {
druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties); //DruidDataSrouce工廠模式
} catch (Exception e) {
log.error("獲取配置失敗");
}
}
private static Properties loadPropertiesFile(String fullFile) {
String webRootPath = null;
if (null == fullFile || fullFile.equals("")){
throw new IllegalArgumentException("Properties file path can not be null" + fullFile);
}
webRootPath = DBPoolConnection.class.getClassLoader().getResource("").getPath();
webRootPath = new File(webRootPath).getParent();
InputStream inputStream = null;
Properties p =null;
try {
inputStream = new FileInputStream(new File(webRootPath + File.separator + fullFile));
p = new Properties();
p.load(inputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != inputStream){
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return p;
}
uj5u.com熱心網友回復:
DBPoolConnection.class.getClassLoader().getResource("/"+fullFile)uj5u.com熱心網友回復:
那我第二行的 path 應該怎么寫呢?uj5u.com熱心網友回復:
我想改成相對路徑讀取properties,這樣打成jar之后運行也方便轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/254599.html
標籤:Java SE
上一篇:cas協議5.3x,我把登錄頁放在前端,使用了restful登錄,然后怎么集成oauth服務端呀
下一篇:ncc
