1.定义注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface ExampleAnnotation {
String value() default "我是注解";
String[] param() default {"get", "post"};
}
2.使用注解
@ExampleAnnotation(value="我改变了注解的默认值", param={"get", "post"})
public class AnnotationExample {
public static void main(String[] args) {
if(AnnotationExample.class.isAnnotationPresent(ExampleAnnotation.class)) {
ExampleAnnotation annotation = (ExampleAnnotation)AnnotationExample.class.getAnnotation(ExampleAnnotation.class);
System.out.println(annotation);
System.out.println(annotation.value());
String[] param = annotation.param();
for (int i = 0; i < param.length; i++) {
System.out.println(param[i]);
}
}
}
}
3.Java中的元注解
Java中提供了四种元注解,专门负责注解其他的注解,分别如下
@Retention元注解,表示需要在什么级别保存该注释信息(生命周期)。可选的RetentionPoicy参数包括:
RetentionPolicy.SOURCE: 停留在java源文件,编译器被丢掉
RetentionPolicy.CLASS:停留在class文件中,但会被VM丢弃(默认)
RetentionPolicy.RUNTIME:内存中的字节码,VM将在运行时也保留注解,因此可以通过反射机制读取注解的信息
@Target元注解,默认值为任何元素,表示该注解用于什么地方。可用的ElementType参数包括
ElementType.CONSTRUCTOR: 构造器声明
ElementType.FIELD: 成员变量、对象、属性(包括enum实例)
ElementType.LOCAL_VARIABLE: 局部变量声明
ElementType.METHOD: 方法声明
ElementType.PACKAGE: 包声明
ElementType.PARAMETER: 参数声明
ElementType.TYPE: 类、接口(包括注解类型)或enum声明
@Documented:将注解包含在JavaDoc中
@Inheried:允许子类继承父类中的注解
JAVA注解
最新推荐文章于 2024-10-01 09:29:45 发布