详见地址:http://www.cnblogs.com/xdp-gacl/p/3622275.html
java用 @interface Annotation{ } 定义一个注解 @Annotation,一个注解是一个类
注解(Annotation)很重要,未来的开发模式都是基于注解的,JPA是基于注解的,Spring2.5以上都是基于注解的,Hibernate3.x以后也是基于注解的,现在的Struts2有一部分也是基于注解的了,注解是一种趋势,现在已经有不少的人开始用注解了,注解是JDK1.5之后才有的新特性
@Override,@Deprecated,@SuppressWarnings为常见的3个注解。
注解相当于一种标记,在程序中加上了注解就等于为程序加上了某种标记,标记可以加在包、类,属性、方法,方法的参数以及局部变量上
以后,JAVAC编译器,开发工具和其他程序可以用反射来了解你的类以及各种元素上有无任何标记,看你有什么标记,就去干相应的事
注解@Override用在方法上,当我们想重写一个方法时,在方法上加@Override,当我们方法的名字出错时,编译器就会报错,如图:
注解@Deprecated,用来表示某个类的属性或方法已经过时,不想别人再用时,在属性和方法
上用@Deprecated修饰,如图:
注解@SuppressWarnings用来压制程序中出来的警告,比如在没有用泛型或是方法已经过时的时候,
//这里就是注解,称为压缩警告,这是JDK内部自带的一个注解,一个注解就是一个类,在这里使用了这个注解就是创建了SuppressWarnings类的一个实例对象
如图:
注解@Retention可以用来修饰注解,是注解的注解,称为元注解
Retention注解说明
当在Java源程序上加了一个注解,这个Java源程序要由javac去编译,javac把java源文件编译成.class文件,在编译成class时可能会把Java源程序上的一些注解给去掉,java编译器(javac)在处理java源程序时,可能会认为这个注解没有用了,于是就把这个注解去掉了,那么此时在编译好的class中就找不到注解了, 这是编译器编译java源程序时对注解进行处理的第一种可能情况,假设java编译器在把java源程序编译成class时,没有把java源程序中的注解去掉,那么此时在编译好的class中就可以找到注解,当程序使用编译好的class文件时,需要用类加载器把class文件加载到内存中,class文件中的东西不是字节码,class文件里面的东西由类加载器加载到内存中去,类加载器在加载class文件时,会对class文件里面的东西进行处理,如安全检查,处理完以后得到的最终在内存中的二进制的东西才是字节码,类加载器在把class文件加载到内存中时也有转换,转换时是否把class文件中的注解保留下来,这也有说法,所以说一个注解的生命周期有三个阶段:java源文件是一个阶段,class文件是一个阶段,内存中的字节码是一个阶段,javac把java源文件编译成.class文件时,有可能去掉里面的注解,类加载器把.class文件加载到内存时也有可能去掉里面的注解,因此在自定义注解时就可以使用Retention注解指明自定义注解的生命周期,自定义注解的生命周期是在RetentionPolicy.SOURCE阶段(java源文件阶段),还是在RetentionPolicy.CLASS阶段(class文件阶段),或者是在RetentionPolicy.RUNTIME阶段(内存中的字节码运行时阶段),根据JDK提供的API可以知道默认是在RetentionPolicy.CLASS阶段 (JDK的API写到:the retention policy defaults to RetentionPolicy.CLASS.)
Retention注解有一个属性value,是RetentionPolicy类型的,Enum RetentionPolicy是一个枚举类型,
@Documented注解标记的元素,Javadoc工具会将此注解标记元素的注解信息包含在javadoc中。默认,注解信息不会包含在Javadoc中
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
/**
* Returns the retention policy.
* @return the retention policy
*/
RetentionPolicy value();
}
这个枚举决定了Retention注解的作用范围,也可理解为Rentention 搭配 RententionPolicy使用。RetentionPolicy有3个值:CLASS RUNTIME SOURCE
用@Retention(RetentionPolicy.CLASS)修饰的注解,表示注解的信息被保留在class文件(字节码文件)中当程序编译时,但不会被虚拟机读取在运行的时候;
用@Retention(RetentionPolicy.SOURCE )修饰的注解,表示注解的信息会被编译器抛弃,不会留在class文件中,注解的信息只会留在源文件中;
用@Retention(RetentionPolicy.RUNTIME )修饰的注解,表示注解的信息被保留在class文件(字节码文件)中当程序编译时,会被虚拟机保留在运行时,
所以他们可以用反射的方式读取。RetentionPolicy.RUNTIME 可以让你从JVM中读取Annotation注解的信息,以便在分析程序的时候使用.
package com.self; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface MyTarget { }
定义个一注解@MyTarget,用RetentionPolicy.RUNTIME修饰;
package com.self; import java.lang.reflect.Method; public class MyTargetTest { @MyTarget public void doSomething() { System.out.println("hello world"); } public static void main(String[] args) throws Exception { Method method = MyTargetTest.class.getMethod("doSomething",null); if(method.isAnnotationPresent(MyTarget.class))//如果doSomething方法上存在注解@MyTarget,则为true { System.out.println(method.getAnnotation(MyTarget.class)); } } }
上面程序打印:@com.self.MyTarget(),如果RetentionPolicy值不为RUNTIME,则不打印
@Retention(RetentionPolicy.SOURCE ) public @interface Override @Retention(RetentionPolicy.SOURCE ) public @interface SuppressWarnings @Retention(RetentionPolicy.RUNTIME ) public @interface Deprecated
由上可以看出,只有注解@Deprecated在运行时可以被JVM读取到
注解中可以定义属性,看例子:
@Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String hello() default "gege"; String world(); int[] array() default { 2, 4, 5, 6 }; EnumTest.TrafficLamp lamp() ; TestAnnotation lannotation() default @TestAnnotation(value = "ddd"); Class style() default String.class; }
上面程序中,定义一个注解@MyAnnotation,定义了6个属性,他们的名字为:
hello,world,array,lamp,lannotation,style.
- 属性hello类型为String,默认值为gege
- 属性world类型为String,没有默认值
- 属性array类型为数组,默认值为2,4,5,6
- 属性lamp类型为一个枚举,没有默认值
- 属性lannotation类型为注解,默认值为@TestAnnotation,注解里的属性是注解
- 属性style类型为Class,默认值为String类型的Class类型
看下面例子:定义了一个MyTest类,用注解@MyAnnotation修饰,注解@MyAnnotation定义的属性都赋了值
@MyAnnotation(hello = "beijing", world="shanghai",array={},lamp=TrafficLamp.RED,style=int.class) public class MyTest { @MyAnnotation(lannotation=@TestAnnotation(value="baby"), world = "shanghai",array={1,2,3},lamp=TrafficLamp.YELLOW) @Deprecated @SuppressWarnings("") public void output() { System.out.println("output something!"); } }
接着通过反射读取注解的信息:
public class MyReflection { public static void main(String[] args) throws Exception { MyTest myTest = new MyTest(); Class<MyTest> c = MyTest.class; Method method = c.getMethod("output", new Class[] {}); //如果MyTest类名上有注解@MyAnnotation修饰,则为true if(MyTest.class.isAnnotationPresent(MyAnnotation.class)) { System.out.println("have annotation"); } if (method.isAnnotationPresent(MyAnnotation.class)) { method.invoke(myTest, null); //调用output方法 //获取方法上注解@MyAnnotation的信息 MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class); String hello = myAnnotation.hello(); String world = myAnnotation.world(); System.out.println(hello + ", " + world);//打印属性hello和world的值 System.out.println(myAnnotation.array().length);//打印属性array数组的长度 System.out.println(myAnnotation.lannotation().value()); //打印属性lannotation的值 System.out.println(myAnnotation.style()); } //得到output方法上的所有注解,当然是被RetentionPolicy.RUNTIME修饰的 Annotation[] annotations = method.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation.annotationType().getName()); } } }
上面程序打印:
have annotation output something! gege, shanghai 3 baby class java.lang.String com.heima.annotation.MyAnnotation java.lang.Deprecated
如果注解中有一个属性名字叫value,则在应用时可以省略属性名字不写。
可见,@Retention(RetentionPolicy.RUNTIME )注解中,RetentionPolicy.RUNTIME是注解属性值,属性名字是value,
属性的返回类型是RetentionPolicy,如下:
public @interface MyTarget { String value(); }
可以这样用:
@MyTarget("aaa") public void doSomething() { System.out.println("hello world"); }
注解@Target也是用来修饰注解的元注解,它有一个属性ElementType也是枚举类型,
//Target注解决定MyAnnotation注解可以加在哪些成分上,如加在类身上,或者属性身上,或者方法身上等成分
@Target元注解
@Target元注解决定了一个注解可以标识到哪些成分上,如标识在在类身上,或者属性身上,或者方法身上等成分,@Target默认值为任何元素(成分)
例如:
1 @Target(value={TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE}) 2 @Retention(value=SOURCE) 3 public @interface SuppressWarnings
值为:ANNOTATION_TYPE CONSTRUCTOR FIELD LOCAL_VARIABLE METHOD PACKAGE PARAMETER TYPE
如@Target(ElementType.METHOD) 修饰的注解表示该注解只能用来修饰在方法上
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MyTarget { String value() default "hahaha"; }
如把@MyTarget修饰在类上,则程序报错,如:
@MyTarget public class MyTargetTest