一 . 什么是注解
注解是一种标记在 Java 类、方法、字段和其他程序元素上的特殊标签。这些标签提供了有关元素的额外信息,通常以注解的方式存储在 Java 源代码中。注解通常不影响程序的运行,但可以在编译时、运行时或通过工具进行处理。
Java 注解以 @ 符号开头,后跟注解的名称,如 @Override、@Deprecated 等。注解可以有元素,元素以键值对的形式存储信息。以下是一个简单的注解示例:
@Author(name = "John Doe", date = "2023-09-01")
public class MyClass {// 类的内容
}
在上面的示例中,@Author 是一个自定义注解,具有两个元素 name 和 date,它们存储了有关类 MyClass 的信息。
二. 自定义注解
除了使用内置注解,Java 还允许我们创建自定义注解。自定义注解可以用来添加程序的元数据,或者用于特定的用途,例如测试框架、依赖注入等。要创建自定义注解,需要使用 @interface 关键字。具体语法如下:
public @interface 注解名{public 属性类型 属性名() default 默认值;...
}
三. 元注解
元注解是用来修饰注解的注解。常见的元注解如下:
1.@Retention
该注解定义了一个注解的生命周期,即该注解在哪个阶段可用。属性可取值如下:
- RetentionPolicy.SOURCE:注解仅在源代码中存在,编译器使用后就会丢弃它,在编译后不会保留。
- RetentionPolicy.CLASS:注解在编译后仍然存在于类文件中,但在运行时虚拟机不保留注解。
- RetentionPolicy.RUNTIME:注解在运行时仍然可用,运行时虚拟机会保留注解,可以通过反射读取。
2.@Target
注解定义了一个注解可以应用于哪些 Java 类型,常见的类型如下:
- ElementType.TYPE:可以应用于类、接口、枚举或注解。
- ElementType.FIELD:可以应用于字段。
- ElementType.METHOD:可以应用于方法。
- ElementType.PARAMETER:可以应用于方法参数。
- ElementType.CONSTRUCTOR:可以应用于构造函数。
- ElementType.LOCAL_VARIABLE:可以应用于局部变量。
- ElementType.ANNOTATION_TYPE:可以应用于注解类型。
- ElementType.PACKAGE:可以应用于包声明。
4.@Documented
表明被它修饰的注解应该被包含在Java文档中(如Javadoc)中。
5.@Inherited
该注解表明一个注解是可继承的。这意味着如果一个类被标记为使用某个注解,那么它的子类可以自动获得这个注解。
四. 注解解析
读取注解信息,可以使用 Java 的反射机制。
MyAnnotation.java
package com.example.test;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {String value();int number() default 0;
}
AnnotationClass.java
package com.example.test;@MyAnnotation(value = "myAnnotation", number = 1)
public class AnnotationClass {@MyAnnotation(value = "myAnnotation-test1", number = 2)public void test1() {}
}
AnnotationTest.java
package com.example.test;import java.lang.reflect.Method;public class AnnotationTest {public static void parseClass() {// 先得到Class对象Class<AnnotationClass> demoClass = AnnotationClass.class;// 解析类上的注解if (demoClass.isAnnotationPresent(MyAnnotation.class)) {MyAnnotation annotation = demoClass.getDeclaredAnnotation(MyAnnotation.class);System.out.println(annotation.value());System.out.println(annotation.number());}}public static void parseMethod() throws NoSuchMethodException {// 先得到Class对象Class<AnnotationClass> demoClass = AnnotationClass.class;Method test1 = demoClass.getDeclaredMethod("test1");// 解析方法上的注解if (test1.isAnnotationPresent(MyAnnotation.class)) {MyAnnotation method = test1.getDeclaredAnnotation(MyAnnotation.class);System.out.println(method.value());System.out.println(method.number());}}public static void main(String[] args) throws Exception {System.out.println("============parse class============");parseClass();System.out.println("============parse method============");parseMethod();}
}
运行结果:
============parse class============
myAnnotation
1
============parse method============
myAnnotation-test1
2