自定义注解的定义与使用

    科技2022-07-14  133

    注解的定义

    **注解是一种用来修饰类、方法、变量、参数、包且不会对所修饰的代码产生直接影响的元数据形式。**属于java的一种数据类型,和类、接口、数组、枚举类似。

    自定义注解并应用的步骤

    定义——自定义标记配置——把标记打在需要用到的程序代码中解析——在编译期或运行时检测到标记,并进行特殊操作

    基本语法

    注解在Java中,与类、接口、枚举类似,因此其声明语法基本一致,只是所使用的关键字有所不同@interface。在底层实现上,所有定义的注解都会自动继承java.lang.annotation.Annotation接口。

    根据我们在自定义类的经验,在类的实现部分无非就是书写构造、属性或方法。但是,在自定义注解中,其实现部分只能定义一个东西:注解类型元素(annotation type element)。

    public @interface CherryAnnotation { public String name(); int age() default 18; int[] array(); }

    注意:

    访问修饰符必须为public,不写默认为public。该元素的类型只能是基本数据类型、String、Class、枚举类型、注解类型(体现了注解的嵌套效果)以及上述类型的一位数组。该元素的名称一般定义为名词,如果注解中只有一个元素,请把名字起为value(后面使用会带来便利操作)。()不是定义方法参数的地方,也不能在括号中定义任何参数,仅仅只是一个特殊的语法。default代表默认值,值必须和第2点定义的类型一致。如果没有默认值,后续使用注解时必须给该类型元素赋值。

    常用元注解

    元注解即专门修饰注解的注解。

    @Target

    @Target注解,是专门用来限定某个自定义注解能够被应用在哪些Java元素上面的。它使用一个枚举类型定义如下:

    public enum ElementType { /** 类,接口(包括注解类型)或枚举的声明 */ TYPE, /** 属性的声明 */ FIELD, /** 方法的声明 */ METHOD, /** 方法形式参数声明 */ PARAMETER, /** 构造方法的声明 */ CONSTRUCTOR, /** 局部变量声明 */ LOCAL_VARIABLE, /** 注解类型声明 */ ANNOTATION_TYPE, /** 包的声明 */ PACKAGE }

    使用:

    //@CherryAnnotation被限定只能使用在类、接口或方法上面 @Target(value = {ElementType.TYPE,ElementType.METHOD}) public @interface CherryAnnotation { String name(); int age() default 18; int[] array(); }

    @Retention

    @Retention注解,翻译为持久力、保持力。即用来修饰自定义注解的生命力。 注解的生命周期有三个阶段:1、Java源文件阶段;2、编译到class文件阶段;3、【常用】运行期阶段。同样使用了RetentionPolicy枚举类型定义了三个阶段:

    public enum RetentionPolicy { /** * Annotations are to be discarded by the compiler. * (注解将被编译器忽略掉) */ SOURCE, /** * Annotations are to be recorded in the class file by the compiler * but need not be retained by the VM at run time. This is the default * behavior. * (注解将被编译器记录在class文件中,但在运行时不会被虚拟机保留,这是一个默认的行为) */ CLASS, /** * Annotations are to be recorded in the class file by the compiler and * retained by the VM at run time, so they may be read reflectively. * (注解将被编译器记录在class文件中,而且在运行时会被虚拟机保留,因此它们能通过反射被读取到) * @see java.lang.reflect.AnnotatedElement */ RUNTIME }

    @Documented

    @Documented注解,是被用来指定自定义注解是否能随着被定义的java文件生成到JavaDoc文档当中。

    @Inherited

    @Inherited注解,是指定某个自定义注解如果写在了父类的声明部分,那么子类的声明部分也能自动拥有该注解。@Inherited注解只对那些@Target被定义为ElementType.TYPE的自定义注解起作用。

    使用注解

    @Retention(RetentionPolicy.RUNTIME) @Target(value = {ElementType.METHOD}) @Documented public @interface CherryAnnotation { String name(); int age() default 18; int[] score(); } public class Student { @CherryAnnotation(name = "cherry-peng", age = 23, score = {99,66,77}) public void study(int times){ for(int i = 0; i < times; i++){ System.out.println("Good Good Study, Day Day Up!"); } } }

    特殊语法

    没有注解类型元素使用时可以不带 ()如果只有一个注解类型元素,且命名为value,那么使用时可以 @注解名(注解值)如果注解类型元素是一个数组类型,且只填了一个值,那么可以省略 {}如果一个注解的@Target是定义为Element.PACKAGE,那么这个注解是配置在package-info.java中的,而不能直接在某个类的package代码上面配置

    反射操作获取注解

    public class TestAnnotation { public static void main(String[] args){ try { // 获取Student的Class对象 Class stuClass = Class.forName("pojos.Student"); // 说明一下,这里形参不能写成Integer.class,应写为int.class Method stuMethod = stuClass.getMethod("study",int.class); if(stuMethod.isAnnotationPresent(CherryAnnotation.class)){ System.out.println("Student类上配置了CherryAnnotation注解!"); // 获取该元素上指定类型的注解 CherryAnnotation cherryAnnotation = stuMethod.getAnnotation(CherryAnnotation.class); System.out.println("name: " + cherryAnnotation.name() + ", age: " + cherryAnnotation.age() + ", score: " + cherryAnnotation.score()[0]); }else{ System.out.println("Student类上没有配置CherryAnnotation注解!"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } }

    注意:

    如果我们要获得的注解是配置在方法上的,那么我们要从Method对象上获取;如果是配置在属性上,就需要从该属性对应的Field对象上去获取,如果是配置在类型上,需要从Class对象上去获取。总之在谁身上,就从谁身上去获取!isAnnotationPresent(Class annotationClass)方法是专门判断该元素上是否配置有某个指定的注解;getAnnotation(Class annotationClass)方法是获取该元素上指定的注解。之后再调用该注解的注解类型元素方法就可以获得配置时的值数据;反射对象上还有一个方法getAnnotations(),该方法可以获得该对象身上配置的所有的注解。它会返回给我们一个注解数组,需要注意的是该数组的类型是Annotation类型,这个Annotation是一个来自于java.lang.annotation包的接口。
    Processed: 0.016, SQL: 8