2016-12-05 75 views

回答

1

看看這個例子..你只需要聲明你的驗證器爲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; 
    } 
} 
+0

在這個例子中,我們注入mydao這將有法呼籲獲取數據。但我的問題是我只需要在驗證期間加載數據的POJO。糾正我,如果我不正確你的例子 –

+0

@abhinavjain你可以有'myDAO.loadData(字符串)你的pojo或我不明白你 – Jaiwo99

+0

謝謝你的答案。我看起來像這樣@Autowire MyPojo myPojo和注射後我可以使用我的pojo。 pojo彈簧的加載應該處理。或者可能是我期待春天不能提供的東西,我不確定 –