Java Spring条件注释运算符

Java Spring条件注释运算符,java,spring,spring-security,Java,Spring,Spring Security,我需要为spring安全性的两个配置类设置一个条件。当条件为真时,使用配置A;当条件为假时,使用配置B 我目前使用两个条件类。它们产生相反的结果 我可以在条件注释中使用一些运算符吗?像这样的 @Conditional( value = !MyCondition.class ) @Conditional注释接收一个实现条件接口的类名,如果该条件匹配,则创建bean 如果您试图实现的条件只是对另一个现有条件的否定,那么您可以从现有条件进行扩展,并重写matches方法,作为调用父类matches方

我需要为spring安全性的两个配置类设置一个条件。当条件为真时,使用配置A;当条件为假时,使用配置B

我目前使用两个条件类。它们产生相反的结果

我可以在条件注释中使用一些运算符吗?像这样的

@Conditional( value = !MyCondition.class )

@Conditional
注释接收一个实现
条件
接口的类名,如果该条件匹配,则创建bean

如果您试图实现的条件只是对另一个现有条件的否定,那么您可以从现有条件进行扩展,并重写
matches
方法,作为调用父类
matches
方法的否定。

在Spring中调用代码以获得反向条件

   public class MyCondition extends SpringBootCondition {
        public ConditionOutcome getMatchOutcome(ConditionContext context, 
             AnnotatedTypeMetadata metadata) {
          ConditionMessage.Builder message = ConditionMessage.forCondition("MyCondition");

          boolean enable = Binder.get(context.getEnvironment()).bind("execute.enable", 
                Boolean.class).orElse(false);
          if (enable) {
              return ConditionOutcome.match(message.foundExactly("execute enabled"));
          }

          return ConditionOutcome.noMatch(message.notAvailable("execute disable"));
       }
    }

    public class InversedCondition extends MyCondition {

          @Override
          public ConditionOutcome getMatchOutcome(
              ConditionContext context, AnnotatedTypeMetadata metadata) {
            return ConditionOutcome.inverse(super.getMatchOutcome(context, metadata));
          }
    }
有时候,你并不真的需要一个反向条件。如果您将MyCondition用于EnableRun类。然后,您可以对DisalbeRun类使用@ConditionalOnMissingBean(enablehollo.class)