什么是JAVA Annotation
Java 从1.5开始提供了 Annotation (注释,标注),它用来d修饰应用程序的zhi元素(类,方法,daozhuan性,参数,本地变量,包、元数据),编译器将其与元数据一同存储在 class 文件中,运行期间通过 Java 的反射来处理对其修饰元素的访问。Annotation 仅仅用来修饰元素,而不能影响代码的执行。只有通过其配套的框架或工具才能对其信息进行访问和处理。
@interface 是用来修饰 Annotation 的,请注意,它不是 interface。这个关键字声明隐含了一个信息:它是继承了 java.lang.annotation.Annotation 接口,而不是声明了一个 interface。
例如,我们创建一个LogAnnotation的自定义注解类:
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAnnotation {
String module() default "";
}
@interface意思是声明一个注解,方法名对应参数名,返回值类型对应参数类型。
@Target
@Target注解用于定义注解的使用位置,如果没有该项,表示注解可以用于任何地方。@Target的格式为:
@Target的ElementType取值有以下类型:
// 单参数
@Target({ ElementType.METHOD })
// 多参数
@Target(value = {ElementType.METHOD,ElementType.TYPE})
TYPE:类,接口或者枚举
FIELD:域,包含枚举常量
METHOD:方法
PARAMETER:参数
CONSTRUCTOR:构造方法
LOCAL_VARIABLE:局部变量
ANNOTATION_TYPE:注解类型
PACKAGE:包
@Retention
@Retention注解用于指明修饰的注解的生存周期,即会保留到哪个阶段。格式为:`@Retention(RetentionPolicy.RUNTIME)
RetentionPolicy的取值包含以下三种:
SOURCE:源码级别保留,编译后即丢弃。
CLASS:编译级别保留,编译后的class文件中存在,在jvm运行时丢弃,这是默认值。
RUNTIME:运行级别保留,编译后的class文件中存在,在jvm运行时保留,可以被反射调用。
@Documented
指明修饰的注解,可以被例如javadoc此类的工具文档化,只负责标记,没有成员取值。
@Inherited
@Inherited注解用于标注一个父类的注解是否可以被子类继承,如果一个注解需要被其子类所继承,则在声明时直接使用@Inherited注解即可。如果没有写此注解,则无法被子类继承。下面做一个测试:
//自定义一个注解
@interface MyAnnoation
{
public String key() default "key1";
public String value() default "value1";
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
//如果父类使用了HeritedApplication注解,则子类应该继承
@Inherited
@MyAnnoation
@interface HeritedApplication {
}
//父类使用了@HeritedApplication注解
@HeritedApplication
class Person {
}
class Student extends Person{
}
class AnnotationInherited{
public static void main(String[] args) throws Exception
{
Class clazz = Student.class;
//Student类是否有@HeritedApplication
if(clazz.isAnnotationPresent(HeritedApplication.class)){
System.out.println("true");
}
}
}