Reputation: 15
I am trying to perform validation in spring and for that I need some data to be available before I execute the validation. That data is in my sql inside the table . I am looking for solution which will load my spring bean from the mysql table and that bean I can use to get the data for the validation .
Upvotes: 1
Views: 58
Reputation: 10017
Check out this example.. you just need declare your validator as bean.
@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = MyValidatorImpl.class)
@Documented
public @interface MyValidator {
String message() default "invalid";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
@Component // <---- this will allow you to access spring component
public class MyValidatorImpl implements ConstraintValidator<MyValidator, String> {
@Autowired MyDAO myDAO;
public void initialize(MyValidator constraint) {
}
public boolean isValid(String s, ConstraintValidatorContext context) {
return false;
}
}
Upvotes: 1