I am looking for configurationOnProperty
usage where I can specify to consider more than one value as shown below
Eg: @ConditionalOnProperty(value = "test.configname", havingValue = "value1"or "value2")
或
I would like to know if it is possible to specify confiugrationOnProperty
with condition of havingValue != "value3"
Eg: @ConditionalOnProperty(value = "test.configname", havingValue != "value3")
Please let me know if there is a way to achieve any one of the above in spring boot
configuration.
最佳答案
Spring Boot 提供 AnyNestedCondition
for 创建了一个条件,该条件将在任何嵌套条件匹配时匹配。它还提供AllNestedConditions
和 NoneNestedConditions
用于分别匹配所有嵌套条件或不匹配任何嵌套条件时的匹配。
对于您想要匹配 value1
或 value2
值的特定情况,您可以创建一个 AnyNestedCondition
,如下所示:
class ConfigNameCondition extends AnyNestedCondition {
public ConfigNameCondition() {
super(ConfigurationPhase.PARSE_CONFIGURATION);
}
@ConditionalOnProperty(name = "test.configname", havingValue = "value1")
static class Value1Condition {
}
@ConditionalOnProperty(name = "test.configname", havingValue = "value2")
static class Value2Condition {
}
}
然后将它与@Conditional
一起使用,例如:
@Bean
@Conditional(ConfigNameCondition.class)
public SomeBean someBean() {
return new SomeBean();
}
如嵌套条件注释的 javadoc 中所示(链接到上面),嵌套条件可以是任何类型。在这种特殊情况下,它们不需要都属于同一类型。
关于java - Spring @ConditionalOnProperty havingValue = "value1"或 "value2",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51257432/