java的注解解释

    xiaoxiao2025-08-09  7

     @interface用来声明一个注解,其中的每一个方法实际上是声明了一个配置参数。方法的名称就是参数的名称,返回值类型就是参数的类型(返回值类型只能是基本类型、Class、String、enum)。可以通过default来声明参数的默认值。

    四个元注解分别是:@Target,@Retention,@Documented,@Inherited ,再次强调下元注解是java API提供,是专门用来定义注解的注解,其作用分别如下:       @Target 表示该注解用于什么地方,可能的值在枚举类 ElemenetType 中,包括:            ElemenetType.CONSTRUCTOR----------------------------构造器声明            ElemenetType.FIELD --------------------------------------域声明(包括 enum 实例)            ElemenetType.LOCAL_VARIABLE------------------------- 局部变量声明            ElemenetType.METHOD ----------------------------------方法声明            ElemenetType.PACKAGE --------------------------------- 包声明            ElemenetType.PARAMETER ------------------------------参数声明            ElemenetType.TYPE--------------------------------------- 类,接口(包括注解类型)或enum声明                   @Retention 表示在什么级别保存该注解信息。可选的参数值在枚举类型 RetentionPolicy 中,包括:            RetentionPolicy.SOURCE ---------------------------------注解将被编译器丢弃            RetentionPolicy.CLASS -----------------------------------注解在class文件中可用,但会被VM丢弃            RetentionPolicy.RUNTIME VM-------将在运行期也保留注释,因此可以通过反射机制读取注解的信息。                    @Documented 将此注解包含在 javadoc 中 ,它代表着此注解会被javadoc工具提取成文档。在doc文档中的内容会因为此注解的信息内容不同而不同。相当与@see,@param 等。               @Inherited 允许子类继承父类中的注解。 简单演示下如何使用: package com.tmser.annotation;   import java.lang.annotation.ElementType; import java.lang.annotation.Target;   @Target(ElementType.PACKAGE) @Retention(RetentionPolicy.RUNTIME)   public @interface TestA {   } 下面改下我们的测试类: package com.tmser.annotation;   import java.util.HashMap; import java.util.Map;     @TestA(name="type",gid=Long.class) //类成员注解 public class UserAnnotation { @TestA(name="param",id=1,gid=Long.class) //类成员注解 private Integer age; @TestA (name="construct",id=2,gid=Long.class)//构造方法注解 public UserAnnotation(){ } @TestA(name="public method",id=3,gid=Long.class) //类方法注解 public void a(){ Map m = new HashMap(0); } @TestA(name="protected method",id=4,gid=Long.class) //类方法注解 protected void b(){ Map m = new HashMap(0); } @TestA(name="private method",id=5,gid=Long.class) //类方法注解 private void c(){ Map m = new HashMap(0); } public void b(Integer a){  } } 下面到了最重要的一步了,就是如何读取我们在类中定义的注解。只要读取出来了使用的话就简单了。 package com.tmser.annotation;   import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.Method;   public class ParseAnnotation {   public static void parseTypeAnnotation() throws ClassNotFoundException {           Class clazz = Class.forName("com.tmser.annotation.UserAnnotation");                   Annotation[] annotations = clazz.getAnnotations();           for (Annotation annotation : annotations) {           TestA testA = (TestA)annotation;             System.out.println("id= ""+testA.id()+""; name= ""+testA.name()+""; gid = "+testA.gid());           }       }  public static void parseMethodAnnotation(){ Method[] methods = UserAnnotation.class.getDeclaredMethods();           for (Method method : methods) {                             boolean hasAnnotation = method.isAnnotationPresent(TestA.class);               if (hasAnnotation) {                                 TestA annotation = method.getAnnotation(TestA.class);                   System.out.println("method = " + method.getName()                           + " ; id = " + annotation.id() + " ; description = "                           + annotation.name() + "; gid= "+annotation.gid());               }           }   } public static void parseConstructAnnotation(){ Constructor[] constructors = UserAnnotation.class.getConstructors();           for (Constructor constructor : constructors) {                        boolean hasAnnotation = constructor.isAnnotationPresent(TestA.class);               if (hasAnnotation) {                                 TestA annotation =(TestA) constructor.getAnnotation(TestA.class);                   System.out.println("constructor = " + constructor.getName()                           + " ; id = " + annotation.id() + " ; description = "                           + annotation.name() + "; gid= "+annotation.gid());               }           }   } public static void main(String[] args) throws ClassNotFoundException { parseTypeAnnotation(); parseMethodAnnotation(); parseConstructAnnotation(); } } 先别说话,运行: id= "0"; name= "type"; gid = class java.lang.Long method = c ; id = 5 ; description = private method; gid= class java.lang.Long method = a ; id = 3 ; description = public method; gid= class java.lang.Long method = b ; id = 4 ; description = protected method; gid= class java.lang.Long constructor = com.tmser.annotation.UserAnnotation ; id = 2 ; description = construct; gid= class java.lang.Long 看到了吧,我们定义的注解都完整的输出了,你要使用哪个,直接拿去用就好了。

    转载请注明原文地址: https://ju.6miu.com/read-1301580.html
    最新回复(0)