abhinav jain
abhinav jain

Reputation: 15

how can I load spring bean with hiberate mysql data spring-rest

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

Answers (1)

Jaiwo99
Jaiwo99

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

Related Questions