有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我的配置文件存在问题,它位于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\

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

Properties location [${ext.properties.dir:classpath:}/properties.properties] not resolvable: class path resource [properties.properties] cannot be opened because it does not exist

如何修复此错误


共 (2) 个答案

  1. # 1 楼答案

    Spring Boot在类路径上查找application.properties。您不需要显式的@PropertySource注释。而且@PropertySource没有能力解析路径中的SPEL,从您收到的错误消息中可以明显看出这一点

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

  2. # 2 楼答案

    我看到您正在使用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