英文:
@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"})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论