Auro
Auro

Reputation: 1638

Any Spring conditional annotation whose condition depends on the value of a specific variable?

I'm new to Spring and have started building Web services with Springboot. There is a requirement that says I have to enable / disable a RestController based on a value of a specific variable of a Class.

I was looking at all the questions here dealing with @ConditionalOnExpression and @ConditionalOnProperty, but none of them told me if I could set the condition of a RestController on the basis of a value of a variable.

If I have the following class

public class CheckCondition {

  public boolean allowed = true;
}

what conditional annotation should I use to that will decide the condition on the basis of the class's variable's value?

Upvotes: 0

Views: 2316

Answers (1)

pvpkiran
pvpkiran

Reputation: 27078

you can write your own conditional class by extending Condition interface. Something like this

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

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

And then you can use this condition on a class like this

@Configuration
@Conditional(value = SomeCondition.class)
public class YourClass{
}

Upvotes: 2

Related Questions