java注解Annotation

本文深入解析Java注解的原理及应用,包括注解类型、元注解、目标元素及反射获取注解信息的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个自定义的annotation的类型只能是原始数据类型(primitive type), 字符串(String),类(Class), 注解(annotation), 枚举(enumeration)或一维数组(1-dimensional arrays)

public @interface MyAnnotation {
String name();
String[] value();
int type();
EnumTest te();
long tes();
double dd();
float f();
byte by();
char c();
boolean b();
short sh();
}

java.lang.annotation.Retention告诉编译器如何对待 Annotation,使用Retention时,需要提供java.lang.annotation.RetentionPolicy的枚举值.

public enum RetentionPolicy {
SOURCE, // 编译器处理完Annotation后不存储在class中
CLASS, // 编译器把Annotation存储在class中,这是默认值
RUNTIME // 编译器把Annotation存储在class中,可以由虚拟机读取,反射需要
}

java.lang.annotation.Target告诉编译器Annotation使用在哪些地方,使用需要指定java.lang.annotation.ElementType的枚举值.

public enum ElementType {
TYPE, // 指定适用点为 class, interface, enum
FIELD, // 指定适用点为 field
METHOD, // 指定适用点为 method
PARAMETER, // 指定适用点为 method 的 parameter
CONSTRUCTOR, // 指定适用点为 constructor
LOCAL_VARIABLE, // 指定使用点为 局部变量
ANNOTATION_TYPE, //指定适用点为 annotation 类型
PACKAGE // 指定适用点为 package
}

java.lang.annotation.Documented用于指定该Annotation是否可以写入javadoc中.
java.lang.annotation.Inherited用于指定该Annotation用于父类时是否能够被子类继承.

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented //这个Annotation可以被写入javadoc
@Inherited //这个Annotation 可以被继承,只有target是type类型的才起作用
@Target({ElementType.CONSTRUCTOR,ElementType.METHOD}) //表示这个Annotation只能用于注释 构造子和方法
@Retention(RetentionPolicy.CLASS) //表示这个Annotation存入class但vm不读取
public @interface MyAnnotation {
String value() default "hahaha";
}

java.lang.reflect.AnnotatedElement接口提供了四个方法来访问Annotation

public Annotation getAnnotation(Class annotationType);//获得指定类型的注解对象
public Annotation[] getAnnotations();//获得所有注解对象
public Annotation[] getDeclaredAnnotations();//获得声明的所有注解对象
public boolean isAnnotationPresent(Class annotationType);//判断某个对象上是否有指定类型的注解

Class、Constructor、Field、Method、Package等都实现了该接口,可以通过这些方法访问Annotation信息,前提是要访问的Annotation指定Retention为RUNTIME.

以下示例:
ServiceTest 注解

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ServiceTest {
String value();
}


加上注解的AnnotationTest.java类

@ServiceTest("nihao")
public class AnnotationTest {
}

测试类ServiceMain

public class ServiceMain {
public static void main(String[] args) {
//判断AnnotationTest类上是否有ServiceTest注解
boolean flag=AnnotationTest.class.isAnnotationPresent(ServiceTest.class);
if(flag){
ServiceTest serviceTest = AnnotationTest.class.getAnnotation(ServiceTest.class);//获取注解ServiceTest注解对象
System.out.println(serviceTest.value());//通过注解对象获取值
}
}

}

最后输出:
nihao
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值