4

I want to add validations to a Java Bean. For example, I want to do the following:

@MaxLength(50)
@RequiredField
public void setEmployeeName(String name){
   .....
}

I know I can write code that gets the validations for a specific method by calling method.getDeclaredAnnotation after all the bean values have been set. I would like to avoid writing this code

Is there anything in Java6 that gives standard validations via annotations? Do I need aspectj to invoke these annotations?

thanks in advance.

4

3 回答 3

1

You can use Bean Validation Framework. Here is short overview http://relation.to/Bloggers/BeanValidationSneakPeekPartI

于 2009-09-11T18:03:22.053 回答
0

看看JSR 303。RI(参考实现)在这里还有一个很好的教程。不,您不需要 AspectJ。

于 2009-09-11T18:20:32.217 回答
0

您能够做到这一点的唯一方法是通过反射和自定义验证实用程序/拦截器/代理。JSR 303 和 JSR 305 被提议引入类似的功能,但不存在类似的功能。

您将遇到的问题之一是这些注释需要在某种框架级别处理,或者至少在某种调用操作之前被拦截。两种最常见的蛮力方法可以通过创建实用程序或通过在调用处理程序(代理)中验证预调用来完成。

现实情况是,除非这被内置到 Spring、Struts、Guice、Java 本身等中,否则只会产生不必要的开销,最好按需检查验证边界。

于 2009-10-07T03:52:07.647 回答