2017-07-08 111 views
1

我有配置文件位于除我的jar文件以外的其他目录中的问题。@PropertySource无法打开,因为它不存在

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

我该如何解决这个错误?

+0

这可能会帮助你 - https://stackoverflow.com/questions/33184807/override-properties-file-in-spring-webapp-at-runtime – Arpit

+0

@Arpit,我做了与答案中相同的配置,但我得到了'不能打开,因为它不存在' –

+0

你使用的是'spring mvc'还是'spring boot'? – elmigue017

回答

1

我看到你正在使用应用,根据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 
+0

'spring.config.location'不会帮助他使用'@ PropertySource' –

+0

您是否得到相同的错误或者是否有所不同?你检查@ AbhijitSarkar的建议吗? –

+0

@JUANCALVOPINAM,谢谢,'--spring.config.location'很好用 –

1

春季启动查找在类路径上。您不需要明确的@PropertySource注释。并且@PropertySource不具备解析路径中的SPEL的功能,这应该从您收到的错误消息中看出。使用src/main/resources/application.properties。当你想使用外部文件时,请使用spring.config.location,比如他的答案中建议的@ juan-calvopina-m。

相关问题