gpt4 book ai didi

java - ElementType.CONSTRUCTOR 有什么用?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:29:27 24 4
gpt4 key购买 nike

我正在学习注释和注释处理器。

我遇到了 javax.validation.constraints.NotNull声明

@Target(value={METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER})
@Retention(value=RUNTIME)
@Documented
@Constraint(validatedBy={})
public @interface NotNull

我已经对这个注解有点熟悉了。

@NotNull
public Integer getAge() {
return age;
}

@NotNull
private String name;

private int age;

@NotNullElementType.CONSTRUCTOR 有什么用法?

最佳答案

ElementType.CONSTRUCTOR 指出注释可以在构造函数级别分类(当在注释声明中指定 @Target 时)。

例子:

@Entity
@Table(name = "Request")
public class Request implements Serializable {

@NotNull
public Request() {

}
}

Beans Validation Specification (1.0 final) 声明如下(在 2.1 约束注释 章):

Constraint annotations can target any of the following ElementTypes:

  • FIELD for constrained attributes
  • METHOD for constrained getters
  • TYPE for constrained beans
  • ANNOTATION_TYPE for constraints composing other constraints.

While other ElementTypes are not forbidden, the provider does not have to recognize and process constraints placed on such types. Built-in types do support PARAMETER and CONSTRUCTOR to allow Bean Validation provider specific extensions. It is considered good practice to follow the same approach for custom annotations.

本质上,虽然还没有建设性/有用的示例在构造函数级别使用@NotNull,但规范声明包含ElementType 注释(因为内置注释已经包含它们)。

希望对您有所帮助。

关于java - ElementType.CONSTRUCTOR 有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16413647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com