Java自定义注解结合反射获取注解中字段
发布日期:2021-05-15 00:00:45 浏览次数:17 分类:精选文章

本文共 3100 字,大约阅读时间需要 10 分钟。

���������java������������������������������������������������java���������������������������������������������������������������������������������������������

1. ���������������������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

2. ���������

java���������������������������������������������������������

  • @Target���������������������������������������������������������������������������������
  • @Retention���������������������������������������������������������������������
  • @Documented���������������������Javadoc���������������������������������
  • @Inherited���������������������������������������

3. ���������������

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String name() default "";
int age() default 0;
int id() default -1; // -1������������
String[] schools() default {"������������"};
}

4. ���������������������

������������������������������������������������������������������������������������������������������������������������

@Slf4j
public class Test {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
Class c = Class.forName("com.mytest.test.User2");
// ������������������
TableKuang tableKuang = c.getAnnotation(TableKuang.class);
System.out.println(tableKuang.value());
// ������������������
Field[] declaredFields = c.getDeclaredFields();
for (Field declaredField : declaredFields) {
FieldKuang annotation = declaredField.getAnnotation(FieldKuang.class);
System.out.println(annotation);
System.out.println(annotation.columnName());
System.out.println(annotation.length());
System.out.println(annotation.type());
}
}
}
@TableKuang("������user")
@Data
@AllArgsConstructor
@NoArgsConstructor
class User2 {
@FieldKuang(columnName = "name������", type = "String������", length = 11)
private String name;
@FieldKuang(columnName = "id������", type = "int������", length = 12)
private int id;
@FieldKuang(columnName = "age������", type = "int������", length = 13)
private int age;
}

5. ������������������

���������������������������������������������������������������������

  • getFields() ���������public���������
  • getDeclaredFields() ������������������������������������������public���������
  • ���������������������������������������������������������������������������������

6. ������������������

������������������������������������������������������

Method getName = c.getMethod("getName", null);
Method setName = c.getMethod("setName", String.class);
Constructor[] constructors = c.getConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor);
}

7. ������������

������������������������������������������������������������������������������������������������������������

上一篇:Nacos安装、集群部署及利用Nginx反向代理
下一篇:Java代理模式及spring aop实现原理

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年04月29日 13时23分27秒