Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 这个嵌套注释做什么/允许什么?_Java_Hibernate_Validation_Annotations_Spring Validator - Fatal编程技术网

Java 这个嵌套注释做什么/允许什么?

Java 这个嵌套注释做什么/允许什么?,java,hibernate,validation,annotations,spring-validator,Java,Hibernate,Validation,Annotations,Spring Validator,我在看@org.hibernate.validator.constants.NotEmpty注释: @Documented @Constraint(validatedBy = { }) @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) @Retention(RUNTIME) @ReportAsSingleViolation @NotNull @Size(min = 1) public @interface No

我在看
@org.hibernate.validator.constants.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();
    }
}
我不知道它是如何工作的,也不知道如何使用它。据我所知,Java8下的任何东西都不允许在同一个元素上重复注释

有人能澄清一下吗?

之所以存在,是因为同一个元素不能重复相同的注释。在NotEmpty.List的帮助下,多个注释有效地应用于一个元素。注释处理检查NotEmpty注释列表,这些注释是NotEmpty.list的值

在NotEmpty的情况下,使用验证器列表的一个原因可能是每个组使用和分配不同的消息

为了举例,让我们以可以代表公司或个人的实体为例。在这两种情况下,名称不应为null,但消息不同:

@NotEmpty.List({
    @NotEmpty( message = "Person name should not be empty",   
               groups=PersonValidations.class),
    @NotEmpty( message = "Company name should not be empty",    
               groups=CompanyValidations.class),
})
private String name;

它就像一个嵌套的静态类或嵌套的接口。通过
@NotEmpty.List
使用它。那必须是@NotEmpty.List({…}),不是吗?
@NotEmpty.List({
    @NotEmpty( message = "Person name should not be empty",   
               groups=PersonValidations.class),
    @NotEmpty( message = "Company name should not be empty",    
               groups=CompanyValidations.class),
})
private String name;