0

Imagine i have an annotation A

@Target({TYPE})
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {Validator.class})
public @interface A {

   String value() default "";

   String message() default "{javax.validation.constraints.form.customization.message}";

   Class<?>[] groups() default {};

   Class<? extends Payload>[] payload() default {};
}

that has a Constraint Validator which is not an implementation of ConstraintValidator rather an extension of one

public interface Validator extends ConstraintValidator<A, Entity> {
}

And then some implementation of Validator. Is there any way i can make it work with Spring, dependency injection and all that good stuff? Thank you

1 Answers1

1

Okkkkkkkkkkkkk, it's actually possible.

Turns out this question is kind of a copy of another one. The only thing is it won't work just yet. Here's the final piece of code you have to add to your configuration.

    @Bean
    Validator validatorFactory() {
        ValidatorFactory validatorFactory = Validation.byDefaultProvider()
                .configure()
                .constraintValidatorFactory(new SpringConstraintValidatorFactoryEx())
                .buildValidatorFactory();

        return validatorFactory.getValidator();
    }

It's from Hibernate documentation in case someone is wondering