Annotation注解
概念
注解和注释的区别是注解是官方的解释,注释是自己写的解释。
主要根据annotation的生命周期,用于源代码的注释说明、告诉编译器检查某些条件、影响javadoc工具生成的行为、影响jvm程序的运行。
annotation的生命周期分为只在源代码存在、class文件中存在、jvm运行期存在三种。
只在源代码中存在的不会影响class的运行
只有运行期存在的才会影响程序的运行
java默认提供的几个注解:@Deprecated、@SuppressWarnings、@Override
jdk1.5开始支持,jdk1.6开始可自定义,可加在类、接口、方法、成员变量的定义前面。
Annotation本身是一个接口

实现这个接口的类才能成为注解使用,不过实现的方式不是直接implements语法,而是使用@interface的语法。使用@interface定义的类默认实现了Annotation接口。
使用
定义方法:
定义接口的interface前面加@符号即可,如:

然后就可以使用了,如:

带参数:


注解中定义方法,使用时参数名就是方法名。如果只有一个且方法名是”value”,则使用时可以不指定参数名,默认是value,如:


也可以使用default指定默认值,如:


ps:参数类型只能是基本数据类型、String、Enum、Annotation、Class、以上类型的数组类型,不能是Integer、Object等类型。
元注解
用于修饰注解的注解,有@Target、@Retention、@Documented、@Inherited
jdk自带提供的注解:
@Deprecated 意思是“废弃的,过时的”
@Override 意思是“重写、覆盖”
@SuppressWarnings 意思是“压缩警告”,
如@SuppressWarnings("unused")、@SuppressWarnings("rawtypes")等。
@Target
用于规定注解的使用范围,可选值如下:

1.CONSTRUCTOR:用于描述构造器
2.FIELD:用于描述域
3.LOCAL_VARIABLE:用于描述局部变量
4.METHOD:用于描述方法
5.PACKAGE:用于描述包
6.PARAMETER:用于描述参数
7.TYPE:用于描述类、接口(包括注解类型) 或enum声明
@Retention
描述注解的生命周期

SOURCE:表示只在源代码java中存在,class文件中不存在
CLASS:表示在class文件中也存在,但jvm加载时会去掉
RUNTIME:表示会加载到jvm中。
测试:
Retention 为RetentionPolicy .SORUCE时查看class文件:

Retention 为RetentionPolicy .CLASS时查看class文件,此时是RuntimeInvisibleAnnotations,即运行期不可见:

Retention 为RetentionPolicy .RUNTIME时查看class文件,此时是RuntimeVisibleAnnotations,即运行期可见:

因为只有运行期可见的注解,才可以在程序中动态判断一个类是否有该注解,进行控制程序流程(需要通过反射取得类的注解),因此springmvc等的Controller注解都是RUMTIME的,如:



RUNTIME的annotation控制程序逻辑使用举例:
TestInterface有2个实现类,TestClass1加了@ControllerAnnotation注解,而TestClass2没有,test方法判断传入的对象的类是否有@ControllerAnnotation注解而进入不同的处理逻辑。
ControllerAnnotation.java:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface ControllerAnnotation {
}
Test.java:
interface TestInterface {
}
@ControllerAnnotation
class TestClass1 implements TestInterface {
}
class TestClass2 implements TestInterface {
}
public class Test {
public static void main(String[] args) {
TestInterface t1 = new TestClass1();
TestInterface t2 = new TestClass2();
test(t1);
test(t2);
}
public static void test(TestInterface test) {
if (test.getClass().isAnnotationPresent(ControllerAnnotation.class)) {
System.out.println("进入有ControllerAnnotation的处理逻辑");
} else {
System.out.println("进入没有ControllerAnnotation的处理逻辑");
}
}
}
输出:
进入有ControllerAnnotation的处理逻辑
进入没有ControllerAnnotation的处理逻辑
运行时获取注解的参数:
如:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
public String test1();
public String test2();
}
@MyAnnotation(test1="aaa", test2="bbb")
public class Test {
public static void main(String[] args) throws Exception {
Annotation annotation = Test.class.getAnnotations()[0];
Method method = annotation.getClass().getMethod("test1");
System.out.println(method.invoke(annotation));
Method method2 = annotation.getClass().getMethod("test2");
System.out.println(method2.invoke(annotation));
}
}
输出:
aaa
bbb
利用反射得到Annotation的对象(每一个实参的定义都会创建一个对应的annotation对象,它实现了Annotation接口),然后回调它的test1和test2方法,得到实参。
@Documented
用于说明使用了该注解的类或方法等,在用javadoc工具生成文档时,把该annotation也生成到文档中去。
默认是不把annotation生成到javadoc文档的
@Inherited
父类使用了该注解,则它的子类也默认使用了该注解。
如:
Test.java:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@TestAnTT
class Super {}
class Sub extends Super {}
//@Inherited
@Retention(RetentionPolicy.RUNTIME)
@interface TestAnTT {
}
public class Test {
public static void main(String[] args) {
System.out.println(Sub.class.isAnnotationPresent(TestAnTT.class));
}
}
这里注释掉@Inherited输出false,不注释@Inherited输出true。
浙公网安备 33010602011771号