Java: Annotaion

 

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
/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 *
 * 历史版本: JDK 17.01
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc DBField.java
 * Interface
 * Record
 * Annotation
 * Enum
 * */
 
 
 
package CoreJava.twelfth;
 
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
 
/**
 *
 *
 * */
@Documented
@Target(ElementType.FIELD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface DBField {
    /**
     *
     *
     * */
    String name();
    /**
     *
     *
     * */
    Class< ?> type();
    /**
     *
     *
     * */
    boolean isPrimaryKey() default false;
 
 
}

  

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 *
 * 历史版本: JDK 17.01
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc DuUser.java
 * Interface
 * Record
 * Annotation
 * Enum
 * */
 
 
package CoreJava.twelfth;
import java.util.Date;
 
/**
 *
 *
 * */
public class DuUser {
    /**
     *
     *
     * */
    @DBField(name = "id", isPrimaryKey = true, type = Long.class)
    private long id;
    /**
     *
     *
     * */
    @DBField(name = "name", type = String.class)
    private String name;
    /**
     *
     *
     * */
    @DBField(name = "email", type = String.class)
    private String email;
    /**
     *
     *
     * */
    @DBField(name = "createdtime", type = Date.class)
    private Date created;
    /**
     *
     *
     * */
    public long getId() {
        return id;
    }
    /**
     *
     *
     * */
    public void setId(long id) {
        this.id = id;
    }
    /**
     *
     *
     * */
    public String getName() {
        return name;
    }
    /**
     *
     *
     * */
    public void setName(String name) {
        this.name = name;
    }
    /**
     *
     *
     * */
    public String getEmail() {
        return email;
    }
    /**
     *
     *
     * */
    public void setEmail(String email) {
        this.email = email;
    }
    /**
     *
     *
     * */
    public Date getCreated() {
        return created;
    }
    /**
     *
     *
     * */
    public void setCreated(Date created) {
        this.created = created;
    }
 
}

  

调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
System.out.println("Java Custom Annotation Example");
          System.out.println();
 
          DuUser usr = new DuUser();
          usr.setEmail("geovindu@dusystem.com");
          usr.setName("Geovin Du");
          usr.setId(112);
          usr.setCreated(new Date());
 
          for (Field field : usr.getClass().getDeclaredFields()) {
              DBField dbField = field.getAnnotation(DBField.class);
              System.out.println("field name: " + dbField.name());
 
              // changed the access to public
              field.setAccessible(true);
              Object value = field.get(usr);
              System.out.println("field value: " + value);
 
              System.out.println("field type: " + dbField.type());
              System.out.println("is primary: " + dbField.isPrimaryKey());
              System.out.println();
          }

  

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Java Custom Annotation Example
 
field name: id
field value: 112
field type: class java.lang.Long
is primary: true
 
field name: name
field value: Geovin Du
field type: class java.lang.String
is primary: false
 
field name: email
field value: geovindu@dusystem.com
field type: class java.lang.String
is primary: false
 
field name: createdtime
field value: Tue Nov 01 21:46:59 CST 2022
field type: class java.util.Date
is primary: false

  

posted @   ®Geovin Du Dream Park™  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示