java通过反射Reflect操作注解Annotation

news/2024/10/21 6:10:28/

通过反射获取加在类上的注解

java中可以通过反射来操作注解,可以获得加在类上面的注解,进而获得加在类上面的注解 的配置参数的值。
加在类上面的注解 Table_Annotation 的定义:

java">@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Table_Annotation {// value字段。用于指定当前注解修饰的类对应 数据库中的哪个表。String value();
}

实体类:Employee

java">// 使用注解 Table_Annotation 修饰,value字段指定当前类Employee对应数据库中的表 tb_employee
@Table_Annotation(value = "tb_employee")
class Employee {private int id;private int age;private String name;
}

通过反射,操作加在类Employee上面的注解 Table_Annotation

java">public static void main(String[] args) throws NoSuchFieldException {// 演示通过反射 获取类上加的注解// 首先,获得 目标类Employee 对应的Class对象Class<Employee> employeeClass = Employee.class;// 其次,通过Class对象获得这个类上的所有注解。当然,此处类上只有一个注解Table_AnnotationAnnotation[] annotations = employeeClass.getAnnotations();System.out.println(Arrays.toString(annotations));// [@reflect_package.Table_Annotation("tb_employee")]// 获得注解对应的value的值// 通过指定注解名的方式获得加在类Employee上面的指定注解Table_Annotation table_annotation = employeeClass.getAnnotation(Table_Annotation.class);// 通过 .value()的方式,获得这个注解加在类Employee上时,对应的valueString value = table_annotation.value();System.out.println(value); // tb_employee}

可以看到,通过反射的方式,获得了加在类Employee上面的注解。进一步,通过指定注解的方式,可以获得加在类上面的指定注解的value字段。从而可以知道类Employee对应的是数据库中的哪张表。

通过反射获取加在字段上的注解

加在类的字段上面的注解 Field_Annotation

java">@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Field_Annotation {// 指示注解 Field_Annotation 修饰的属性 对应数据库中的表中的哪个字段String columnName();// 指示注解 Field_Annotation 修饰的属性 对应字段的属性String type(); // 指示注解 Field_Annotation 修饰的属性 对应字段的长度int length(); 
}

通过反射,操作 加在类Employee 的具体字段 上面的注解 Field_Annotation

java">public static void main(String[] args) throws NoSuchFieldException {// 首先,获得目标类Employee 的Class对象Class<Employee> employeeClass = Employee.class;// 再通过反射获取指定的字段Field field = employeeClass.getDeclaredField("id");// 其次,通过反射获取加在这个字段上面的指定注解 Field_AnnotationField_Annotation field_annotation = field.getAnnotation(Field_Annotation.class);System.out.println(field_annotation);// @reflect_package.Field_Annotation(columnName="id", type="int", length=10)// 最后:获取加在 类Employee的字段id上面的指定注解 Field_Annotation 的三个参数对应的值System.out.println(field_annotation.columnName()); // idSystem.out.println(field_annotation.type()); // intSystem.out.println(field_annotation.length()); // 10}

可以看到,可以通过 反射获取加在类的指定字段上面的注解,进一步,还可以获得修饰字段的指定注解对应的各个参数属性的值。


http://www.ppmy.cn/news/1466890.html

相关文章

在点云地图中进行点云计数

文章目录 概要头文件主要代码概要 在激光SLAM(Simultaneous Localization and Mapping)中,局部点云地图是通过激光雷达扫描捕捉到的周围环境的局部三维点集合。统计局部点云地图中的所有点数目是一个常见的需求,这可以帮助我们了解数据的密集程度、有效性等。 为了统计局…

Python sorted 用法:深入解析排序函数的奥秘

Python sorted 用法&#xff1a;深入解析排序函数的奥秘 在Python编程中&#xff0c;sorted函数是一个强大的工具&#xff0c;用于对可迭代对象进行排序。然而&#xff0c;它的用法和功能远不止表面看起来那么简单。本文将深入剖析sorted函数的四个方面、五个方面、六个方面和…

全自动打包封箱机:解析其在产品质量与安全保障方面的作用

在当今快节奏的生产环境中&#xff0c;全自动打包封箱机以其高效、精准的特点&#xff0c;正逐渐成为生产线上的得力助手。它不仅提升了生产效率&#xff0c;更在产品质量与安全保障方面发挥着举足轻重的作用。星派将详细解析全自动打包封箱机在产品质量与安全保障方面的作用。…

kotlin基础之协程

Kotlin协程&#xff08;Coroutines&#xff09;是Kotlin提供的一种轻量级的线程模型&#xff0c;它允许我们以非阻塞的方式编写异步代码&#xff0c;而无需使用回调、线程或复杂的并发API。协程是一种用户态的轻量级线程&#xff0c;它可以在需要时挂起和恢复&#xff0c;从而有…

Jvm(一)之栈、堆、方法区

前言-与正文无关 生活远不止眼前的苦劳与奔波&#xff0c;它还充满了无数值得我们去体验和珍惜的美好事物。在这个快节奏的世界中&#xff0c;我们往往容易陷入工作的漩涡&#xff0c;忘记了停下脚步&#xff0c;感受周围的世界。让我们一起提醒自己&#xff0c;要适时放慢脚步…

爬虫案例:有道翻译python逆向

pip install pip install requestspip install base64pip install pycrytodome tools 浏览器的开发者工具&#xff0c;重点使用断点&#xff0c;和调用堆栈 工具网站&#xff1a;https://curlconverter.com/ 简便请求发送信息 flow 根据网站信息&#xff0c;preview,respon…

C++ Thread多线程并发记录(3)线程创建总结

1.启动线程传递全局函数 #include <iostream> #include <thread>void Th1(int id){std::cout << "Create global fun. id " << id << std::endl; } void TH1(const int &id){std::cout << "Create global fun. id &…

2024.5.31学习记录

1、面经复习&#xff1a;webpack 2、rosebush 开发完成&#xff1a; rosebush官网