如何管理具有全局默认值的Spring Boot属性映射?

huangapple go评论58阅读模式
英文:

How to manage Spring Boot properties Map with global default value?

问题

我需要在我的Springboot应用程序中管理特定的行为:我需要能够仅为某些客户启用(或禁用)某个功能,但在给定客户的情况下,如果未定义该功能标志,我需要具有默认的全局值。

例如:

mycomponent1:
  myfeature1:
    enabled: true
    enabled.customer1: false

这意味着功能1对所有客户都启用,但对于customer1则不启用。

在我的属性类中,如何正确处理这种覆盖的方式是什么?因为我不能同时拥有相同名称的两个字段:

@ConfigurationProperties(prefix = "mycomponent1.myfeature1")
public MyComponent1Properties {

    private Boolean enabled;

    private Map<String, Boolean> enabled;

}

我尝试按以下方式重命名我的字段,以便能够在类中填充两个字段:

@ConfigurationProperties(prefix = "mycomponent1.myfeature1")
public MyComponent1Properties {

    private Boolean enabled;

    private Map<String, Boolean> enabledWithId;

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public void setEnabledWithId(Map<String, Boolean> enabled) {
        this.enabledWithId = enabledWithId;
    }

}

但这不起作用,因为我必须同时重命名我的属性(我不想这样做),或者字段未填充,因为setter名称与属性不匹配,然后属性绑定器无法填充字段。

有人能给出如何正确处理具有一个全局默认值的这种Map值的建议吗?非常感谢您的帮助。

英文:

I'd need to manage in my Springboot application a specific behaviour: I need to be able to activate a feature for some customers only (or disable this feature as well), but having a default global value in case the feature flag is not defined for a given customer.

For example:

mycomponent1:
  myfeature1:
    enabled: true
    enabled.customer1: false

would mean that feature1 is enabled for all customers but customer1.

What is the correct way to manage this kind of override as, in my property class, I cannot have both fields with the same name:

@ConfigurationProperties(prefix = &quot;mycomponent1.myfeature1&quot;)
public MyComponent1Properties {

    private Boolean enabled;

    private Map&lt;String, Boolean&gt; enabled;

}

I tried to rename my fields the following way to be able to have both fields populated in the class:

@ConfigurationProperties(prefix = &quot;mycomponent1.myfeature1&quot;)
public MyComponent1Properties {

    private Boolean enabled;

    private Map&lt;String, Boolean&gt; enabledWithId;

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public void setEnabledWithId(Map&lt;String, Boolean&gt; enabled) {
        this.enabledWithId = enabledWithId;
    }

}

but this doesn't work because either I have to rename my properties as well (and I don't want it) or the fields are not populated because setter name does not match the properties and then the properties binder cannot populate field.

Could someone advise about the right way to handle such Map values with one global default value ?
Thanks a lot for your help.

答案1

得分: 0

为此的最佳实践是创建一个属性,用于所谓的黑名单客户:

mycomponent1:
  myfeature1:
    enabled: true
    blacklisted: customer1,customer6

在你的代码中: private List&lt;String&gt; blacklisted;

因此,你的功能将对所有人启用,除了被列入黑名单的客户。只需不要忘记检查你的客户是否在此列表中 如何管理具有全局默认值的Spring Boot属性映射?

希望对你有所帮助。

英文:

Best practice for this is to create a property for, so-called, blacklisted customers:

mycomponent1:
  myfeature1:
    enabled: true
    blacklisted: customer1,customer6

And in your code: private List&lt;String&gt; blacklisted;

Thus, your feature will be enabled for everyone, except blacklisted customers. Just do not forget to check that your customer in this list 如何管理具有全局默认值的Spring Boot属性映射?

Hope it will helps you.

huangapple
  • 本文由 发表于 2023年2月27日 00:59:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定