自定义Annotation(二)下

简介: 自定义Annotation(二)

三. 添加元注解,表明注解的位置


可以添加四个元注解, @Retention, @Documented, @Target, @Inherited


可以用这四个元注解,来为自定义的新注解添加功能。


三.一 @Target


表示可以定义的范围, 具体看 枚举 ElementType 的取值。


@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    ElementType[] value();
}


如 想使 @Table 只能放置在 类上面,不能放置在方法或者属性上面。


@Target(ElementType.TYPE) //只能放置在类上
public @interface Table {
}


那么,当将 @Table 放置在属性,或者方法上时,就会出错。


20200702195157988.png


@Id 和 @Column 定义在属性上面。


@Target(ElementType.FIELD)
public @interface Id {
}


@Target(ElementType.FIELD)
public @interface Column {
}


当然, 范围可以定义多个, 用数组传递即可。


@Target({ElementType.METHOD,ElementType.FIELD})
public @interface Name {
}


可以放置在 方法上面,也可以放置在属性上面 。


三.二 @Retention


定义保存的范围。


@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    /**
     * Returns the retention policy.
     * @return the retention policy
     */
    RetentionPolicy value();
}


用户自定义的,建议使用 RUNTIME


@Target(ElementType.TYPE) //只能放置在类上
@Retention(RetentionPolicy.RUNTIME) //jvm虚拟机可以载入
public @interface Table {
}


三.三 @Documented


是否生成文档


@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}


用户自定义的,建议添加 @Documented, 默认不添加 。


三.四 @Inherited


是否继承


@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}


不建议添加继承。


一般在自定义的注释上面,添加 @Documented, @Target, @Retention 三个元注解即可。


四. 添加属性


注解,里面的是格式是: 数据类型 属性值


属性值,可以有一个,也可以有多个,也可以是数组形式, 可以添加默认值。


如果属性只有一个的话, 均采用 value 属性。 规定,如果一个注解只有一个属性值,并且该属性值为 value 的话,那么可以省略 value属性。


如果属性没有默认值,那么必须提供该属性的值。


四.一 添加一个属性值


@Target(ElementType.TYPE) //只能放置在类上
@Retention(RetentionPolicy.RUNTIME) //jvm虚拟机可以载入
@Documented
@Inherited
public @interface Table {
    //添加一个属性值, 表名 
    String tableName();
}


应用到 Person 上面


@Table(tableName="person")
public class Person implements Serializable {
}


四.二 添加多个属性值


@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME) //jvm虚拟机可以载入
@Documented
public @interface Column {
    //列名
    String name();
    //长度
    int length();
}


应用到 Person 上面


 @Column(name = "name",length = 50)
    private  String name;


四.三 添加默认值


用 default 默认值 即可。


@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME) //jvm虚拟机可以载入
@Documented
public @interface Id {
    //默认列名是 id
    String name() default "id";
    //默认长度是 11 int 类型 
    int length() default 11;
}


应用到 Person 上面


@Id
 private int id;


四.四 添加数组类型


@Target({ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME) //jvm虚拟机可以载入
@Documented
public @interface Name {
    //填入值,数组形式
    String[] value() default {"两个蝴蝶飞"};
    //注入
    String comment() default "一个快乐的程序员";
}


一个自定义的注解, 只有添加了作用范围和 属性值,才算比较完整,才可以使用。


一定要掌握创建注解的流程, 先创建基本的,再添加作用范围,后添加属性。


谢谢您的观看,如果喜欢,请关注我,再次感谢 !!!

相关文章
|
1月前
|
Java 编译器 程序员
JavaSE中注解(Annotation)详细介绍与实例
JavaSE中注解(Annotation)详细介绍与实例
46 0
|
安全 Java 编译器
一文带你全面深入理解Java注解Annotation
一文带你全面深入理解Java注解Annotation
212 1
一文带你全面深入理解Java注解Annotation
|
Java 开发者
annotation 实现 | 学习笔记
快速学习 annotation 实现,介绍了 annotation 实现系统机制, 以及在实际应用过程中如何使用。
64 0
annotation 实现 | 学习笔记
|
开发框架 Java 编译器
Java注解Annotation小结
Java注解Annotation小结
|
Java 编译器
自定义Annotation(二)上
注解Annotation 很强大,很重要,是不是想要自己能够编写注解呢? 接下来,我们就可以尝试编写简单的注解了。
自定义Annotation(二)上
|
Java 开发者
自定义 Annotation | 学习笔记
快速学习 自定义 Annotation
|
前端开发 安全 Java
Java注解(Annotation):请不要小看我!(1)
Java注解(Annotation):请不要小看我!
99 0
Java注解(Annotation):请不要小看我!(1)
|
前端开发 Java 编译器
Java注解(Annotation):请不要小看我!(2)
Java注解(Annotation):请不要小看我!
117 0
Annotation-自定义注解入门
Annotation-自定义注解入门
110 0