【问题标题】:SpringBoot stores configs in BOOTSpringBoot 在 BOOT 中存储配置
【发布时间】:2018-10-20 04:43:24
【问题描述】:

我有一个 spring boot 应用程序作为 myapp,我有一些需要外部化的配置。它们最初位于main/resource/config下的一个分发项目中

当我运行 maven 构建时,tar.gz 有 config 文件夹以及 myapp.jar。 这个 myapp.jar 有一个 BOOT 文件夹,其中还包含 jar 之外的 config 文件夹中的所有文件。现在,当我将配置文件夹更改为外部时。 Springboot 仍然从 jar 内的 BOOT 文件夹中读取配置文件。如何读取外部配置文件。

【问题讨论】:

    标签: java spring-boot packaging


    【解决方案1】:

    因为你的外部配置文件不在classpath中,所以springboot不会知道这些文件,它会从classpath中搜索文件。

    这是externalized config 的文档。如果使用外部配置,则需要在运行springboot时设置spring.config.location,如

    java -Dspring.config.location=file:/gfs/config/post/post.properties,/gfs/config/post/post_pwd.properties,./config/db-config.properties,./config/db-passwd.properties -jar myapp-1.0-SNAPSHOT.jar

    【讨论】:

    • 有没有办法不将它们添加到 BOOT 中而不在命令中添加 Dspring.config.location=()
    • @sarmahdi,或者您需要自己在代码中读取文件。这比设置spring.config.location
    【解决方案2】:

    sarmahdi - 你可以试试这个...

    java -Dconfig_base_dir=/opt/tomcat/conf/bpm -jar myapp-1.0-SNAPSHOT.jar

    @Configuration
        @PropertySources({
            @PropertySource("file:${config_base_dir}/app.properties"),
            @PropertySource("file:${config_base_dir}/dbconfig.properties")
     })
    public class PostConfig {
    

    更多示例... Mkyong java

    【讨论】:

      【解决方案3】:

      在我的情况下,我只是在 application.yaml 中使用了 file:config/myconfig.properties,所以它没有在 jar 中查找,而是在其中查找文件夹 config。如果我使用 classpath:/config/myconfig.properties 那么它将采用 jar 中捆绑的内容

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-26
        • 2016-02-22
        • 2021-07-26
        • 2016-12-08
        • 1970-01-01
        • 1970-01-01
        • 2013-09-04
        • 2017-07-12
        相关资源
        最近更新 更多