【问题标题】:@PropertySource cannot be opened because it does not exist@PropertySource 无法打开,因为它不存在
【发布时间】:2017-07-08 13:56:28
【问题描述】:

我的 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],因为它 不存在

我该如何解决这个错误?

【问题讨论】:

  • 这可能会对你有所帮助 - stackoverflow.com/questions/33184807/…
  • @Arpit,我做了与答案相同的配置,但我得到了cannot be opened because it does not exist
  • 您使用的是spring mvc 还是spring boot
  • 我正在使用spring boot

标签: java spring


【解决方案1】:

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

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

最后的指令是:

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

【讨论】:

  • spring.config.location 不会帮他处理@PropertySource
  • 您是遇到同样的错误还是不同?你检查了@AbhijitSarkar 的建议吗?
  • @JUANCALVOPINAM,谢谢,--spring.config.location 效果很好
【解决方案2】:

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

使用src/main/resources/application.properties。当您想使用外部文件时,请使用 @juan-calvopina-m 在他的回答中建议的 spring.config.location

【讨论】:

  • @Alex78191 你的评论是相关的,到底如何?
猜你喜欢
  • 2019-07-12
  • 1970-01-01
  • 2011-04-23
  • 2016-09-08
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 2012-02-16
  • 1970-01-01
相关资源
最近更新 更多