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

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

我正在寻找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

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所示(链接到上面),嵌套条件可以是任何类型,在这个特定的例子中,它们不需要都是同一类型。

dxxyhpgq

dxxyhpgq2#

注解@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 * 文件中都存在这些属性,或者仅依赖缺省值
从此处获取Spring Boot SpEL ConditionalOnExpression检查多个属性

ttygqcqt

ttygqcqt3#

我正在寻找configurationOnProperty用法,在这里我可以指定考虑多个值
您可以使用Spring 4.0Condition 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;
      }

}

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

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;
      }

}
nlejzf6q

nlejzf6q4#

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

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

或使用

@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

相关问题