I'm looking to validate my object properties using customized validators, and i need to inject some beans. So i can't autowire any beans and i get this exception :
java.lang.NoSuchMethodException: com.*.*.MyValidatorValidator.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3517)
at java.base/java.lang.Class.getConstructor(Class.java:2238)
at org.hibernate.validator.internal.util.privilegedactions.NewInstance.run(NewInstance.java:41)
... 110 common frames omitted
How can success Autowire a beans ?
@Slf4j
@RequiredArgsConstructor
public class MyValidator implements ConstraintValidator<ValidMapper, String> {
private MyService myService;
@Autowired
public MyValidator(MyService myService) {
this.myService= myService;
}
@Override
public boolean isValid(String valueToValid, ConstraintValidatorContext context) {
// Some code using MyService
return true;
}
}
@RequiredArgsConstructor
;@Autowired
is not required if you only have a single constructor.) – Ackerman