@为复杂对象创建条件属性,转换为YAML格式

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

@Conditionalonproperty for complex objects to yaml

问题

我有一个带有以下结构的 YAML 配置:

  foo:
    bar:
      myComplexObject:
        firstProperty: 1000
        secondProperty: "You know the rules, and so do I"
        rickRollingExecuted: true

如果在配置中存在 "myComplexObject" 属性,我需要初始化一个 bean。

我尝试过:

@ConditionalOnProperty(prefix = "foo.bar", name = "myComplexObject")

但在这种情况下,Spring Boot 会忽略该属性,并且不会初始化我的 bean。

但对于简单的结构,比如:

 foo:
    bar:
      value: 1000

@ConditionalOnProperty(prefix = "foo.bar", name = "value")

对于复杂对象,是否可以使用 @ConditionalOnProperty?也许有一些替代方法来实现相同的效果?

英文:

I am having yaml config with the next structure:

  foo:
    bar:
      myComplexObject:
        firstProperty: 1000
        secondProperty: "You know the rules, and so do I"
        rickRollingExecuted: true

And I need to init bean only if "myComplexObject" property is present in config.

I've tried:

@ConditionalOnProperty(prefix = "foo.bar", name = "myComplexObject")

But in this case spring boot ignores property and do not init my bean.

But it works fine for simple structures like:

 foo:
    bar:
      value: 1000

@ConditionalOnProperty(prefix = "foo.bar", name = "value")

Is that possible to use @ConditionalOnProperty for complex objects ? Maybe there is some alternative to do the same?

答案1

得分: 1

当然,请尝试这样做:

@ConditionalOnProperty(prefix = "foo.bar", name = {"firstProperty", "secondProperty", "rickRollingExecuted"})
英文:

Sure, try this:

@ConditionalOnProperty(prefix = "foo.bar", name = {"firstProperty","secondProperty", "rickRollingExecuted"})

huangapple
  • 本文由 发表于 2020年10月6日 22:38:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64228084.html
匿名

发表评论

匿名网友

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

确定