简体   繁体   中英

bean validation jsr349 @Min.List , how does this work?

I am working on develop a custom Validation Annotation , and the annotation need to be repeatable.

"Min.List" can meet the needs , and i did the same thing on my own annotation .

@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();
    }
}

It works.

In my mind, the validator will create different instance for differnt type which need to be validated. So inside the validator,i can get the data from the annotation and store it on type level field. But under the repeatable annotaion circumstances, i notice there is more than one instance created.

So my question is how does the @interface List means and works ?

The behaviour you are observing is defined in the 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.

This was the way to support "Repeating Annotations" before Java 8.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM