java注解应用实例 - Annotation, 自定义注解, 注解类规则

本文介绍了java的自定义注解及注解类编写的规则, 并通过实例来说明下如何使用java的注解. 实例演示了注解在类,构造方法,方法和字段的使用. 可以从这里下载到完成的工程代码: http://dl.iteye.com/topics/download/f74972df-234f-30c9-aadd-ca2ed1376bc2

自定义注解类编写的一些规则:

1. Annotation型定义为@interface, 所有的Annotation会自动继承java.lang.Annotation这一接口,并且不能再去继承别的类或是接口.

2. 参数成员只能用public或默认(default)这两个访问权修饰

3. 参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和String、Enum、Class、annotations等数据类型,以及这一些类型的数组.

4. 要获取类方法和字段的注解信息,必须通过Java的反射技术来获取 Annotation对象,因为你除此之外没有别的获取注解对象的方法

5. 注解也可以没有定义成员, 不过这样注解就没啥用了

自定义注解类时, 可以指定目标 (类、方法、字段, 构造函数等) , 注解的生命周期(运行时,class文件或者源码中有效), 是否将注解包含在javadoc中及是否允许子类继承父类中的注解, 具体如下:

1. @Target 表示该注解目标,可能的 ElemenetType 参数包括:

ElemenetType.CONSTRUCTOR 构造器声明
ElemenetType.FIELD 域声明(包括 enum 实例)
ElemenetType.LOCAL_VARIABLE 局部变量声明
ElemenetType.METHOD 方法声明
ElemenetType.PACKAGE 包声明
ElemenetType.PARAMETER 参数声明
ElemenetType.TYPE 类,接口(包括注解类型)或enum声明

2. @Retention 表示该注解的生命周期,可选的 RetentionPolicy 参数包括

RetentionPolicy.SOURCE 注解将被编译器丢弃
RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃
RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息

3. @Documented 指示将此注解包含在 javadoc 中

4.  @Inherited 指示允许子类继承父类中的注解

好, 该介绍的介绍了, 看下自定义的注解应用实例:

1. 首先看下定义的注解类:

类注解定义, MyClassAnnotation.java:

[java] view plain copy
  1. package  com.ross.annotation;  
  2. import  java.lang.annotation.*;  
  3. /**  
  4.  * Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com  
  5.  * Date: 2012-1-29  
  6.  * Since: MyJavaExpert v1.0  
  7.  * Description: class annotation  
  8.  */   
  9. @Retention (RetentionPolicy.RUNTIME)   
  10. @Target (ElementType.TYPE)   
  11. public   @interface  MyClassAnnotation   
  12. {  
  13.     String uri();  
  14.     String desc();  
  15. }  
默认构造方法注解定义,MyConstructorAnnotation.java: 
[java] view plain copy
  1. package  com.ross.annotation;  
  2. import  java.lang.annotation.ElementType;  
  3. import  java.lang.annotation.Retention;  
  4. import  java.lang.annotation.RetentionPolicy;  
  5. import  java.lang.annotation.Target;  
  6. /**  
  7.  * Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com  
  8.  * Date: 2012-1-29  
  9.  * Since: MyJavaExpert v1.0  
  10.  * Description: Constructor annotation  
  11.  */   
  12. @Retention (RetentionPolicy.RUNTIME)   
  13. @Target (ElementType.CONSTRUCTOR)   
  14. public   @interface  MyConstructorAnnotation   
  15. {  
  16.     String uri();  
  17.     String desc();  
  18. }  
方法注解定义,MyMethodAnnotation.java:
[java] view plain copy
  1. package  com.ross.annotation;  
  2. import  java.lang.annotation.ElementType;  
  3. import  java.lang.annotation.Retention;  
  4. import  java.lang.annotation.RetentionPolicy;  
  5. import  java.lang.annotation.Target;  
  6. /**  
  7.  * Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com  
  8.  * Date: 2012-1-29  
  9.  * Since: MyJavaExpert v1.0  
  10.  * Description: method annotation  
  11.  */   
  12. @Retention (RetentionPolicy.RUNTIME)   
  13. @Target (ElementType.METHOD)   
  14. public   @interface  MyMethodAnnotation   
  15. {  
  16.     String uri();  
  17.     String desc();  
  18. }  
字段注解定义, MyFieldAnnotation.java:
[java] view plain copy
  1. package  com.ross.annotation;  
  2. import  java.lang.annotation.ElementType;  
  3. import  java.lang.annotation.Retention;  
  4. import  java.lang.annotation.RetentionPolicy;  
  5. import  java.lang.annotation.Target;  
  6. /**  
  7.  * Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com  
  8.  * Date: 2012-1-29  
  9.  * Since: MyJavaExpert v1.0  
  10.  * Description: field annotation  
  11.  */   
  12. @Retention (RetentionPolicy.RUNTIME)   
  13. @Target (ElementType.FIELD)   
  14. public   @interface  MyFieldAnnotation   
  15. {  
  16.     String uri();  
  17.     String desc();  
  18. }  
2. 再看下我们注解的应用和测试:

在类上面使用了MyClassAnnotation注解, 默认构造方法上使用了MyConstructorAnnotation注解,  自定义方法上使用了MyMethodAnnotation注解, 自定义字段上使用了MyFieldAnnotation注解, 在Mail函数中则实现了访问这些注解,并打印注解信息.

MySample.java:

[java] view plain copy
  1. package  com.ross.annotation;  
  2. import  java.lang.reflect.*;  
  3. /**  
  4.  * Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com  
  5.  * Date: 2012-1-29  
  6.  * Since: MyJavaExpert v1.0  
  7.  * Description: This class is used to show how to use the annotation of each level  
  8.  */   
  9. @MyClassAnnotation (uri =  "com.ross.MySample" , desc =  "The class name" )  
  10. public   class  MySample  
  11. {  
  12.     @MyFieldAnnotation (uri =  "com.ross.MySample#id" , desc =  "The class field" )  
  13.     public  String id;  
  14.   
  15.     /**  
  16.      * Description: default constructor  
  17.      */   
  18.     @MyConstructorAnnotation (uri =  "com.ross.MySample#MySample" , desc =  "The default constuctor" )  
  19.     public  MySample()  
  20.     {  
  21.     }  
  22.   
  23.     /**  
  24.      * Description: normal method  
  25.      */   
  26.     @MyMethodAnnotation (uri =  "com.ross.MySample#setId" , desc =  "The class method" )  
  27.     public   void  setId(String id)  
  28.     {  
  29.         this .id = id;  
  30.     }  
  31.   
  32.     /**  
  33.      * Description: MyAnnotation test  
  34.      * @throws NoSuchMethodException   
  35.      * @throws SecurityException   
  36.      * @throws NoSuchFieldException   
  37.      */   
  38.     public   static   void  main(String[] args)  throws  SecurityException,  
  39.             NoSuchMethodException, NoSuchFieldException  
  40.     {  
  41.         MySample oMySample = new  MySample();  
  42.         // get class annotation   
  43.         MyClassAnnotation oMyAnnotation = MySample.class   
  44.                 .getAnnotation(MyClassAnnotation.class );  
  45.         System.out.println("Class's uri: "  + oMyAnnotation.uri() +  "; desc: "   
  46.                 + oMyAnnotation.desc());  
  47.   
  48.         // get constructor annotation   
  49.         Constructor oConstructor = oMySample.getClass().getConstructor();  
  50.         MyConstructorAnnotation oMyConstructorAnnotation = (MyConstructorAnnotation) oConstructor  
  51.                 .getAnnotation(MyConstructorAnnotation.class );  
  52.         System.out.println("Constructor's uri: "   
  53.                 + oMyConstructorAnnotation.uri() + "; desc: "   
  54.                 + oMyConstructorAnnotation.desc());  
  55.   
  56.         // get method annotation   
  57.         Method oMethod = oMySample.getClass().getDeclaredMethod("setId" ,String. class );  
  58.         MyMethodAnnotation oMyMethodAnnotation = oMethod  
  59.                 .getAnnotation(MyMethodAnnotation.class );  
  60.         System.out.println("Method's uri: "  + oMyMethodAnnotation.uri()  
  61.                 + "; desc: "  + oMyMethodAnnotation.desc());  
  62.   
  63.         // get field annotation   
  64.         Field oField = oMySample.getClass().getDeclaredField("id" );  
  65.         MyFieldAnnotation oMyFieldAnnotation = oField  
  66.                 .getAnnotation(MyFieldAnnotation.class );  
  67.         System.out.println("Field's uri: "  + oMyFieldAnnotation.uri()  
  68.                 + "; desc: "  + oMyFieldAnnotation.desc());  
  69.   
  70.     }  
  71.   
  72. }  

控制台打印结果:

[plain] view plain copy
  1. Class's uri: com.ross.MySample; desc: The class name  
  2. Constructor's uri: com.ross.MySample#MySample; desc: The default constuctor  
  3. Method's uri: com.ross.MySample#setId; desc: The class method  
  4. Field's uri: com.ross.MySample#id; desc: The class field  

至此本实例就完成了, 其实就是抓住两点一个是定义注解类,另外一个是如何访问注解, 就算是学会了.

注: 转载请注明出处: http://hejiangtao.iteye.com 用于商业得给我分成大笑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值