Spring @ConditionalOnProperty havingValue = "value1" 或 "value2"
Spring @ConditionalOnProperty havingValue = "value1" or "value2"
我正在寻找 configurationOnProperty
用法,我可以在其中指定考虑多个值,如下所示
例如:@ConditionalOnProperty(value = "test.configname", havingValue = "value1" or "value2")
或
我想知道是否可以在 havingValue != "value3"
的条件下指定 confiugrationOnProperty
例如:@ConditionalOnProperty(value = "test.configname", havingValue != "value3")
如果有办法在 spring boot
配置中实现上述任何一个,请告诉我。
注释 @ConditionalOnProperty
和 @ConditionalOnExpression
都没有 java.lang.annotation.Repeatable
注释,因此您不能只添加多个注释来检查多个属性。
以下语法已经过测试并有效:
两个属性的解
@ConditionalOnExpression("${properties.first.property.enable:true} && ${properties.second.property.startServer:false}")
注意以下几点:
- 您需要使用冒号表示默认值
表达式语言语句中的 属性
- 每个属性在一个单独的表达式语言块中${}
- && 运算符在 SpEL 块之外使用
它允许具有不同值的多个属性并且可以扩展到多个属性。
如果您想检查超过 2 个值并仍然保持可读性,您可以在您正在评估的不同条件之间使用串联运算符:
超过 2 个属性的解决方案
@ConditionalOnExpression("${properties.first.property.enable:true} " +
"&& ${properties.second.property.enable:true} " +
"&& ${properties.third.property.enable:true}")
缺点是您不能像使用 @ConditionalOnProperty
注释时那样使用 matchIfMissing 参数,因此您必须确保属性存在于 .properties 中 或 YAML 文件用于所有 profiles/environments 或仅依赖默认值
取自此处
I am looking for configurationOnProperty
usage where I can specify to
consider more than one value
您可以使用 Spring 4.0
个中的 Condition interface 个。
此接口有一个方法 matches(...)
您可以使用。
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class TestCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String testValue = (context.getEnvironment().getProperty("test.configname");
return "value1".equalsIgnoreCase("testValue") || "value2".equalsIgnoreCase("testValue");
}
}
然后在 @Configuration
中使用 TestCondition
,如下所示:
@Configuration
public class TestConfig {
@Conditional(value=TestCondition .class)
public MyBean getTestConfigBean() {
//TODO YOUR CODE;
}
}
I would like to know if it is possible to specify
confiugrationOnProperty with condition of havingValue != "value3"
public class TestCondition2 implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String testValue = (context.getEnvironment().getProperty("test.configname");
return ! "value3".equalsIgnoreCase("testValue");
}
}
然后像这样使用它:
@Configuration
public class TestConfig {
@Conditional(value=TestCondition2 .class)
public MyBean getTestConfigBean() {
//TODO YOUR CODE;
}
}
Spring Boot 提供AnyNestedCondition
for created a condition that will match when any nested condition matches. It also provides AllNestedConditions
and 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 中所示(链接到上面),嵌套条件可以是任何类型。在这种特殊情况下,它们不需要都属于同一类型。
我正在寻找 configurationOnProperty
用法,我可以在其中指定考虑多个值,如下所示
例如:@ConditionalOnProperty(value = "test.configname", havingValue = "value1" or "value2")
或
我想知道是否可以在 havingValue != "value3"
confiugrationOnProperty
例如:@ConditionalOnProperty(value = "test.configname", havingValue != "value3")
如果有办法在 spring boot
配置中实现上述任何一个,请告诉我。
注释 @ConditionalOnProperty
和 @ConditionalOnExpression
都没有 java.lang.annotation.Repeatable
注释,因此您不能只添加多个注释来检查多个属性。
以下语法已经过测试并有效:
两个属性的解
@ConditionalOnExpression("${properties.first.property.enable:true} && ${properties.second.property.startServer:false}")
注意以下几点:
- 您需要使用冒号表示默认值 表达式语言语句中的 属性
- 每个属性在一个单独的表达式语言块中${}
- && 运算符在 SpEL 块之外使用
它允许具有不同值的多个属性并且可以扩展到多个属性。
如果您想检查超过 2 个值并仍然保持可读性,您可以在您正在评估的不同条件之间使用串联运算符:
超过 2 个属性的解决方案
@ConditionalOnExpression("${properties.first.property.enable:true} " +
"&& ${properties.second.property.enable:true} " +
"&& ${properties.third.property.enable:true}")
缺点是您不能像使用 @ConditionalOnProperty
注释时那样使用 matchIfMissing 参数,因此您必须确保属性存在于 .properties 中 或 YAML 文件用于所有 profiles/environments 或仅依赖默认值
取自此处
I am looking for
configurationOnProperty
usage where I can specify to consider more than one value
您可以使用 Spring 4.0
个中的 Condition interface 个。
此接口有一个方法 matches(...)
您可以使用。
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class TestCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String testValue = (context.getEnvironment().getProperty("test.configname");
return "value1".equalsIgnoreCase("testValue") || "value2".equalsIgnoreCase("testValue");
}
}
然后在 @Configuration
中使用 TestCondition
,如下所示:
@Configuration
public class TestConfig {
@Conditional(value=TestCondition .class)
public MyBean getTestConfigBean() {
//TODO YOUR CODE;
}
}
I would like to know if it is possible to specify confiugrationOnProperty with condition of havingValue != "value3"
public class TestCondition2 implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String testValue = (context.getEnvironment().getProperty("test.configname");
return ! "value3".equalsIgnoreCase("testValue");
}
}
然后像这样使用它:
@Configuration
public class TestConfig {
@Conditional(value=TestCondition2 .class)
public MyBean getTestConfigBean() {
//TODO YOUR CODE;
}
}
Spring Boot 提供AnyNestedCondition
for created a condition that will match when any nested condition matches. It also provides AllNestedConditions
and 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 中所示(链接到上面),嵌套条件可以是任何类型。在这种特殊情况下,它们不需要都属于同一类型。