【发布时间】:2011-06-20 14:09:26
【问题描述】:
我正在寻找以下参数验证框架:
1) 允许通过注释(如 OVal、JaValid)指定参数约束
2) 验证代码自动注入(在编译或运行时)到方法中(即不需要显式调用 Validator 对象)
我正在寻找的示例:
public class Person {
private String name;
....
//Method which arguments should be validated
public void setName(@NotBlank String name){
//<---validating code should be injected here
this.name = name;
}
}
我试图避免的代码示例
//Example of call to the validated method
...
Person person = new Person();
person.setName("John");
...
...
Validator validator = new Validator(...);//glue code
Person person = new Person();
person.setName("John");
validator.validate(person);//glue code
...
感谢您的回答!
【问题讨论】:
-
我找到了一个合适的解决方案(顺便说一句,它在 OVal 文档中)——可以将 OVal 与 Spring AOP 集成,例如通过使用 DefaultAdvisorAutoProxyCreator。它仍然不是完美的解决方案(因为它需要使用弹簧,并且 OVal 约束不是我想要的特定类型)。
标签: java validation annotations aop arguments