Java注解

一、注解的定义和使用

使用@interface修饰符来声明注解

/*
 * 自定义注解
 */
public @interface MyAnnotation {

}

使用注解:@MyAnnotation

/**
 * 使用注解
 **/
@MyAnnotation
public class MyTestClass {

    @MyAnnotation
    private String name;

    @MyAnnotation
    public void test(){

    }
}

二、元注解

元注解:也称为注解的注解,作用在注解中,分别有@Retention@Target@Document@Inherited@Repeatable(JDK1.8加入)五种。

@Retention

表示注解存在阶段是在源码(编译期),字节码(类加载)还是运行期(JVM中运行),在@Retention注解中使用枚举RetentionPolicy来标识注解存在时期

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

@Retetion(RetentionPolicy.SOURCE):表示注解仅存在于源码中,在class字节码文件中不包含

@Retention(RetentionPolicy.CLASS):默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得

@Retention(RetentionPolicy.RUNTIME):, 注解会在class字节码文件中存在,在运行时可以通过反射获取到

​ 通过上述分析,自定义的注解如果只存着源码中或者字节码文件中就无法发挥作用,我们需要在运行期间能获取到注解信息,所以自定义注解中通常使用 @Retention(RetentionPolicy.RUNTIME)

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {

}

@Target

表示注解的作用范围,可以是类,方法,方法参数变量等,通过枚举类ElementType表示作用类型

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    ElementType[] value();
}

@Target(ElementType.TYPE) :作用于接口、类、枚举、注解

@Target(ElementType.FIELD) :作用于属性字段、常量

@Target(ElementType.METHOD): 作用于方法

@Target(ElementType.PARAMETER): 作用于方法参数

@Target(ElementType.CONSTRUCTOR): 作用于构造函数

@Target(ElementType.LOCAL_VARIABLE):作用于局部变量

@Target(ElementType.ANNOTATION_TYPE):作用于注解(@Retention注解中就使用该属性)

@Target(ElementType.PACKAGE) :作用于包

@Target(ElementType.TYPE_PARAMETER) :作用于类型泛型,即泛型方法、泛型类、泛型接口

@Target(ElementType.TYPE_USE) :类型使用.可以用于标注任意类型除了 class

通常使用@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyAnnotation {

}

@Documented

​ 表示能够将注解中的元素包含到 Javadoc 中去

@Inherited

​ 表示此注解可继承,如果一个类被@Inherited注解修饰,那么即使它的子类没有被其他注解修饰,则它的子类也继承了父类的注解,通过反射可以获得注解的相关信息。

@Repeatable

​ 表示被这个元注解修饰的注解可以同时作用一个对象多次,但是每次作用注解又可以代表不同的含义。

三、注解的属性

注解属性类型可以是以下列出的类型:

  • 1.基本数据类型
  • 2.String
  • 3.枚举类型
  • 4.注解类型
  • 5.Class类型
  • 6.以上类型的一维数组类型

如果注解又多个属性,则可以在注解括号中用“,”号隔开分别给对应的属性赋值

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyTestAnnotation {
    String name() default "mao";
    int age() default 18;
}

@MyTestAnnotation(name = "father",age = 50)
public class Father {
}

获取注解属性:通过反射

/**是否存在对应 Annotation 对象*/
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
    return GenericDeclaration.super.isAnnotationPresent(annotationClass);
}

 /**获取 Annotation 对象*/
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
    Objects.requireNonNull(annotationClass);
	return (A) annotationData().annotations.get(annotationClass);
}

 /**获取所有 Annotation 对象数组*/   
public Annotation[] getAnnotations() {
    return AnnotationParser.toArray(annotationData().annotations);
}    

jdk提供的注解

@Override:表示当前方法是一个重写的方法

@Deprecated:表示当前方法是一个过时的方法

@SuppressWarnings:对程序中的警告去除

四、注解使用案例

使用注解进行参数配置

package com.whw.annotation;

import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface DataInfo {
    String name() default "";

    String note() default "";
}


@DataInfo(name = "张三",note = "中国人")
class User {

}

class MyTest {
    public static void main(String[] args) {
        Class<User> userClass = User.class;
        // 判断注解是否存在
        boolean b = userClass.isAnnotationPresent(DataInfo.class);
        if (b) {
            DataInfo annotation = userClass.getAnnotation(DataInfo.class);
            // 获取注解信息
            System.out.println("注解信息:" + annotation.name() + "," + annotation.note());
        } else {
            System.out.println("注解不存在");
        }
    }
}
posted @ 2021-08-06 14:08  码农小匠  阅读(35)  评论(0编辑  收藏  举报