Tomcat 中 db.properties 無法找到
阿新 • • 發佈:2020-07-28
java專案中可以找到db.properties檔案,但使用Tomcat的web專案中卻無法找到檔案。
可以嘗試以下解決方案。
- 確保web專案中目錄WEB-INF/classes下生成了db.properties 檔案。
pom.xml 檔案中新增如下程式碼,確保資源匯入成功
<!-- 在build中匯入resources ,防止資源匯出失敗問題--> <build> <resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>**/*.properties</exclude> <exclude>**/*.xml</exclude> </excludes> <filtering>false</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
- 使用類載入器獲取檔案流的方式載入檔案,並確保路徑為
"/db.properties"
, 資源路徑要新增上。
InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("/db.properties");
properties.load(is);