浅谈Java中注解Annotation的定义、使用、解析

2025-05-29 0 99

此例子,用于说明如何在Java中对“注解 Annotation”的定义、使用和解析的操作。注解一般用于自定义开发框架中,至于为什么使用,此处不作过多说明,这里只说明如何使用,以作备记。下面例子已测试,可以正常运行通过。

1、注解自定义。

这里定义两个注解,分别用来注解类和注解属性。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
package cc.rulian.ann;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

/**

* 类注释

*/

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

public @interface MyTable

{

public String name() default "";

public String version() default "1";

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19
package cc.rulian.ann;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

/**

* 字段注释

*/

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.FIELD)

public @interface MyField {

public String name() default ""; //名称

public String type() default ""; //类型

}

2、注解的使用。

说明如何在类中使用自定义注解

?

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
package cc.rulian.ann;

import java.util.Date;

/**

* 基础日志

*

*/

@MyTable(name="T_BaseLog",version="2")

public class BaseLog{

@MyField(name="addTime",type="Date")

private Date log_time; // 时间

@MyField(name="log_level",type="String")

private String log_level; // 级别

@MyField(name="message",type="String")

private String message; // 日志内容

public Date getLog_time()

{

return log_time;

}

public void setLog_time(Date log_time)

{

this.log_time = log_time;

}

public String getLog_level()

{

return log_level;

}

public void setLog_level(String log_level)

{

this.log_level = log_level;

}

public String getMessage()

{

return message;

}

public void setMessage(String message)

{

this.message = message;

}

}

3、注解的解析。

说明如何解析注解

?

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
package cc.rulian.ann;

import java.lang.reflect.Field;

/**

* 读取注释

*/

public class ReadAnn

{

public static void main(String[] args)

{

// 读取类的注释

BaseLog obj = new BaseLog();

// Annotation[] arr = obj.getClass().getAnnotations(); //得到所有注释

MyTable table = obj.getClass().getAnnotation(MyTable.class); // 取得指定注释

System.out.println("类注释(name): " + table.name());

System.out.println("类注释(version): " + table.version());

// 读取属性的注释

Field[] fields = obj.getClass().getDeclaredFields();

for (Field f : fields)

{

// Annotation[] arr2 = f.getAnnotations(); //得到所有注释

MyField ff = f.getAnnotation(MyField.class);// 取得指定注释

if(ff != null)

{

System.out.println("属性(" + f.getName() + "): " + ff.name() + " -- " + ff.type());

}

}

}

}

4、解析输出结果。

类注释(name): T_BaseLog
类注释(version): 2
属性(log_time): addTime — Date
属性(log_level): log_level — String
属性(message): message — String

以上这篇浅谈Java注解Annotation的定义、使用、解析就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 浅谈Java中注解Annotation的定义、使用、解析 https://www.kuaiidc.com/117550.html

相关文章

发表评论
暂无评论