Annotation注解
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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注解⽽进⼊不同的处理逻辑。
import ng.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的处理逻辑
运⾏时获取注解的参数:
如:
@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 ng.annotation.Retention;
import ng.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。