我刚接触Spring并开始使用Springboot构建Web服务。有一个要求说我必须根据类的特定变量的值启用/禁用RestController
。
我在看这里处理@ConditionalOnExpression
和@ConditionalOnProperty
的所有问题,但没有人告诉我是否可以根据值设置RestController
的条件变量。
如果我有以下课程
public class CheckCondition {
public boolean allowed = true;
}
我应该使用哪种条件注释来根据类的变量值来决定条件?
答案 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{
}