Annotation 成为注解,注解是可以添加到Java源代码的语法元数据。类、方法、变量、参数、包均可以被注解,能够将信息元数据和程序元素关联。函数
@Document:是否保存到Javadoc文档中.net
@Retention:保留的时间(可选值:SOURCE:源码时;CLASS:编译时;RUNTIME:运行时)继承
@Target:能够用来修饰那些程序元素文档
@Inherited:是否能够被继承,默认为falseget
Annotation(注解)的做用:编译器
1.标记做用,用于告诉编译器一些信息源码
@Retention(RetentionPolicy.SOURCE)it
2.编译时动态处理,能够动态生成代码io
@Retention(RetentionPolicy.CLASS)编译
3.运行时动态处理,能够获得注解信息
@Retention(RetentionPolicy.RUNTIME)
使用注解:
1.运行时Annocation解析: @Retention(RetentionPolicy.RUNTIME)
method.getAnnotation(AnnotationName.class); //Annotation 注解的名称 返回注解信息 method.getAnnotations(); //返回目标的全部注解,一个Target能够有多个注解 method.isAnnotationPresent(AnnotationName.class); //表示该Target是否被某个Annotation修饰
getAnnotation(AnnotationName.class) 表示获得该target某个Annotation的信息,由于一个Target能够被多个Annotation修饰。getAnnotations()则表示获得该Target全部的Annotation
2.编译时Annotation解析 @Retentiom(RetentionPolicy.CLASS)
//Servlet3.0 @WebServlet注解