【问题标题】:Spring Boot case sensitive resolving properties from program argumentsSpring Boot区分大小写的程序参数解析属性
【发布时间】:2021-11-10 01:05:37
【问题描述】:

我正在尝试在我的本地机器上运行 Spring Boot 应用程序。当我对属性使用以下语法时,它可以很好地解析:

username: ${project.db.user}

但以下引发异常:

username: ${PROJECT_DB_USER}

有什么方法可以在这里使用大写吗?我的 CI/CD 管道用大写定义了它的所有变量

【问题讨论】:

  • 我不知道答案,但你试过username: ${PROJECT.DB.USER}吗?

标签: java spring-boot command-line-arguments


【解决方案1】:

我也遇到过类似的问题,据我了解,自 springBoot v2.1 以来,application.properties 文件不能很好地处理环境变量。解决方案是使用 Spring Profiles。 您需要添加与环境一样多的 .properties 文件,如下所示:

application.properties
application-staging.properties
application-prod.properties

如果您有 staging 和 prod 配置文件。配置文件的 .properties 会覆盖基本的 application.properties,因此请确保不要忘记每个文件中的任何内容!

可能还有其他解决方案。

您可以使用各种方法设置配置文件,在 Spring boot 中,使用 Maven(对不起,我唯一熟悉的)您可以像这样简单地将其添加到 pom.xml 中:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <profiles>
                <profile>staging</profile>
            </profiles>
        </configuration>
    </plugin>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 2018-12-18
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多