H__D  

java注解概念

  Java提供了一种原程序中的元素关联任何信息和任何数据的途径和方法

java注解介绍

  常用注解

  @Override:表示方法是重写的方法

  @Deprecated:过时的方法

  @SuppressWarnings:抑制警告

  注解分类

  按照运行机制分

  1、源码注解:注解只在源码中存在,编译成.class文件就不存在了

  2、编译时注解:注解在源码和.class文件中都存在,如@Override

  3、运行时注解:在运行阶段起作用,甚至会影响运行逻辑,如@Autowired

  按照来源分

  1、JDK自带注解

  2、第三方注解

  3、自定义注解

自定义注解和使用

  1、使用@interface关键定义注解(Description.java),如下:

 1 package com.hd;
 2 
 3 import java.lang.annotation.Documented;
 4 import java.lang.annotation.ElementType;
 5 import java.lang.annotation.Inherited;
 6 import java.lang.annotation.Retention;
 7 import java.lang.annotation.RetentionPolicy;
 8 import java.lang.annotation.Target;
 9 
10 /**
11  * 注解的注解:元注解
12  * 
13  * 注解作用域
14     ElementType.TYPE:允许被修饰的注解作用在类、接口和枚举上
15     ElementType.FIELD:允许作用在属性字段上
16     ElementType.METHOD:允许作用在方法上
17     ElementType.PARAMETER:允许作用在方法参数上
18     ElementType.CONSTRUCTOR:允许作用在构造器上
19     ElementType.LOCAL_VARIABLE:允许作用在本地局部变量上
20     ElementType.ANNOTATION_TYPE:允许作用在注解上
21     ElementType.PACKAGE:允许作用在包上
22     
23     注解的生命周期
24     RetentionPolicy.SOURCE:当前注解编译期可见,不会写入 class 文件
25     RetentionPolicy.CLASS:类加载阶段丢弃,会写入 class 文件
26     RetentionPolicy.RUNTIME:永久保存,可以反射获取
27  *
28  */
29 // 注解的作用域
30 @Target({ElementType.METHOD, ElementType.TYPE})
31 // 注解的生命周期
32 @Retention(RetentionPolicy.RUNTIME)
33 // 允许子类继承
34 @Inherited
35 // 生成javadoc的时候生成注解的信息
36 @Documented
37 // @interface:使用@interface关键定义注解
38 public @interface Description {
39     
40     // 注解的成员
41     // 成员类型所限的,合法的类型包括原始数据类型及String、Class、Annotation、Enumeration
42     String desc();
43     
44     String author();
45     
46     // 成员以无参无异常方式生命,可以用default为成员指定一个默认值
47     int age() default 18;
48     
49     // 如果注解成员只有一个时,成员名必须取名未value(),在使用时可以忽略成员名和赋值号(=)
50     // 注解可以没有成员,没有成员的注解成为标识注解
51 }

  2、在普通类上使用注解,使用方法

    注解使用: @<注解名>(<成员名1>=<成员值1>, <成员名2>=<成员值2>, ...)

    如下:

 1  package com.hd;
 2 
 3  /**
 4   * 注解使用:
 5   * @<注解名>(<成员名1>=<成员值1>, <成员名2>=<成员值2>, ...)
 6   * @author H__D
 7   * @date 2019-07-09 22:49:32
 8   *
 9   */
10 @Description(desc="I am class annotation", author="hd")
11 public class TestDescription {
12 
13     @Description(desc="I am method annotation", author="hd")
14     public String test(){
15         
16         return "red";
17     }
18     
19 
20 }

  3、解析注解,通过反射获取类,函数或成员上的运行时注解信息,从而实现动态控制程序运行的逻辑

 1 package com.hd;
 2 
 3 import java.lang.annotation.Annotation;
 4 import java.lang.reflect.Method;
 5 
 6 /**
 7  * 解析注解
 8  * 通过反射获取类,函数或成员上的运行时注解信息,从而实现动态控制程序运行的逻辑
 9  * 
10  * 对于一个类或者接口来说,Class 类中提供了以下一些方法用于反射注解。
11     getAnnotation:返回指定的注解
12     isAnnotationPresent:判定当前元素是否被指定注解修饰
13     getAnnotations:返回所有的注解
14     getDeclaredAnnotation:返回本元素的指定注解
15     getDeclaredAnnotations:返回本元素的所有注解,不包含父类继承而来的
16  * 
17  * @author H__D
18  * @date 2019-07-09 22:52:42
19  *
20  */
21 public class ParseDecription {
22     
23     public static void main(String[] args) {
24         // TODO Auto-generated method stub
25         // 1、使用类加载器加载类
26         try {
27             Class c = Class.forName("com.hd.TestDescription");
28             System.out.println(c);
29             
30             // 2、找到类上面的注解
31             boolean isExist = c.isAnnotationPresent(Description.class);
32             
33             if(isExist) {
34                 // 3、拿到注解实例
35                 Description d = (Description) c.getAnnotation(Description.class);
36                 System.out.println("========parse class annotation=========");
37                 System.out.println("desc = " + d.desc());
38                 System.out.println("author = " + d.author());
39                 System.out.println("age = " + d.age());
40             }
41             
42             // 4、找到方法上的注解
43             Method[] ms = c.getMethods();
44             for (Method m : ms) {
45                 boolean isMExist = m.isAnnotationPresent(Description.class);
46                 if(isMExist) {
47                     Description d = m.getAnnotation(Description.class);
48                     System.out.println("========parse method annotation=========");
49                     System.out.println("desc = " + d.desc());
50                     System.out.println("author = " + d.author());
51                     System.out.println("age = " + d.age());
52                 }
53             }
54             
55             // 另外一种解析方法
56             for (Method m : ms) {
57                 Annotation[] annotations = m.getAnnotations();
58                 for (Annotation annotation : annotations) {
59                     if(annotation instanceof Description) {
60                         System.out.println("========parse method annotation other way=========");
61                         Description d = (Description) annotation;
62                         System.out.println("desc = " + d.desc());
63                         System.out.println("author = " + d.author());
64                         System.out.println("age = " + d.age());
65                     }
66                 }
67             }
68             
69         } catch (ClassNotFoundException e) {
70             // TODO Auto-generated catch block
71             e.printStackTrace();
72         }
73     }
74 }

  4、运行结果如下:

    

  

posted on 2019-07-09 23:33  H__D  阅读(1827)  评论(0编辑  收藏  举报