基于AOP和注释寻找方法参数​​的验证框架 [英] Looking for method argument validation framework based on AOP and annotations

查看:181
本文介绍了基于AOP和注释寻找方法参数​​的验证框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找参数验证框架:结果
1)允许通过指定annontations参数的约束(如椭圆形,JaValid)结果
2)验证code自动注入(编译或运行时)到方法中(即需要Validator对象没有显式调用)

I'm looking for argument validation framework which:
1) Allows specifying argument constraints via annontations (like OVal, JaValid)
2) Validation code automatically injected (during compilation or runtime) into methods (i.e. no explicit call to Validator object is required)

的例子就是我在寻找:

Example of what i'm looking for:


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");
...


code我试图避免示例

Example of code i'm trying to avoid


...
    Validator validator = new Validator(...);//glue code
    Person person = new Person();
    person.setName("John");
    validator.validate(person);//glue code
...

谢谢解答!

推荐答案

我觉得你的意思是编译或运行时自动注入吧?

I think you meant "automatically injected during compilation or runtime", right?

我有同样的问题。我的解决办法是春天验证和自写AOP层(约三个等级)。

I had the same problem. My solution was Spring Validation and self-written AOP layer (about three classes).

我的验证code是这样的:

My validation code looks like this:

@Validational(验证=
  {com.mycompany.MyValidator})

@Validational( validators = {"com.mycompany.MyValidator"} )

公共无效myMethod的(字符串paramToValidate)

public void myMethod( String paramToValidate )

这篇关于基于AOP和注释寻找方法参数​​的验证框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆