Java注解的使用

2022-10-14,

一,基本概念

java 注解是jdk5.0引入的注释机制,可以被使用在类,方法,参数等地方中,并且可以通过java的反射机制获取注解中的内容,

注解相当于标签,可以标识方法,类或属性具有某些特征,在编译器生成的类文件时,可以被嵌入到字节码中。另外用户可以自

定义注解,完成定制化的开发,尤其是在利用springboot进行项目开发时,我们会经常使用注解管理spring容器的bean,从而大大

提高了开发的效率。

 二,常用注解

在开发过程中,我们可以经常看到一些内置的注解:

@override :用于校验该方法是否是重载方法,如果不是重载方法,而且还是使用这个注解则会报错。

@deprecated :用于过时的用法,如果继续使用,编译器会给出警告

@suppresswarnings :用于指示编译器忽略注解中声明的警告

在编写自定义注解时,也会使用一些元注解

1,@retention:定义了注解的保留策略(retentionpolicy)

@documented
@retention(retentionpolicy.runtime)
@target(elementtype.annotation_type)
public @interface retention {
    /**
     * returns the retention policy.
     * @return the retention policy
     */
    retentionpolicy value();
}

其中retentionpolicy是一个枚举类型,共有三种枚举值

(1)class:此注解的缺省行为,表明在程序运行期间,注解可以被编译器保存在类文件中,但不会被虚拟机保留。

(2)runtime:表明在程序运行期间,既可以被编译器保存在类文件中,也被虚拟机保留,所以注解的内容可以通过反射机制读取

(3)source:注解会被编译器丢弃

2,@target:定义了注解的作用目标

@documented
@retention(retentionpolicy.runtime)
@target(elementtype.annotation_type)
public @interface target {
    /**
     * returns an array of the kinds of elements an annotation type
     * can be applied to.
     * @return an array of the kinds of elements an annotation type
     * can be applied to
     */
    elementtype[] value();
}

其中elementtype是一个枚举类型,并且表明@target注解拥有的是枚举类型的数组,可以被指定多个值。

(1)type:允许作用在类,接口或者枚举声明上

(2)field:允许作用在属性字段上

(3)method:允许作用在方法上

(4)parameter:允许作用在参数上

(5)constructor:允许作用在构造器上

(6)local_variable:允许作用在本地变量上

(7)annotation_type:允许作用在注解类型上

(8)package:允许作用在包上

jdk1.8之后,新增type_parameter和type_use两个属性

(9)type_parameter:允许作用在类型参数上

(10)type_use:允许作用在使用类型的地方上

3,@documented 定义注解可以包含在javadoc中

4,@inherited:表明注解可以被子类集成使用

三,自定义注解

1,使用关键字@interface进行自定义注解,注解内容可以指定注解属性的类型,缺省值等

1 @documented
2 @retention(retentionpolicy.runtime)
3 @target({elementtype.method, elementtype.field})
4 public @interface myannotation {
5 
6     int id() default 0;
7 
8     string[] value();
9 }

自定义的注解myannotation包含id和value两个属性,其中属性id的类型为int,且缺省值为0,属性value的类型为string数组。

注意,在声明属性时,属性名后跟的小括号一定要加上。@myannotation表明可以被使用在方法或属性字段上,并且被编译器保存在类文件中,

一直驻留在jvm虚拟机中,所以可以通过反射访问到注解中的内容。

2,使用方法

1 public class user {
2     
3     @myannotation(value = {"male", "female"})
4     public void getuser(string name, int age) {
5 
6         system.out.println("user: [" + name + "," + age + "]");
7     }
8 }

@myannotation被使用在getuser方法上,并且指定注解的vlue属性值为male和female

3,利用反射获取注解

 1 public class myannotationtest {
 2 
 3     public static void main(string[] args) throws exception {
 4 
 5 
 6         user user = new user();
 7         //通过返回获取实例
 8         class<user> userclass = user.class;
 9 
10         method method = userclass.getmethod("getuser", string.class, int.class);
11         //利用反射调用方法
12         method.invoke(user, "rose", 24);
13 
14         //获取方法上的myannotation注解
15         if (method.isannotationpresent(myannotation.class)) {
16 
17             //获取方法上的注解实例
18             myannotation annotation = method.getannotation(myannotation.class);
19 
20             string[] value = annotation.value();
21 
22             for (string v : value) {
23                 system.out.printf("%s ", v);
24             }
25         } else {
26             system.out.println("没有应用myannotation注解");
27         }
28 
29         system.out.println();
30 
31         //获取方法上的所有注解
32         annotation[] annotations = method.getannotations();
33         for (annotation annotation : annotations) {
34             system.out.println(annotation);
35         }
36 
37     }
38 
39 }

利用java的反射机制获取方法上的注解内容,通过method类的getannotation方法可以获取到指定的注解,getannotations方法可以获取到方法上的所用注解。

运行结果:

四,小结

注解相当于标签,利用反射机制可以获取到注解中的内容,可以作用在类,方法,参数等地方,使其具有某些属性,通过注解对程序进行标识来实现特定的处理,让编写的程序更加简洁。

《Java注解的使用.doc》

下载本文的Word格式文档,以方便收藏与打印。