【问题标题】:how to load spring-boot based REST application properties from a different file?如何从不同的文件加载基于 spring-boot 的 REST 应用程序属性?
【发布时间】:2015-01-30 10:55:10
【问题描述】:

似乎 spring-boot 启动器在以下位置查找 application.{xml|properties|yml|yaml} 文件以加载属性。有没有办法指定不同的文件名或文件来加载属性?

$ java -Ddebug -jar target/app-1.0.jar
16 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./config/application.properties' resource not found
16 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./config/application.yaml' resource not found
16 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./config/application.yml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/application.properties' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/application.xml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/application.yaml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/application.yml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/config/application.properties' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/config/application.xml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/config/application.yaml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/config/application.yml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./application.properties' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./application.xml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./application.yaml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./application.yml' resource not found
6 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./config/application.xml' resource not found

编辑

有没有办法指定另一个 Bean 配置文件(如 application-context.xml)?

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    您需要使用@ConfigurationProperties 来加载属性。

    @ConfigurationProperties(locations = {"yourProperties.xml"})
    

    【讨论】:

      【解决方案2】:

      可以设置springs启动时搜索配置文件的文件夹。

      java -jar target/app-1.0.jar -Dspring.config.location=your/config/dir/

      【讨论】:

      • 注意 - 要使用非标准文件名,需要与 spring.config.name 结合使用。
      【解决方案3】:

      在命令行中,您可以使用以下属性来提及额外的启动配置文件:

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

      另一种选择是:

      -Dspring.config.name="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
        

      following Docs resource 中有关 Spring Boot 配置的更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-25
        • 2016-07-17
        • 2019-03-16
        • 1970-01-01
        • 2021-06-05
        • 2020-05-26
        • 2017-06-17
        • 2017-11-17
        相关资源
        最近更新 更多