我想编写具有复杂逻辑的自定义验证器,该逻辑取决于必须在启动时设置的某些属性。可以在spring配置文件中写入component-scan标签,但是如何在class或spring validater中设置其他属性作为无状态类?
public class CustomValidator implements Validator{
private Map<String,Integer> parameters;
public boolean supports(Class clazz) {
return ObjectToValidate.class.equals(clazz);
}
public void validate(Object obj, Errors e) {
ObjectToValidate object = (ObjectToValidate) obj;
switch (parameters.get(object.getIntegerProperty())) {
case 1:
//validation algorithm 1;
break;
case 2:
//validation algorithm 2;
break;
//etc.
}
}
public void setParameters(Map<String,Integer> parameters){
this.parameters = parameters;
}
}