分享

J2EE第二十天??Java?Annotation注解

 静谧的知识园林 2012-08-02

J2EE第二十天

Java Annotation注解

Annotation 的由来:

Java5.0版发布以来,5.0平台提供了一个正式的annotation功能:允许开发者定义、使用自己的annotation类型。此功能由一个定义annotation类型的语法和一个描述annotation声明的语法,读取annotationAPI,一个使用annotation修饰的class文件,一个annotation处理工具(apt)组成。

Annotation可以从源文件、class文件或者正在运行时反射的多种方式被读取。

JDK5内建的Annotation:

Override

Deprecated

SuppressWarnings

自定义一个 Annotation 类型:

使用@interface自行定义Annotation型态时,实际上是自动继承了java.lang.annotation.Annotation接口

由编译程序自动为您完成其它产生的细节

在定义Annotation型态时,不能继承其它的Annotation型态或是接口

告知编译程序如何处理@Retention

在使用Retention型态时,需要提供java.lang.annotation.RetentionPolicy的枚举型态

注解的三种形态:

     SOURCE, //编译程序处理完Annotation信息后就完成任务

     CLASS,  //编译程序将Annotation储存于class档中,缺省

     RUNTIME //编译程序将Annotation储存于class檔中,可由VM读入

 

package java.lang.annotation;

public enum RetentionPolicy

 {

     SOURCE, //编译程序处理完Annotation信息后就完成任务

     CLASS,  //编译程序将Annotation储存于class档中,缺省

     RUNTIME //编译程序将Annotation储存于class檔中,可由VM读入

}

java.lang.reflect.AnnotatedElement接口

public Annotation getAnnotation(Class annotationType);

public Annotation[] getAnnotations();

public Annotation[] getDeclaredAnnotations();

public boolean isAnnotationPresent(Class annotationType);

ClassConstructorFieldMethodPackage等类别,都实现了AnnotatedElement接口

 

 

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多