【发布时间】:2014-07-22 20:59:43
【问题描述】:
我在看@org.hibernate.validator.constaints.NotEmpty注解:
@Documented
@Constraint(validatedBy = { })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@ReportAsSingleViolation
@NotNull
@Size(min = 1)
public @interface NotEmpty {
String message() default "{org.hibernate.validator.constraints.NotEmpty.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
/**
* Defines several {@code @NotEmpty} annotations on the same element.
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
public @interface List {
NotEmpty[] value();
}
}
我对最后一部分感到困惑:
/**
* Defines several {@code @NotEmpty} annotations on the same element.
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
public @interface List {
NotEmpty[] value();
}
我不确定它是如何工作的,也不知道如何使用它。据我了解,Java 8 下的任何内容都不允许在同一元素上重复注释。
谁能澄清一下?
【问题讨论】:
-
它就像一个嵌套的静态类或嵌套接口。
@NotEmpty.List使用它。 -
现在,您可以使用@Repeatable 和容器注释来实现相同的目标,同时在注释和您要注释的内容中使用更少的冗长代码
标签: java hibernate validation annotations spring-validator