MLS*_*MLS 7 java spring javabeans spring-boot
我正在寻找configurationOnProperty
可以指定考虑多个值的用法,如下所示
例如: @ConditionalOnProperty(value = "test.configname", havingValue = "value1" or "value2")
要么
我想知道是否可以指定confiugrationOnProperty
条件havingValue != "value3"
例如: @ConditionalOnProperty(value = "test.configname", havingValue != "value3")
请让我知道是否有一种方法可以实现上述任何一项spring boot
配置。
Ali*_*ien 11
注释@ConditionalOnProperty
和@ConditionalOnExpression
两者都没有java.lang.annotation.Repeatable
注释,因此您无法仅添加多个注释来检查多个属性。
以下语法已经过测试并有效:
两个属性的解决方案
@ConditionalOnExpression("${properties.first.property.enable:true} && ${properties.second.property.startServer:false}")
Run Code Online (Sandbox Code Playgroud)
请注意以下事项:
它允许具有不同值的多个属性并且可以扩展到多个属性。
如果要检查多于 2 个值并仍保持可读性,可以在要评估的不同条件之间使用连接运算符:
超过 2 个属性的解决方案
@ConditionalOnExpression("${properties.first.property.enable:true} " +
"&& ${properties.second.property.enable:true} " +
"&& ${properties.third.property.enable:true}")
Run Code Online (Sandbox Code Playgroud)
缺点是您不能像使用注释一样使用 matchIfMissing 参数,@ConditionalOnProperty
因此您必须确保所有配置文件/环境的.properties或YAML文件中都存在这些属性,或者仅依赖默认值价值
取自此处Spring Boot SpEL ConditionalOnExpression 检查多个属性
Spring Boot提供AnyNestedCondition
了一个已创建的条件,该条件将在任何嵌套条件匹配时匹配。当所有嵌套条件或没有嵌套条件分别匹配时,它还提供AllNestedConditions
和NoneNestedConditions
进行匹配。
对于您要匹配value1
或的值的特定情况,value2
您可以创建AnyNestedCondition
如下所示的代码:
class ConfigNameCondition extends AnyNestedCondition {
public ConfigNameCondition() {
super(ConfigurationPhase.PARSE_CONFIGURATION);
}
@ConditionalOnProperty(name = "test.configname", value = "value1")
static class Value1Condition {
}
@ConditionalOnProperty(name = "test.configname", value = "value2")
static class Value2Condition {
}
}
Run Code Online (Sandbox Code Playgroud)
然后将其与一起使用@Conditional
,例如:
@Bean
@Conditional(ConfigNameCondition.class)
public SomeBean someBean() {
return new SomeBean();
}
Run Code Online (Sandbox Code Playgroud)
如嵌套条件注释的javadoc中所示(链接到上面),嵌套条件可以是任何类型。在这种特殊情况下,它们不必都具有相同的类型。
我正在寻找
configurationOnProperty
可以指定考虑多个值的用法
您可以使用Condition接口Spring 4.0
。
该接口有一个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");
}
}
Run Code Online (Sandbox Code Playgroud)
然后TestCondition
在你的内部使用@Configuration
如下:
@Configuration
public class TestConfig {
@Conditional(value=TestCondition .class)
public MyBean getTestConfigBean() {
//TODO YOUR CODE;
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以指定 configurationOnProperty 条件为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");
}
}
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
@Configuration
public class TestConfig {
@Conditional(value=TestCondition2 .class)
public MyBean getTestConfigBean() {
//TODO YOUR CODE;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9673 次 |
最近记录: |