任何Spring条件注释,其条件取决于特定变量的值?

时间:2017-05-24 08:45:31

标签: java spring spring-mvc spring-boot

我刚接触Spring并开始使用Springboot构建Web服务。有一个要求说我必须根据类的特定变量的值启用/禁用RestController

我在看这里处理@ConditionalOnExpression@ConditionalOnProperty的所有问题,但没有人告诉我是否可以根据值设置RestController的条件变量。

如果我有以下课程

public class CheckCondition {

  public boolean allowed = true;
}

我应该使用哪种条件注释来根据类的变量值来决定条件?

1 个答案:

答案 0 :(得分:1)

您可以通过扩展Condition接口来编写自己的条件类。像这样的东西

class SomeCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

       //do your logic and return true or false based on some condition
    }
}

然后你可以在像这样的类

上使用这个条件
@Configuration
@Conditional(value = SomeCondition.class)
public class YourClass{
}