Java Reflect如何利用反射获取属性上的注解
更新时间:2024年09月27日 08:49:59 作者:我一直在流浪
AnnotatedElement接口是Java反射机制的一部分,用于读取运行中程序的注释信息,通过getAnnotation、getAnnotations、isAnnotationPresent和getDeclaredAnnotations方法,可以访问和判断注解,Field类实现了该接口
1. AnnotatedElement接口
AnnotatedElement接口表示目前正在此 JVM 中运行的程序的一个已注释元素,该接口允许反射性地读取注释。
调用AnnotatedElement对象的如下方法可以访问Annotation信息:
getAnnotation(Class<T>annotationClass):返回该程序元素上存在的指定类型的注释,如果该类型的注释不存在,则返回null。Annotation[] getAnnotations():返回此元素上存在的所有注释。boolean isAnnotationPresent(Class<?extendsAnnotation>annotationClass):判断该程序元素上是否存在指定类型的注释,如果存在则返回true,否则返回false。Annotation[] getDeclaredAnnotations():返回直接存在于此元素上的所有注释。与此接口中的其他方法不同,该方法将忽略继承的注释。
2. Field类实现了AnnotatedElement接口

3. 获取属性上的注解
① 自定义注解
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD,ElementType.TYPE,ElementType.FIELD,ElementType.PARAMETER} )
public @interface MyParam1 {
String value() default "";
}@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD,ElementType.TYPE,ElementType.FIELD,ElementType.PARAMETER} )
public @interface MyParam2 {
String value() default "";
}② 给方法参数上添加注解
@ControllerAdvice
@Controller
@MyAnnotation(name = "李四",age=12)
public class Test {
@MyField1("name1")
@MyField2("name2")
@Value("name")
private String name;
@MyField1("email1")
@MyField2("email2")
@Value("email")
private String email;
}③ 获取属性上的注解
public class Main {
public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException {
// 得到Class类对象
Class<?> clazz = Class.forName("com.example.redislock.annotation.Test");
// 获取类的所有属性
Field[] fields = clazz.getDeclaredFields();
// 获取属性上的所有注解
int i = 1;
for (Field field : fields) {
System.out.println("第"+i+++"个属性的注解有:");
Annotation[] annotations = field.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation.annotationType());
}
}
// 获取属性上指定MyField2类型的注解
System.out.println();
System.out.println("获取属性上指定MyField2类型的注解:");
for (Field field : fields) {
MyField2 myField2 = field.getAnnotation(MyField2.class);
System.out.println(myField2);
System.out.println(myField2.value());
}
// 获取属性上指定MyField2类型的注解
System.out.println();
System.out.println("获取属性上指定MyField2类型的注解:");
for (Field field : fields) {
MyField2[] myField2s = field.getAnnotationsByType(MyField2.class);
for (MyField2 myField2 : myField2s) {
System.out.println(myField2);
}
}
}
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringCloud Gateway动态转发后端服务实现过程讲解
这篇文章主要介绍了SpringCloud Gateway动态转发后端服务实现过程,简单的路由转发可以通过SpringCloudGateway的配置文件实现,在一些业务场景种,会需要动态替换路由配置中的后端服务地址,单纯靠配置文件无法满足这种需求2023-03-03
SpringBoot中使用异步线程导致Request请求头丢失问题的解决方法
异步线程请求头丢失的问题通常发生在多线程环境中,特别是在使用 CompletableFuture 或其他异步编程模型时,本文给大家详细介绍了SpringBoot中使用异步线程导致Request请求头丢失问题的原因和解决方法,需要的朋友可以参考下2025-07-07
springboot快速集成mybatis-plus的详细教程
这篇文章主要介绍了springboot快速集成mybatis-plus的教程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-09-09
SpringBoot集成Swagger使用SpringSecurity控制访问权限问题
这篇文章主要介绍了SpringBoot集成Swagger使用SpringSecurity控制访问权限问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-05-05


最新评论