Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring@ConditionalOnProperty havingValue=";价值1“;或;价值2“;_Java_Spring_Spring Boot_Javabeans - Fatal编程技术网

Java Spring@ConditionalOnProperty havingValue=";价值1“;或;价值2“;

Java Spring@ConditionalOnProperty havingValue=";价值1“;或;价值2“;,java,spring,spring-boot,javabeans,Java,Spring,Spring Boot,Javabeans,我正在查找配置文件属性用法,在这里我可以指定考虑一个以上的值,如下面的 例如:@conditionalnproperty(value=“test.configname”,havingValue=“value1”或“value2”) 或 我想知道是否可以指定条件为havingValue!=“值3” 例如:@conditionalnproperty(value=“test.configname”,havingValue!=“value3”) 请告诉我是否有办法在spring boot配置中实现上述任

我正在查找<代码>配置文件属性用法,在这里我可以指定考虑一个以上的值,如下面的

例如:
@conditionalnproperty(value=“test.configname”,havingValue=“value1”或“value2”)

我想知道是否可以指定条件为
havingValue!=“值3”

例如:
@conditionalnproperty(value=“test.configname”,havingValue!=“value3”)


请告诉我是否有办法在
spring boot
配置中实现上述任何一项。

注释
@ConditionalOnProperty
@ConditionalOnExpression
都没有
java.lang.annotation.Repeatable
注释,因此您不能只添加多个注释用于检查多个属性

以下语法已经过测试,可以正常工作:

两个属性的解决方案

@ConditionalOnExpression("${properties.first.property.enable:true} && ${properties.second.property.startServer:false}")
@ConditionalOnExpression("${properties.first.property.enable:true} " +
        "&& ${properties.second.property.enable:true} " +
        "&& ${properties.third.property.enable:true}")
注意以下几点:

  • 您需要使用冒号表示法来指示 表达式语言语句中的属性
  • 每个属性都位于单独的表达式语言块${}
  • &&运算符在SpEL块之外使用
它允许具有不同值的多个属性,并可以扩展到多个属性

如果要检查2个以上的值并仍保持可读性,可以在要计算的不同条件之间使用串联运算符:

两个以上属性的解决方案

@ConditionalOnExpression("${properties.first.property.enable:true} && ${properties.second.property.startServer:false}")
@ConditionalOnExpression("${properties.first.property.enable:true} " +
        "&& ${properties.second.property.enable:true} " +
        "&& ${properties.third.property.enable:true}")
缺点是,不能像使用
@conditionalnproperty
注释时那样使用matchIfMissing参数,因此必须确保所有配置文件/环境的.properties或YAML文件中都存在属性,或者仅依赖默认值

从这里开始

我正在寻找
configurationOnProperty
用法,我可以在其中指定 考虑一个以上的值

您可以使用
Spring 4.0

此接口有一个方法
匹配(…)
,您可以使用它

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

}
然后在
@配置中使用
TestCondition
,如下所示:

@Configuration
public class TestConfig {

      @Conditional(value=TestCondition .class)
      public MyBean getTestConfigBean() {
          //TODO YOUR CODE;
      }

}
我想知道是否可以具体说明 ConfigurationProperty的条件为havingValue!=“价值3”

然后像这样使用它:

@Configuration
public class TestConfig {

      @Conditional(value=TestCondition2 .class)
      public MyBean getTestConfigBean() {
          //TODO YOUR CODE;
      }

}
Spring Boot提供了一个创建的条件,该条件将在任何嵌套条件匹配时匹配。当所有嵌套条件或无嵌套条件分别匹配时,它还提供和进行匹配

对于要匹配
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中嵌套条件注释(链接到上面)所示,嵌套条件可以是任何类型。它们不必都是同一类型的,因为它们在这种特殊情况下是相同的。

也许它的副本看起来很糟糕。你有票让@ConditionalOnProperty可重复吗?。但实现起来并不是那么容易,因为有些人需要和语义,而另一些人需要或。嵌套条件可能不美观,但
AnyNestedCondition
AllNestedConditions
nonnestedconditions
为您提供了很大的灵活性。它是灵活的,但不是用户友好的)您具有相同的逻辑(和)对于@Qualifier和everyone,每个人都是幸福的,除了问这个问题并想要
的人之外,因此上面使用了
AnyNestedCondition