自定义的配置文件放在 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