【发布时间】: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