@Conditionalonproperty for complex objects to yaml



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?

Answer

Sure, try this:

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


Source: stackoverflow