自定義的配置文件放在 resources 文件夾下,用 org.springframework.core.io.UrlResource 讀取,路徑用 classpath: 方式。普通 spring 項目沒問題,spring boot 打 war 包也能夠,但啓動 Application 時就拋異常了:java.net.MalformedURLException: unknown protocol: classpath。目測這種啓動方式,不支持 classpath: 這種資源定位,因而找了新的方式代替:org.springframework.util.ResourceUtils。java
舊代碼:spring
UrlResource urlResource = new UrlResource("classpath:xxx/xxx.properties"); Properties prop = new Properties(); prop.load(urlResource.getInputStream()); //省略獲取prop屬性部分
新代碼:url
File file = ResourceUtils.getFile("classpath:xxx/xxx.properties"); Properties prop = new Properties(); InputStream is = new FileInputStream(file); //省略獲取prop屬性部分 prop.load(is); is.close();
完美解決spa