水滴石穿-自定义注解以及简单实现通过反射获取自定义注解属性的值

本文最后更新于:April 14, 2022 pm

积土成山,风雨兴焉;积水成渊,蛟龙生焉;积善成德,而神明自得,圣心备焉。故不积跬步,无以至千里,不积小流无以成江海。齐骥一跃,不能十步,驽马十驾,功不在舍。面对悬崖峭壁,一百年也看不出一条裂缝来,但用斧凿,能进一寸进一寸,能进一尺进一尺,不断积累,飞跃必来,突破随之。

目录

元注解

注解其他注解的称为元注解。犹如抓(坏)人的人是警察。需要知道四个类型:@Target、@Retention、@Documented、@Inherited。

作用:

  • @Target:用于描述注解的使用范围(即注解可以使用在上面地方)。参数有:TYPE(类)、FIELD(属性)、METHOD(方法)、PACKAGE(包)、PARAMETER、CONSTRUCTOR、LOCAL_VARIABLE、ANNOTATION_TYPE、TYPE_PARAMETER、TYPE_USE。最后两个是1.8加的。
  • @Retention:表示需要在什么级别保存该注释信息,描述注解的生命周期。有三个值:SOURCE(源码)、CLASS(字节码)、RUNTIME(运行时)。
  • @Documented:表示该注解将被包含在Javadoc中。
  • @Inherited:表示子类可以继承父类中的该注解。

简单自定义注解

1
2
3
4
5
6
@Target(value = {ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface tothefor{
String name();
int age() default 13; //指定默认值
}

📢注意:注解中的每一个属性后面必须要加括号和分号!!!

对于非必填的注解属性,可以设置默认值。如果没有设置默认值,则在使用注解时,必须给其属性赋值。

通过反射获取注解属性值

注解

类注解

1
2
3
4
5
6
//类注解
@Target(value = {ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface TTF_table{
String tableName();
}

属性注解

1
2
3
4
5
6
//类中属性的注解
@Target(value = {ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@interface TTF_col{
String ColValue();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//类
@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
@TTF_table(tableName = "Student")
class User{
@TTF_col(ColValue = "tothefor")
private String username;
@TTF_col(ColValue = "1123456")
private String password;
@TTF_col(ColValue = "13")
private Integer age;
}

可以是简单的自定义类,也可以是对应数据库中的一张表的类。

获取实例

1
2
3
4
5
6
7
8
9
10
11
12
13
public class AnnoTest01 {
public static void main(String[] args) throws Exception {
Class cs = Class.forName("com.annotationStu.User");

TTF_table ttf = (TTF_table) cs.getAnnotation(TTF_table.class); //获取类的注解
String s = ttf.tableName(); //获取值
System.out.println(s);

Field name = cs.getDeclaredField("username"); //获取类中的属性
TTF_col annotation = name.getAnnotation(TTF_col.class); //获取属性的注解
System.out.println(annotation.ColValue()); //获取值
}
}

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.annotationStu;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.lang.annotation.*;
import java.lang.reflect.Field;

/**
* @Author DragonOne
* @Date 2022/4/14 15:21
* @墨水记忆 www.tothefor.com
*/
public class AnnoTest01 {
@SuppressWarnings("all")
public static void main(String[] args) throws Exception {
Class cs = Class.forName("com.annotationStu.User");

TTF_table ttf = (TTF_table) cs.getAnnotation(TTF_table.class); //获取类的注解
String s = ttf.tableName(); //获取类注解中属性的值
System.out.println(s); //Student

Field name = cs.getDeclaredField("username"); //获取类中的属性
System.out.println(name.getName()); //获取属性名称,username
TTF_col annotation = name.getAnnotation(TTF_col.class); //获取属性的注解
System.out.println(annotation.ColValue()); //获取值,tothefor
}
}

//类注解
@Target(value = {ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface TTF_table{
String tableName();
}

//类中属性的注解
@Target(value = {ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@interface TTF_col{
String ColValue();
}


//类
@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
@TTF_table(tableName = "Student")
class User{
@TTF_col(ColValue = "tothefor")
private String username;
@TTF_col(ColValue = "1123456")
private String password;
@TTF_col(ColValue = "13")
private Integer age;
}