- 1. AnnotatedElement接口
- 2. Class 类实现了AnnotatedElement接口
- 3. 获取类上的注解
AnnotatedElement接口表示目前正在此 JVM 中运行的程序的一个已注释元素,该接口允许反射性地读取注释。
该接口主要有如下几个实现类:
- Class:类定义
- Constructor:构造器定义
- Field:类的成员变量定义
- Method:类的方法定义
- Package:类的包定义
调用AnnotatedElement对象的如下方法可以访问Annotation信息:
- getAnnotation(Class
annotationClass):返回该程序元素上存在的指定类型的注释,如果该类型的注释不存在,则返回null。 - Annotation[] getAnnotations():返回此元素上存在的所有注释。
- boolean isAnnotationPresent(ClassannotationClass):判断该程序元素上是否存在指定类型的注释,如果存在则返回true,否则返回false。
- Annotation[] getDeclaredAnnotations():返回直接存在于此元素上的所有注释。与此接口中的其他方法不同,该方法将忽略继承的注释。
Class类实现了AnnotatedElement接口,因此可以通过反射获取加载该类上的注解。
public final class Classimplements java.io.Serializable, GenericDeclaration, Type, AnnotatedElement { }
1. getAnnotation(Class annotationClass)
如果存在该元素的指定类型的注释,则返回这些注释,否则返回 null。
public A getAnnotation(Class annotationClass)
2. isAnnotationPresent(Class extends Annotation> annotationClass)
如果指定类型的注释存在于此元素上,则返回 true,否则返回 false。
public boolean isAnnotationPresent(Class extends Annotation> annotationClass)
3. getAnnotations()
返回此元素上存在的所有注释。(如果此元素没有注释,则返回长度为零的数组。)
public Annotation[] getAnnotations()
4. getDeclaredAnnotations()
返回直接存在于此元素上的所有注释。与此接口中的其他方法不同,该方法将忽略继承的注释。
public Annotation[] getDeclaredAnnotations()3. 获取类上的注解
① 自定义注解
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD,ElementType.TYPE})
public @interface MyAnnotation {
public int age();
// 为name变量指定初始值
public String name() default "张三";
}
② 给Test类上加3个注解:@ControllerAdvice、@Controller、@MyAnnotation(name = “李四”,age=12)
@ControllerAdvice
@Controller
@MyAnnotation(name = "李四",age=12)
public class Test {
public void show(){
System.out.println("111");
}
}
③ 获取Test类上的指定类型的注解
public class Main {
public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException {
// 得到Class类对象
Class> clazz = Class.forName("com.example.redislock.annotation.Test");
// 获取Test类上指定类型的注解
MyAnnotation annotation = clazz.getAnnotation(MyAnnotation.class);
int age = annotation.age();
String name = annotation.name();
System.out.println("name="+name+" age="+age);
}
}
④ 获取Test类上的所有注解
public class Main {
public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException {
// 得到Class类对象
Class> clazz = Class.forName("com.example.redislock.annotation.Test");
// 获取Test类上所有的注解
Annotation[] annotations = clazz.getAnnotations();
for (Annotation annotation : annotations) {
if(annotation instanceof MyAnnotation){
MyAnnotation myAnnotation =(MyAnnotation) annotation;
int age = myAnnotation.age();
String name = myAnnotation.name();
System.out.println("name="+ name +" age="+age);
}
}
}
}
public class Main {
public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException {
// 得到Class类对象
Class> clazz = Class.forName("com.example.redislock.annotation.Test");
// 获取Test类上所有的注解
Annotation[] annotations = clazz.getAnnotations();
for (Annotation annotation : annotations) {
// Test类上的所有注解
Class extends Annotation> annotationType = annotation.annotationType();
System.out.println(annotationType);
}
}
}



