注解
预定义注解
- JDK中预定义的一些注解
@Override :检测被该注解标注的方法是否是继承自父类(接口)的
@Deprecated:该注解标注的内容,表示已过时
@SuppressWarnings:压制警告
一般传递参数all @SuppressWarnings("all")
自定义注解
- 注解通过 @interface 关键字进行定义。
public @interface TestAnnotation {}
- 格式
元注解
public @interface 注解名称{
属性列表;
}
- 本质:注解本质上就是一个接口,该接口默认继承Annotation接口
public interface MyAnno extends java.lang.annotation.Annotation {}
- 属性:接口中的抽象方法
要求:
- 属性的返回值类型有下列取值
- 基本数据类型
- String
- 枚举
- 注解
- 以上类型的数组
- 定义了属性,在使用时需要给属性赋值
- 如果定义属性时,使用default关键字给属性默认初始化值,则使用注解时,可以不进
行属性的赋值。 - 如果只有一个属性需要赋值,并且属性的名称是value,则value可以省略,直接定义值
即可。 - 数组赋值时,值使用{}包裹。如果数组中只有一个值,则{}可以省略
- 案例
//自定义注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface aaa {
int num();
String str();
}
//获取自定义注解的成员属性值
@aaa(num = 20, str = "乔伟")
public class ac {
public static void main(String[] args) {
a();
}
// @aaa(num = 30, str = "拉拉")
public static void a() {
// Method method = ac.class.getDeclaredMethod("a");
// aaa annotation = method.getAnnotation(aaa.class);
aaa annotation = ac.class.getAnnotation(aaa.class);
int num = annotation.num();
String str = annotation.str();
System.out.println(num);
System.out.println(str);
}
}
元注解:用于描述注解的注解
@Target:描述注解能够作用的位置
@Inherited:描述注解是否被子类继承
- ElementType取值:
TYPE:可以作用于类上
METHOD:可以作用于方法上
FIELD:可以作用于成员变量上
@Retention:描述注解被保留的阶段
@Retention(RetentionPolicy.RUNTIME):当前被描述的注解,会保留到class字节码文
件中,并被JVM读取到
Documented:描述注解是否被抽取到api文档中 * @
在程序使用(解析)注解:获取注解中定义的属性值
- 获取注解定义的位置的对象 (Class,Method,Field)
- 获取指定的注解
getAnnotation(Class)
//其实就是在内存中生成了一个该注解接口的子类实现对象
public class ProImpl implements Pro{
public String className(){
return "cn.itcast.annotation.Demo1";
}
public String methodName(){
return "show";
}
}
- 调用注解中的抽象方法获取配置的属性值