Java 1.5 Annotation Notes

java1.5 annotation Framework:

java.lang 包里面的几个默认的Annotation:

   @Override
   @Deprecated
   @SuppressWarnings

java.lang.annotation包里的用于自定于Annotation的几个Annotation:

   @Documented:表明某一类型的注释将通过javadoc和类似的默认工具进行文档化。
   @Inherited:允许子类继承父类中的注解。
   @Retention:表明需要在什么级别保存该注解信息。
   @Target:表明该注解可以用于什么地方。

javax.annotation包里的几个实用的Annotation:
   @Generated:该注解用于标记已生成的源代码,它可以用于区分单个文件中用户编写的代码和生成的代码。
   @PostConstruct:该注解用于在依赖关系注入完成之后需要执行的方法上,以执行任何初始化。
   @PreDestroy:该注解作为回调通知用于各方法,以表示该实例正处于被容器移除的过程中。
   @Resource:该注解用于标记应用程序所需要的资源。


java1.5 annotation分析:

   在jdk的java.lang.annotation包里,打开如下几个源文件:

   1、Target.java

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    ElementType[] value();
}  

   其中的@interface是一个关键字,在设计annotations的时候必须把一个类型定义为@interface,而不能用class或interface关键字

   2、Retention.java 

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    RetentionPolicy value();
}

   看到这里,大家可能都模糊了,都不知道在说什么,别急,往下看一下. 
   在上面的文件都用到了RetentionPolicy,ElementType这两个字段,你可能就会猜到这是两个java文件.的确,这两个文件的源代码如下: 
   
   3、源文件RetentionPolicy.java

public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

   这是一个enum类型,共有三个值,分别是SOURCE,CLASS 和 RUNTIME. 
   SOURCE代表的是这个Annotation类型的信息只会保留在程序源码里,源码如果经过了编译之后,Annotation的数据就会消失,并不会保留在编译好的.class文件里面。 
   ClASS的意思是这个Annotation类型的信息保留在程序源码里,同时也会保留在编译好的.class文件里面,在执行的时候,并不会把这一些信息加载到虚拟机(JVM)中去.注意一下,当你没有设定一个Annotation类型的Retention值时,系统默认值是CLASS. 
   RUNTIME,表示在源码、编译好的.class文件中保留信息,在执行的时候会把这一些信息加载到JVM中去的. 
举一个例子,如@Override里面的Retention设为SOURCE,编译成功了就不要这一些检查的信息;相反,@Deprecated里面的Retention设为RUNTIME,表示除了在编译时会警告我们使用了哪个被Deprecated的方法,在执行的时候也可以查出该方法是否被Deprecated.

   4、源文件ElementType.java

public enum ElementType {
    /** Class, interface (including annotation type), or enum declaration */
    TYPE,

    /** Field declaration (includes enum constants) */
    FIELD,

    /** Method declaration */
    METHOD,

    /** Parameter declaration */
    PARAMETER,

    /** Constructor declaration */
    CONSTRUCTOR,

    /** Local variable declaration */
    LOCAL_VARIABLE,

    /** Annotation type declaration */
    ANNOTATION_TYPE,

    /** Package declaration */
    PACKAGE
}
   @Target里面的ElementType是用来指定Annotation类型可以用在哪一些元素上的.
TYPE(类型)是指可以用在Class,Interface,Enum和Annotation类型上; 另外,从Target.java的源代码可以看出,@Target自己也用了自己来声明自己,只能用在ANNOTATION_TYPE之上。如果一个Annotation类型没有指明@Target使用在哪些元素上,那么它可以使用在任何元素之上,这里的元素指的是上面的八种类型. 例如:
   @Target(ElementType.METHOD) 
   @Target(value=ElementType.METHOD) 
   @Target(ElementType.METHOD,ElementType.CONSTRUCTOR)  
   上面的源文件中的@Documented的目的就是让这一个Annotation类型的信息能够显示在javaAPI说明文档上;没有添加的话,使用javadoc生成API文档的时候就会找不到这一个类型生成的信息。
   另外一点,如果需要把Annotation的数据继承给子类,那么就会用到@Inherited这一个Annotation类型。
   

   自定义一个简单的Annotation例子

   1、Description.java

package com.citi.crc;

import java.lang.annotation.Documented;   import java.lang.annotation.ElementType;   import java.lang.annotation.Retention;   import java.lang.annotation.RetentionPolicy;   import java.lang.annotation.Target;     @Target(ElementType.TYPE)   @Retention(RetentionPolicy.RUNTIME)   @Documented  public @interface Description {       String value();   }

   说明:所有的自定义的Annotation会自动继承java.lang.annotation.Annotation这一个接口,所以不能再去继承别的类或是接口. 
   最重要的一点,Annotation类型里面的参数该怎么设定: 
   第一,只能用public或默认(default)这两个访问权修饰.例如,String value();这里把方法设为defaul默认类型. 
   第二,参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和String,Enum,Class,annotations等数据类型,以及这一些类型的数组.例如,String value();这里的参数成员就为String. 
   第三,如果只有一个参数成员,最好把参数名称设为"value",后加小括号.例:上面的例子就只有一个参数成员. 

   2、Name.java

package com.citi.crc;

import java.lang.annotation.Documented;   import java.lang.annotation.ElementType;   import java.lang.annotation.Retention;   import java.lang.annotation.RetentionPolicy;   import java.lang.annotation.Target;     //注意这里的@Target与@Description里的不同,参数成员也不同   @Target(ElementType.METHOD)   @Retention(RetentionPolicy.RUNTIME)   @Documented  public @interface Entity {       String organization();       String author();   }


   3、UseAnnotation.java

package com.citi.crc;

@Description(value="citi is a good bank...") public class UseAnnotation {  @Entity(organization="Citicrop", author="Jensen, Chen")  public String getEntity() {   return null;  }    @Entity(organization="SPSP", author="ZJ")  public String getEntity2() {   return "Author is Jensen.";  }

}


   4、最后,写一个可以运行提取Test信息的类TestAnnotation

package com.citi.crc;

import java.lang.reflect.Method; import java.util.HashSet; import java.util.Set;

public class TestEntity {

 @SuppressWarnings({ "rawtypes", "unchecked" })  public static void main(String[] args) {   try {    Class clazz = Class.forName("com.citi.crc.UseAnnotation");    Method[] method = clazz.getMethods();    boolean flag = clazz.isAnnotationPresent(Description.class);    if (flag) {     Description des = (Description) clazz       .getAnnotation(Description.class);     System.out.println("Description:" + des.value());    }

   Set<Method> set = new HashSet<Method>();    for (int i = 0; i < method.length; i++) {     boolean otherFlag = method[i].isAnnotationPresent(Entity.class);     if (otherFlag)      set.add(method[i]);    }    for (Method m : set) {     Entity name = m.getAnnotation(Entity.class);     System.out.println("Organization:" + name.organization());     System.out.println("Author:" + name.author());    }   } catch (Exception e) {    e.printStackTrace();   }

 } }

    5、运行结果: 
Description:citi is a good bank...
Organization:SPSP
Author:ZJ
Organization:Citicrop
Author:Jensen, Chen

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值