简体   繁体   English

如何使用配置文件外部化Spring Boot中的配置?

[英]How to externalize configuration in Spring Boot using profiles?

I have an application where I would like to change a datasource password that is stored in a application.yml file. 我有一个应用程序,我想更改存储在application.yml文件中的数据源密码。 The password in the YML file is stored such as this: YML文件中的密码存储如下:

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

Note: I also have profiles for development and stage. 注意:我也有用于开发和阶段的配置文件。

The password prop is set on a class using ConfigurationProperties such as follows: 使用ConfigurationProperties在类上设置密码prop,如下所示:

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

    private password;

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

Now, I try to override the prodpassword with prodpa$$word via a command line arg but it doesn't work: 现在,我尝试通过命令行arg用prodpa $$ word覆盖prodpassword,但是它不起作用:

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

I also tried creating an identical (except the new password) application.yml file outside of the jar. 我还尝试在jar之外创建一个相同的(新密码除外)application.yml文件。 That doesn't work either. 那也不行。

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

Note: I left out the file name in the location param due to this note from http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-profile-specific-properties : 注意:由于http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-profile--中的此注释,我在位置参数中省略了文件名具体属性

If you have specified any files in spring.config.location, profile-specific variants of those files will not be considered. 如果您在spring.config.location中指定了任何文件,则将不考虑这些文件的特定于配置文件的变体。 Use directories in spring.config.location if you also want to also use profile-specific properties. 如果您还想使用特定于配置文件的属性,请使用spring.config.location目录。

How can I override datasource.password within the application.yml of the jar? 如何在jar的application.yml中覆盖datasource.password?

Edit: The application is being started / stopped using supervisorctl . 编辑:正在使用supervisorctl启动/停止应用程序。

After changing the config file that contains the java command, supervisorctl must reread the change: 更改包含java命令的配置文件后, supervisorctl必须重新读取更改:

supervisorctl reread

Next, activate the changes with: 接下来,使用以下命令激活更改:

supervisorctl update

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM