Dependency Injection in JSR-303 Constraint Validator with Spring fails

前端 未结 3 461
Happy的楠姐
Happy的楠姐 2020-12-03 06:22

I have the same problem as here and here but couldn\'t find a solution yet.

So my sample test project will show the whole relevant configuration and code:

Co

3条回答
  •  萌比男神i
    2020-12-03 06:39

    This is what worked for me. I had to used the @Inject tag.

        public class FooValidator implements ConstraintValidator {
            private ValidationService validationService;
            @Inject
            public FooValidator(ValidationService validationService){
                this.validationService = validationService;
            }
            @Override
            public void initialize(final FooValid constraintAnnotation) {
                // TODO Auto-generated method stub
            }
            @Override
            public boolean isValid(final Integer value, final ConstraintValidatorContext context) {
                // this.validationService is always NULL!
                Assert.notNull(this.validationService, "the validationService must not be null");
                return false;
            }
    

    }

提交回复
热议问题