首页 文章

spring boot external config

提问于
浏览
8

我正在尝试将外部属性文件加载到我的spring启动应用程序中 . 最初我在配置类中使用了@PropertySource . 但现在我想删除这个注释,所以类不依赖于位置 . 所以我试着用:

java -jar my-boot-ws.war --SPRING_CONFIG_NAME=file:///Users/TMP/resources/

基于此http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html文档但我收到以下错误:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder

使用注释工作正常,但我真的想摆脱它 . 对此的任何帮助都会很棒

谢谢

更正*

抱歉复制粘贴错误上面的命令应该是:

java -jar my-boot-ws.war --spring.config.location=file:///Users/TMP/resources/

我不是试图更改配置文件的名称只是添加一个额外的位置 . 如下所述:

如果spring.config.location包含目录(而不是文件),则它们应该以/结尾(并且在加载之前将附加从spring.config.name生成的名称) .

我将此解释为说文件$ .properties将从命令行传入的--spring.config.location加载

4 回答

  • 20

    经过一些更多的googeling我发现Spring Boot and multiple external configuration files表明以下是正确的用法:

    java -jar my-boot-ws.war --spring.config.location=file:///Users/TMP/resources/myFile.properties
    

    我的印象是--spring.config.location会在指定的目录中加载其他属性文件 . 根据我提到的链接上的帖子,情况并非如此 . 如果指定了目录,则基于链接,然后搜索application.properties . 但是这里的文档http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html似乎暗示 Spring 季启动应用程序将首先查看类路径,如果可用,请抓取应用程序名称以获取基于该名称的其他属性文件 .

    但是,一旦我指定了文件名,一切正常,所以我猜我错了 .

  • 10

    在命令行中,您应该使用下面的属性来提及其他引导配置文件:

    --spring.config.location="file:/path/to/application.properties"
    

    另一种选择是:

    -Dspring.config.location="file:/path/to/application.properties"
    

    请注意,字符为小写,单词分隔符为句点 . .

    否则,您可以使用已使用的密钥的环境变量:

    • 在* nix系统中:
    export SPRING_CONFIG_NAME=file:/path/to/application.properties
    
    • 在Windows操作系统中:
    set SPRING_CONFIG_NAME=file:/path/to/application.properties
    
  • 0

    这可能不是一个常见的问题,但我遇到了它 . 即使用 --spring.config.name 替换它,你也必须在你的类路径中有一个 application.properties (由于敏感信息我在gitignore中有我的) .

  • 4

    1)Makesure args在run方法内部传递

    public class GemFireTestLoaderApplication {
    
    public static void main(String[] args) {
    
            SpringApplication.run(GemFireTestLoaderApplication.class, args);
            }
    }
    

    2)如果您已在xml注释中配置或先删除

    <!-- <context:property-placeholder location="classpath:config.properties" />  -->
    
    <!--   <context:property-placeholder location="file:/data/xxx/vaquarkhan/dataLoader/config.properties" /> -->
    

    以下命令可用于传递属性名称

    3.1)

    java -jar GemfireTest-0.0.1-SNAPSHOT.jar --spring.config.location=file:///C:/data/xxx/vaquarkhan/dataLoader/test/config.properties
    

    3.2)

    java -jar GemfireTest-0.0.1-SNAPSHOT.jar --spring.config.location=file:///C:/data/xxx/vaquarkhan/dataLoader/test/config.properties
    

    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

相关问题