【问题标题】:How to externalize configuration in Spring Boot using profiles?如何使用配置文件在 Spring Boot 中外部化配置?
【发布时间】:2016-08-19 09:36:02
【问题描述】:

我有一个应用程序,我想更改存储在 application.yml 文件中的数据源密码。 YML文件中的密码是这样存储的:

----
spring:
    profiles: production
datasource:
    password: prodpassword

注意:我也有关于发展和阶段的资料。

密码属性是使用ConfigurationProperties在类上设置的,如下所示:

@Component
@ConfigurationProperties(prefix="datasource")
public class DataSourceConnector {

    private password;

    public void setPassword(String password) {
        this.password = password;
    }

现在,我尝试通过命令行 arg 用 prodpa$$word 覆盖 prodpassword,但它不起作用:

java -Dspring.profiles.active=production -jar /usr/share/myapp/myapp-1.0.jar --datasource.password='prodpa$$word'

我还尝试在 jar 之外创建一个相同的(新密码除外)application.yml 文件。这也不行。

java -Dspring.profiles.active=production -jar /usr/share/myapp/myapp-1.0.jar --spring.config.location=/usr/share/myapp/

注意:由于http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-profile-specific-properties 的注释,我在位置参数中省略了文件名:

如果您在 spring.config.location 中指定了任何文件,则不会考虑这些文件的特定于配置文件的变体。如果您还想使用特定于配置文件的属性,请使用spring.config.location 中的目录。

如何在 jar 的 application.yml 中覆盖 datasource.password?

编辑: 应用程序正在使用supervisorctl 启动/停止。

【问题讨论】:

  • 你试过-Dspring.config.location=/usr/share/myapp/吗?
  • 你可以在这里查看我对类似问题的回答:stackoverflow.com/questions/36635163/…
  • 谢谢。我试过了,但没有用。事实证明,这个问题与 Spring 无关。这是由于我运行应用程序的方式。应用程序运行程序(supervisorctl)正在缓存我的配置文件(在应用程序启动和停止之间)。抱歉,我没有将其包含在我的 OP 中。

标签: spring properties configuration spring-boot supervisord


【解决方案1】:

更改包含java 命令的配置文件后,supervisorctl 必须重新读取更改:

supervisorctl reread

接下来,激活更改:

supervisorctl update

【讨论】:

    猜你喜欢
    • 2015-10-27
    • 2019-08-17
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 2021-01-05
    • 2019-12-16
    • 2017-11-24
    相关资源
    最近更新 更多