Can I specify default values for a config.yml on my java application?

自作多情 提交于 2021-01-28 09:20:58

问题


I am currently working with my java application with dropwizard.

It works fine and I run it sending a yml file full or parameters.

So, for example, one of them is this one:

reporting:
  enabled: false

So, what I want to do is to make this yml file my default and when needed (for future functions) send the value of this var

So Id like to do something like this

reporting:
  enabled: ${REPORTING_FLAG:false}

Then I can send REPORTING_FLAG as a environment parameter (docker) and should work fine....

The issue is that I see this model is not recognised by my app.

Is there any way I can do it like this? is there an extra configuration to take in consideration? Ive done it before with a spring app but this seems to be different.

Right now I am getting an error that the expected boolean value is not valid (taking the whole line as a string)

Ideas?


回答1:


So, after reading more dropwizard documentation I found this: https://www.dropwizard.io/0.8.2/docs/manual/core.html

Environment variables

The dropwizard-configuration module also provides the capabilities to substitute configuration settings with the value of environment variables using a SubstitutingSourceProvider and EnvironmentVariableSubstitutor.

public class MyApplication extends Application<MyConfiguration> {
    // [...]
    @Override
    public void initialize(Bootstrap<MyConfiguration> bootstrap) {
        // Enable variable substitution with environment variables
        bootstrap.setConfigurationSourceProvider(
                new SubstitutingSourceProvider(bootstrap.getConfigurationSourceProvider(),
                                                   new EnvironmentVariableSubstitutor()
                )
        );

    }

    // [...]
}

The configuration settings which should be substituted need to be explicitly written in the configuration file and follow the substitution rules of StrSubstitutor from the Apache Commons Lang library.

mySetting: ${DW_MY_SETTING}
defaultSetting: ${DW_DEFAULT_SETTING:-default value}


来源:https://stackoverflow.com/questions/53831014/can-i-specify-default-values-for-a-config-yml-on-my-java-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!