具有值=“值1”或“值2”的Spring@条件开属性

voase2hg  于 2023-03-02  发布在  Spring
关注(0)|答案(4)|浏览(364)

我正在寻找configurationOnProperty用法,其中我可以指定考虑多个值,如下所示
例如:@ConditionalOnProperty(value = "test.configname", havingValue = "value1" or "value2")

我想知道是否可以使用havingValue != "value3"条件指定confiugrationOnProperty
例如:@ConditionalOnProperty(value = "test.configname", havingValue != "value3")
请让我知道是否有办法在spring boot配置中实现上述任一项。

hc2pp10m

hc2pp10m1#

Sping Boot 提供AnyNestedCondition用于创建一个条件,当任何嵌套条件匹配时,该条件将匹配。它还分别提供AllNestedConditionsNoneNestedConditions用于匹配所有嵌套条件或没有嵌套条件匹配。
对于您想要匹配value1value2的值的特定情况,您将创建如下AnyNestedCondition

  1. class ConfigNameCondition extends AnyNestedCondition {
  2. public ConfigNameCondition() {
  3. super(ConfigurationPhase.PARSE_CONFIGURATION);
  4. }
  5. @ConditionalOnProperty(name = "test.configname", havingValue = "value1")
  6. static class Value1Condition {
  7. }
  8. @ConditionalOnProperty(name = "test.configname", havingValue = "value2")
  9. static class Value2Condition {
  10. }
  11. }

然后将其用于@Conditional,例如:

  1. @Bean
  2. @Conditional(ConfigNameCondition.class)
  3. public SomeBean someBean() {
  4. return new SomeBean();
  5. }

如嵌套条件注解的javadoc所示(链接到上面),嵌套条件可以是任何类型,在这个特定的例子中,它们不需要都是同一类型。

展开查看全部
dxxyhpgq

dxxyhpgq2#

注解@ConditionalOnProperty@ConditionalOnExpression都没有java.lang.annotation.Repeatable注解,因此您无法仅添加多个注解来检查多个属性。
下面的语法已经过测试并且有效:

    • 两个属性的解决方案**
  1. @ConditionalOnExpression("${properties.first.property.enable:true} && ${properties.second.property.startServer:false}")

请注意以下事项:

  • 您需要使用冒号表示法来指示表达式语言语句中属性的默认值
  • 每个属性位于单独的表达式语言块${}中
  • &&运算符在SpEL块外部使用

它允许多个属性具有不同的值,并可以扩展到多个属性。
如果要检查2个以上的值并仍保持可读性,可以在要计算的不同条件之间使用串联运算符:

    • 2个以上属性的解决方案**
  1. @ConditionalOnExpression("${properties.first.property.enable:true} " +
  2. "&& ${properties.second.property.enable:true} " +
  3. "&& ${properties.third.property.enable:true}")

缺点是您不能像使用@ConditionalOnProperty注解时那样使用matchIfMissing参数,因此您必须确保所有配置文件/环境的 *. properties * 或 * YAML * 文件中都存在这些属性,或者仅依赖缺省值
从此处获取Spring Boot SpEL ConditionalOnExpression检查多个属性

展开查看全部
ttygqcqt

ttygqcqt3#

我正在寻找configurationOnProperty用法,在这里我可以指定考虑多个值
您可以使用Spring 4.0Condition interface
这个接口有一个可以使用的方法matches(...)

  1. import org.springframework.context.annotation.Condition;
  2. import org.springframework.context.annotation.ConditionContext;
  3. import org.springframework.core.type.AnnotatedTypeMetadata;
  4. public class TestCondition implements Condition {
  5. @Override
  6. public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
  7. String testValue = (context.getEnvironment().getProperty("test.configname");
  8. return "value1".equalsIgnoreCase("testValue") || "value2".equalsIgnoreCase("testValue");
  9. }
  10. }

然后在@Configuration中使用TestCondition,如下所示:

  1. @Configuration
  2. public class TestConfig {
  3. @Conditional(value=TestCondition .class)
  4. public MyBean getTestConfigBean() {
  5. //TODO YOUR CODE;
  6. }
  7. }

我想知道是否可以在havingValue!="value3"的条件下指定configrationOnProperty

  1. public class TestCondition2 implements Condition {
  2. @Override
  3. public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
  4. String testValue = (context.getEnvironment().getProperty("test.configname");
  5. return ! "value3".equalsIgnoreCase("testValue");
  6. }
  7. }

然后像这样使用它:

  1. @Configuration
  2. public class TestConfig {
  3. @Conditional(value=TestCondition2 .class)
  4. public MyBean getTestConfigBean() {
  5. //TODO YOUR CODE;
  6. }
  7. }
展开查看全部
nlejzf6q

nlejzf6q4#

要具有复杂条件,可以使用@ConditionalOnExpression并在其中使用SpEl查询,如

  1. @ConditionalOnExpression("#{ T(java.util.Arrays).asList('value1', 'value2').contains('${test.configname}')}")

或使用

  1. @ConditionalOnExpression("'${test.configname}' ne 'value3'")

要了解更多信息,请查看https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/expressions.htmlhttps://www.baeldung.com/spring-conditional-annotations

相关问题