Java注解Annotation详解

来源:互联网 发布:非凡软件站电脑 编辑:程序博客网 时间:2024/05/16 09:54

Annotation的声明方式:

Annotation类型那个声明于一般的接口声明极为相似,只是其关键字为@interface,其属性必须带有小括号,其实更像定义方法,下面文章以属性称之。

常见的annotation注解有下面几种:

  • @Override: 用在方法,说明这个方法打算重写父类中的另一个方法的声明。
  • @Deprecated: 用于所有元素,说明该方法或属性等已经过时,不建议使用,编译器会产生警告信息,通常是因为它很危险或存在更好的选择。
  • @SuppressWarnings: 取消显示指定的编译器警告。

在自定义的annotation注解中,主要用到以下这些annotation注解:

  • @Retention: 表示注释类型的注释要保留多久。其中的RetentionPolicy共有三种策略,分别是:
  1. @SOURCE: 这个Annotation类型的信息只会保存在程序源码中,源码如果经过了编译之后,Annotation的数据就会消失,并不会保存在编译好的.class二进制文件中。
  2. @CLASS: 这个Annotation类型的信息保留在程序源码中,同时也会保存在编译好的.class文件中,在执行的时候并不会加载到JVM中。(默认)
  3. @RUNTIME: 表示在源码、编译好的.class文件中保存信息,在执行的时候会把这些信息加载到JVM中,这样可以使用反射将其信息获取出来。
  • @Target: 表示注释类型所使用的程序元素的种类。不声明则可以用在任一程序元素中。其中ElementType程序元素类型提供了Java程序中声明的元素的简单分类:
  1. ANNOTATION_TYPE: 注释类型
  2. CONSTRUCTOR: 构造方法
  3. FIELD: 字段(包括枚举常量)
  4. LOCAL_VARIABLE: 局部变量
  5. METHOD: 方法
  6. PACKAGE: 包
  7. PARAMETER: 参数
  8. TYPE: 类Class、接口Interface(包括注释类型Annotation)或枚举Enum
  • @Documented: 表示某一类型的注释将通过javadoc和类似的默认工具进行文档化,文档化时其注释部分将成为注释元素的公共API的一部分。


下面通过一例子解释一下Annotation:


MyAnnotation.java

[java] view plaincopyprint?
  1. import java.lang.annotation.ElementType;  
  2. import java.lang.annotation.Retention;  
  3. import java.lang.annotation.RetentionPolicy;  
  4. import java.lang.annotation.Target;  
  5.   
  6. /** 
  7.  * 自定义Annotation 
  8.  * @author WalkingDog 
  9.  * 
  10.  */  
  11.   
  12. @Target(value = {ElementType.TYPE, ElementType.FIELD,  
  13.         ElementType.METHOD, ElementType.PARAMETER})  
  14.   
  15. @Retention(RetentionPolicy.RUNTIME)  
  16. public @interface MyAnnotation {  
  17.       
  18.     //成员参数,其修饰符只有public、默认(default)。  
  19.     //参数成员只能用八种基本类型和String、Enum、Class、annotation等数据类型及其数组形式、  
  20.     //当自定义annotation中只有一个参数时,最好将参数名定义为value,因为当参数名为value时,在使用注解的时候可以不指定参数名称而直接赋值即可。  
  21.     //例如Annotation(value = "walkingdog")相当于Annotation("walkingdog")  
  22.     String[] value() default "Walkingdog";  
  23.       
  24.     Sex sex();  
  25. }  
  26.   
  27. enum Sex{  
  28.     MALE, FEMALE;  
  29. }  

AnnotationDemo.java
[java] view plaincopyprint?
  1. @MyAnnotation(sex = Sex.MALE, value = "Hello Walkingdog")  
  2. public class AnnotationDemo {  
  3.       
  4.     @MyAnnotation(sex = Sex.MALE)  
  5.     public void say(){  
  6.           
  7.         System.out.println("Walkingdog");  
  8.           
  9.     }  
  10. }  

AnnotationTest.java
[java] view plaincopyprint?
  1. import java.lang.annotation.Annotation;  
  2. import java.lang.reflect.Method;  
  3.   
  4. public class AnnotationTest {  
  5.       
  6.     public static void main(String[] args) throws Exception {  
  7.           
  8.         AnnotationDemo demo = new AnnotationDemo();  
  9.           
  10.         Class<?> clazz = demo.getClass();  
  11.           
  12.         //判断AnnotationDemo时候用了MyAnnotation的注解  
  13.         if(clazz.isAnnotationPresent(MyAnnotation.class)){  
  14.               
  15.             //返回MyAnnotation类型的注解  
  16.             MyAnnotation annotation = clazz.getAnnotation(MyAnnotation.class);  
  17.               
  18.             System.out.println(annotation.value()[0]);  
  19.               
  20.         }  
  21.           
  22.         Method method = clazz.getDeclaredMethod("say"new Class[]{});  
  23.           
  24.         Annotation[] annotations = method.getAnnotations();  
  25.           
  26.         for(Annotation annotation : annotations){  
  27.               
  28.             System.out.println(annotation.annotationType().getName());  
  29.               
  30.         }  
  31.           
  32.     }  
  33. }  

打印结果:
Hello Walkingdog

MyAnnotation

原创粉丝点击