我可以在Java应用程序中为config.yml指定默认值吗? [英] Can I specify default values for a config.yml on my java application?

查看:61
本文介绍了我可以在Java应用程序中为config.yml指定默认值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用dropwizard处理我的Java应用程序.

I am currently working with my java application with dropwizard.

它工作正常,我运行它发送yml文件完整或参数.

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

所以,我要做的是将此yml文件设置为默认值,并在需要时(用于将来的功能)发送此 var

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

所以我想做这样的事情

reporting:
  enabled: ${REPORTING_FLAG:false}

然后我可以将REPORTING_FLAG作为环境参数(泊坞窗)发送,并且应该可以正常工作....

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.

有什么办法可以做到这一点吗?是否需要考虑其他配置?我以前用spring应用程序做到过,但这似乎有所不同.

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)

想法?

推荐答案

因此,在阅读了更多dropwizard文档之后,我发现了这一点:

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

环境变量

dropwizard配置模块还提供了使用SubstitutingSourceProvider和EnvironmentVariableSubstitutor将配置设置替换为环境变量值的功能.

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()
                )
        );

    }

    // [...]
}

需要替换的配置设置必须在配置文件中明确编写,并遵循Apache Commons Lang库中StrSubstitutor的替换规则.

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}

这篇关于我可以在Java应用程序中为config.yml指定默认值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆