@PropertySource无法打开,因为它不存在

时间:2017-07-08 13:56:28

标签: java spring

我遇到配置文件的问题,该文件位于我的jar文件以外的其他目录中。

我使用@PropertySource来加载属性。

@PropertySource(ignoreResourceNotFound = true, value = "${ext.properties.dir:classpath:}/properties.properties")

我尝试使用以下命令运行jar:

java -jar import-0.0.1-SNAPSHOT.jar -Dext.properties.dir=file:/C:\Users\Admin\Desktop\

日志中出现以下错误:属性位置

  

属性位置   [$ {ext.properties.dir:classpath:} / properties.properties]无法解析:   类路径资源[properties.properties]无法打开,因为它   不存在

如何解决此错误?

2 个答案:

答案 0 :(得分:1)

我看到您正在使用spring boot应用程序,根据spring documentation您可以尝试使用此环境属性:

--spring.config.location=file:/path/location/file-name.properties

最后的指示是:<​​/ p>

java -jar import-0.0.1-SNAPSHOT.jar --spring.config.location=file:C:\Users\Admin\Desktop\import.properties

答案 1 :(得分:0)

Spring Boot在类路径中查找application.properties。您不需要明确的@PropertySource注释。并且@PropertySource无法在路径中解析SPEL,这应该从您收到的错误消息中明显看出。

使用src/main/resources/application.properties。如果您想使用外部文件,请在答案中使用@ juan-calvopina-m之类的spring.config.location