这个嵌套注释是做什么/允许的? [英] What does this nested annotation do / allow?

查看:185
本文介绍了这个嵌套注释是做什么/允许的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 @ org.hibernate.validator.constaints.NotEmpty 注释:

 @Retention(RUNTIME)
@ReportAsSingleViolation
@NotNull
@Size(min = 1)
public @interface NotEmpty {
String message()default{org.hibernate.validator.constraints。 NotEmpty.message};

Class<?> [] groups()default {};

类<?扩展了Payload> [] payload()default {};

/ **
*在同一元素上定义了多个{@code @NotEmpty}注释。
* /
@Target({METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER})
@Retention(RUNTIME)
@Documented
public @interface List {
NotEmpty [] value();


$ / code $ / pre
$ b $ p我很困惑最后一部分: p>

  / ** 
*在同一元素上定义多个{@code @NotEmpty}注释。
* /
@Target({METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER})
@Retention(RUNTIME)
@Documented
public @interface List {
NotEmpty [] value();
}

我不确定这是如何工作的,也不知道如何使用它。根据我的理解,Java 8下的任何内容都不允许在同一元素上重复注释。



任何人都可以澄清吗? h2_lin>解决方案

为什么 NotEmpty.List 存在,是为了避免同一个元素不能重复相同的注释。借助NotEmpty.List多个 NotEmpty 注释被有效地应用于一个元素。注释处理通过作为NotEmpty.List的值的NotEmpty注释列表进行检查。

对于NotEmpty而言,使用验证器列表的一个原因可能是使用并为每个组分配不同的消息。



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

  @ NotEmpty.List({
@NotEmpty( message =人名不应该为空,
groups = PersonValidations.class),
@NotEmpty(message =公司名称不应该为空,
groups = CompanyValidations.class) ,
})
私有字符串名称;


I was looking at the @org.hibernate.validator.constaints.NotEmpty annotation:

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

I'm confused by the last part:

    /**
     * Defines several {@code @NotEmpty} annotations on the same element.
     */
    @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
    @Retention(RUNTIME)
    @Documented
    public @interface List {
        NotEmpty[] value();
    }

I'm not sure how that works, nor how to use it. From my understanding, anything under Java 8 does not allow repeated annotations on the same element.

Can anybody clarify?

解决方案

Reason why NotEmpty.List exists, is to go around the fact that same annotation cannot be repeated for same element. With the help of NotEmpty.List multiple NotEmpty annotations are effectively applied to one element. Annotation processing checks through the list of NotEmpty annotations that are the value of NotEmpty.List.

In the case of NotEmpty one reason for using List of validators could be use of groups and assigning different messages per group.

For the sake of example, let's take entity which can represent either company or person. In both cases name should not be null, but messages differ:

@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;

这篇关于这个嵌套注释是做什么/允许的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆