Using a constraint with a parmeterized constructor in Spring Boot application

68 Views Asked by At

I have a Spring Boot application where an interface has a constraint:

@Constraint(
    validatedBy = {
      MyValidator.class
    })
public @interface MyInterface {
    ...
}

I'm using Togglz to enable/disable some features and one class where I want to implement some Togglz code is in MyValidator.

public class MyValidator
    implements MyInterface<
        MyInterface, TDLDetails> {


  private FeatureManager featureManager;

  public static final Feature FEATURE_ONE =
      new NamedFeature("FEATURE_ONE ");

  public MyValidator(FeatureManager featureManager) {
    this.featureManager = featureManager;
  }

  @Override
  public void initialize(MyInterface arg0) {}

  @Override
  public boolean isValid(TDLDetails tdlDetails, ConstraintValidatorContext ctx) 
{
    if (!featureManager.isActive(FEATURE_ONE)) {
      if (tdlDetails.getType().equals(TDLType.ANA)) {
        return (tdlDetails.getPlaceOfIssue() != null);
      }
    }
    return true;
  }
}

Am I wrong to have the parameterized constructor? It seems I need it for Togglz but I'm not sure how it should be used by @Constraint if it takes a parameter. What's the correct way to do this?

0

There are 0 best solutions below