摘要:知识点总结注解解析注解知识点总结注解通过反射获取类函数或成员上的运行时注解信息,从而实现动态控制程序运行的逻辑。
Java知识点总结(注解-解析注解)
@(Java知识点总结)[Java, 注解]
通过反射获取类、函数或成员上的运行时注解信息,从而实现动态控制程序运行的逻辑。
使用注解步骤:
定义注解
类中使用注解
解析注解
示例:
</>复制代码
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value={ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodAnnotation {
String value();
}
</>复制代码
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value={ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldAnnotation {
String columnName();
String type();
int length();
}
</>复制代码
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value={ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface TableAnnotation {
String value();
}
</>复制代码
@TableAnnotation("tb_student")
public class Student {
@FieldAnnotation(columnName="id",type="int",length=10)
private int id;
@FieldAnnotation(columnName="sname",type="varchar",length=10)
private String name;
@FieldAnnotation(columnName="age",type="int",length=3)
private int age;
@MethodAnnotation("get方法上的注解")
public int getId() {
return id;
}
@MethodAnnotation("set方法上的注解")
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
</>复制代码
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Demo3 {
// 使用反射获取注解,生成类对应的数据库表
private static void test1(Class clazz) {
// 获得类所有的注解
Annotation[] annotations = clazz.getAnnotations();
for (Annotation a : annotations) {
System.out.println(a);
}
// 通过注解的名字获取注解
TableAnnotation t = (TableAnnotation) clazz.getAnnotation(TableAnnotation.class);
System.out.println(t.value());
// 获得属性上的注解
try {
Field f = clazz.getDeclaredField("age");
FieldAnnotation fa = f.getAnnotation(FieldAnnotation.class);
System.out.println("字段名称:" + fa.columnName() + ",字段类型:" + fa.type() + ",字段长度:" + fa.length());
} catch (NoSuchFieldException | SecurityException e) {
e.printStackTrace();
}
// 根据获得的表名、字段信息,拼出DDL语句,然后使用JDBC执行这个SQL语句,在数据库中生成相关的表
}
//获取方法上的注解
private static void test2(Class clazz) {
// 获取所有方法上的注解
Method[] ms = clazz.getMethods();
for (Method m : ms) {
boolean isExist = m.isAnnotationPresent(MethodAnnotation.class);
if (isExist) {
MethodAnnotation ma = m.getAnnotation(MethodAnnotation.class);
System.out.println(ma);
}
}
// 获取指定方法上的注解
try {
Method method = clazz.getMethod("getId", null);
Annotation[] as = method.getAnnotations();
for (Annotation a : as) {
if (a instanceof MethodAnnotation) {
MethodAnnotation ma = (MethodAnnotation) a;
System.out.println(ma);
}
}
} catch (NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Class clazz = null;
try {
clazz = Class.forName("com.gs.Student");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
test1(clazz);
test2(clazz);
}
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/71561.html
摘要:注解提供了一种安全的类似注释的机制,用来将任何的信息或元数据与程序元素类方法成员变量等进行关联。为程序的元素类方法成员变量加上更直观更明了的说明,这些说明与程序的业务逻辑无关,并且提供给指定的工具或框架使用。 什么是注解? Annotation 是 Java5 之后开始引入的新特性,中文为注解。注解提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metadata)与程序元素(...
摘要:我们定义注解元素时,经常使用空字符串作为默认值。也经常使用负数比如表示不存在的含义示例既可以修饰方法,也可以修饰类运行时使用关键字定义注解成员以无参无异常方式声明。方法的名称就是参数的名称可以使用为成员指定一个默认值浙江大学清华大学张三 Java知识点总结(注解-自定义注解) @(Java知识点总结)[Java, 注解] 使用@interface自定义注解时,自动继承了java.lan...
摘要:知识点总结注解元注解知识点总结注解元注解的作用就是负责注解其他注解。 Java知识点总结(注解-元注解 ) @(Java知识点总结)[Java, 注解] 元注解的作用就是负责注解其他注解。 Java5.0定义的元注解: @Target @Retention @Documented @Inherited @Target 用于描述注解的使用范围 @Target(value=Elemen...
阅读 2969·2023-04-26 02:00
阅读 2921·2019-08-30 15:54
阅读 1035·2019-08-30 11:15
阅读 1608·2019-08-29 15:31
阅读 1019·2019-08-29 14:12
阅读 599·2019-08-29 13:08
阅读 934·2019-08-27 10:51
阅读 2808·2019-08-26 12:17
极致性价比!云服务器续费无忧!
Tesla A100/A800、Tesla V100S等多种GPU云主机特惠2折起,不限台数,续费同价。
NVIDIA RTX 40系,高性价比推理显卡,满足AI应用场景需要。
乌兰察布+上海青浦,满足东推西训AI场景需要