我正在开发自定义验证注释,并且注释需要可重复。
"Min.List"可以满足需求,我自己的注释上也做了同样的事情。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = ComboValidator.class)
public @interface Combo {
String dependField();
String controlledField();
Class<? extends Releation> relation() default BaseReleation.class;
String message() default "{combo validation}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default {};
/**
* Defines several {@link Combo} annotations on the same element.
*
* @see Combo
*/
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@interface List {
Combo[] value();
}
}
它有效。
在我看来, validator 将为需要验证的不同类型创建不同的实例。因此,在 validator 内部,我可以从注释中获取数据并将其存储在类型级别字段中。但在可重复注释的情况下,我注意到创建了多个实例。
所以我的问题是 @interface List 的含义和工作原理是什么?
最佳答案
您观察到的行为在 Bean Validation Spec 中定义。 :
[...] Bean Validation provider treats regular annotations [...] whose value element has a return type of an array of constraint annotations in a special way. Each element in the value array are processed by the Bean Validation implementation as regular constraint annotations.
这是 Java 8 之前支持“重复注释”的方式。
关于java - bean 验证 jsr349 @Min.List ,这是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52552267/