Java Annotation之应用篇 – 运行期动态解析annotation

来源:互联网 发布:潮流计算的软件 编辑:程序博客网 时间:2024/04/29 13:17

这样说来,annotation到底能起什么作用呢?
1,    编译工具或其他工具可以根据被附加在代码里的annotation信息自动生成配置文件或文档等外部文件。
比如,sun公司就提供了apt(Annotation Processing Tool)工具,apt工具是一个可以处理annotation的命令行工具,apt提供了在编译期针对源代码级别的解析,并可以在解析时生成新的源代码和其他文件,同时还可以对生成的源代码进行编译。
2,    其他程序可以在运行时动态解析将要被执行的程序里的annotation信息,并根据被附加的annotation信息来执行不同的操作。
比如,EJB3规范就比较广泛地使用了annotation特性。比如只要在POJO为class注明@Stateless注释,EJB容器便会根据此annotation把该POJO注册为无状态的Session Bean。EJB3使用了annotation大大地简化了EJB的开发和配置过程。我们会在其他文章里专门介绍EJB Annotation的原理与使用方法,这里不做详述。

本文通过一个简单地例子来说明怎么在运行期动态解析annotation。Apt工具的使用我们会在近期其他文章里对其加以介绍。

比如,我们定义了MyAnnotation3注释:
MyAnnotation3.java

  

Java代码  收藏代码
  1. package com.test.annotation;  
  2. import java.lang.annotation.Annotation;  
  3. import java.lang.annotation.Inherited;  
  4. import java.lang.annotation.Retention;  
  5. import java.lang.annotation.RetentionPolicy;  
  6. @Retention(RetentionPolicy.RUNTIME)  
  7. public @interface MyAnnotation3 {  
  8.  public String value();  
  9.  public String[] multiValues();  
  10.  int number() default 0;  
  11. }  

 

上面定义了一个名为MyAnnotation3的注释。
我们再定义一个GetMyAnnotation类,该类使用了MyAnnotation3注释:
GetMyAnnotation.java:

 

 

Java代码  收藏代码
  1. package com.test.annotation.test;  
  2.   
  3. import java.lang.annotation.Annotation;  
  4. import java.lang.reflect.Field;  
  5. import java.lang.reflect.Method;  
  6. import javax.ejb.EJB;  
  7. import javax.naming.InitialContext;  
  8. import javax.naming.NamingException;  
  9. import com.test.annotation.MyAnnotation3;  
  10.   
  11. // 为GetMyAnnotation类附加MyAnnotation3 注释     
  12. @MyAnnotation3(value = "Class GetMyAnnotation", multiValues = { "1", "2" })  
  13. public class GetMyAnnotation {  
  14.   
  15.     // 为testField1属性附加MyAnnotation3 注释  
  16.     @MyAnnotation3(value = "call testField1", multiValues = { "1" }, number = 1)  
  17.     private String testField1;  
  18.   
  19.     // 为testMethod1方法附加MyAnnotation3 注释  
  20.     @MyAnnotation3(value = "call testMethod1", multiValues = { "1", "2" }, number = 1)  
  21.     public void testMethod1() {  
  22.     }  
  23.   
  24.     @Deprecated  
  25.     @MyAnnotation3(value = "call testMethod2", multiValues = { "3", "4", "5" })  
  26.     public void testMethod2() {  
  27.     }  
  28. }  

  

上面的例子GetMyAnnotation非常简单,里面没有任何功能,但是分别为类(class),属性(field),方法(method)申明(附加)了MyAnnotation3 注释。
下面我们用程序TestMyAnnotation3对GetMyAnnotation里MyAnnotation3注释进行解析。
运行时解析annotation
TestMyAnnotation3.java

 

 

 

Java代码  收藏代码
  1. public class TestMyAnnotation3 {  
  2.   
  3.     public static void main(String[] args) {  
  4.   
  5.         System.out.println("--Class Annotations--");  
  6.   
  7.         if (GetMyAnnotation.class.isAnnotationPresent(MyAnnotation3.class)) {  
  8.             System.out.println("[GetMyAnnotation].annotation:");  
  9.   
  10.             MyAnnotation3 classAnnotation = GetMyAnnotation.class  
  11.                     .getAnnotation(MyAnnotation3.class);  
  12.             printMyAnnotation3(classAnnotation);  
  13.         }  
  14.         System.out.println("--Fields Annotations--");  
  15.   
  16.         Field[] fields = GetMyAnnotation.class.getDeclaredFields();  
  17.   
  18.         for (Field field : fields) {  
  19.   
  20.             if (field.isAnnotationPresent(MyAnnotation3.class)) {  
  21.   
  22.                 System.out.println("[GetMyAnnotation." + field.getName()  
  23.                         + "].annotation:");  
  24.   
  25.                 MyAnnotation3 fieldAnnotation = field  
  26.                         .getAnnotation(MyAnnotation3.class);  
  27.   
  28.                 printMyAnnotation3(fieldAnnotation);  
  29.             }  
  30.         }  
  31.         System.out.println("--Methods Annotations--");  
  32.   
  33.         Method[] methods = GetMyAnnotation.class.getDeclaredMethods();  
  34.   
  35.         for (Method method : methods) {  
  36.   
  37.             System.out.println("[GetMyAnnotation." + method.getName()  
  38.                     + "].annotation:");  
  39.   
  40.             if (method.isAnnotationPresent(MyAnnotation3.class)) {  
  41.   
  42.                 MyAnnotation3 methodAnnotation = method  
  43.                         .getAnnotation(MyAnnotation3.class);  
  44.   
  45.                 printMyAnnotation3(methodAnnotation);  
  46.             }  
  47.         }  
  48.     }  
  49.   
  50.     private static void printMyAnnotation3(MyAnnotation3 annotation3) {  
  51.   
  52.         if (annotation3 == null) {  
  53.             return;  
  54.         }  
  55.         System.out.println("{value=" + annotation3.value());  
  56.         String multiValues = "";  
  57.   
  58.         for (String value : annotation3.multiValues()) {  
  59.             multiValues += "," + value;  
  60.         }  
  61.   
  62.         System.out.println("multiValues=" + multiValues);  
  63.   
  64.         System.out.println("number=" + annotation3.number() + "}");  
  65.     }  
  66. }  

 

 

输出:

 

 

--Class Annotations--
[GetMyAnnotation].annotation:
{value=Class GetMyAnnotation
multiValues=,1,2
number=0}
--Fields Annotations--
[GetMyAnnotation.testField1].annotation:
{value=call testField1
multiValues=,1
number=1}
--Methods Annotations--
[GetMyAnnotation.testMethod1].annotation:
{value=call testMethod1
multiValues=,1,2
number=1}
[GetMyAnnotation.testMethod2].annotation:
{value=call testMethod2
multiValues=,3,4,5
number=0}

JDK1.5以后的版本提供的跟annotation有关的接口:

 

Java代码  收藏代码
  1. interface java.lang.reflect.AnnotatedElement {     
  2.   
  3.     boolean isAnnotationPresent(Class<? extends Annotation> annotationClass);     
  4.   
  5.    <T extends Annotation> T getAnnotation(Class<T> annotationClass);     
  6.   
  7.    Annotation[] getAnnotations();     
  8.   
  9.    Annotation[] getDeclaredAnnotations();     
  10.   
  11. }     

 

 

该接口主要用来取得附加在类(class),构造方法(constructor),属性(field),方法(method),包(package)上的annotation信息。
JDK1.5与此有关的几个类都实现了AnnotatedElement接口:  

 

所以可以利用反射(reflection)功能在程序里动态解析附加的annotation信息。
总结:
本文通过举例简单地说明了怎么动态解析annotation,大家可以举一反三,利用Java的annotation特性,完成更复杂功能等。    

 

 

原创粉丝点击