Java 注解(Annotation)

简介

Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制。
Java 语言中的类、方法、变量、参数和包等都可以被标注。和 Javadoc 不同,Java 标注可以通过反射获取标注内容。在编译器生成类文件时,标注可以被嵌入到字节码中。Java 虚拟机可以保留标注内容,在运行时可以获取到标注内容 。 当然它也支持自定义 Java 标注。
我的理解注解就是给代码块添加一个标记,然后我们根据reflect获取添加的注解对代码进行处理

内置的注解

Java 定义了一套注解,共有 7 个,3 个在 java.lang 中,剩下 4 个在 java.lang.annotation 中。
作用在代码的注解是
@Override - 检查该方法是否是重写方法。如果发现其父类,或者是引用的接口中并没有该方法时,会报编译错误。
@Deprecated - 标记过时方法。如果使用该方法,会报编译警告。
@SuppressWarnings - 指示编译器去忽略注解中声明的警告。
作用在其他注解的注解(或者说 元注解)是:
@Retention - 标识这个注解怎么保存,是只在代码中,还是编入class文件中,或者是在运行时可以通过反射访问。
@Documented - 标记这些注解是否包含在用户文档中。
@Target - 标记这个注解应该是哪种 Java 成员。
@Inherited - 标记这个注解是继承于哪个注解类(默认 注解并没有继承于任何子类)
从 Java 7 开始,额外添加了 3 个注解:
@SafeVarargs - Java 7 开始支持,忽略任何使用参数为泛型变量的方法或构造函数调用产生的警告。
@FunctionalInterface - Java 8 开始支持,标识一个匿名函数或函数式接口。
@Repeatable - Java 8 开始支持,标识某注解可以在同一个声明上使用多次。

自定义注解

自定义注解必须添加两个元注解@Target 和 @Rentention
@Target 标记目标 标记这个注解是加载类 方法 字段 构造函数上
@Rentention 保留策略 标记这个注解的保留策略 是只在代码中,还是编入class文件中,或者是在运行时可以通过反射访问

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnoation {
    String value() default "";
    String des() default "我是一个自定义注解";

}

示例

新建注解

// 注释类上
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ClassAnnotation {
    String value() default "class default";
}
// 注释在变量上
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface SexFieldAnnotion {
    public enum GenderType {
        Male("男"),
        Female("女");
        private String genderStr;
        private GenderType(String arg0) {
            this.genderStr = arg0;
        }
        @Override
        public String toString() {
            return genderStr;
        }
    }
    GenderType gender() default GenderType.Male;
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface NameFieldAnnotion {
    public String value() default "";
}
// 在方法上
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodAnnotion {
    String value() default "default method value";
}

使用注解

@Getter
@Setter
@ClassAnnotation(value = "hello class")
public class Person {
    @NameFieldAnnotion("hello fild")
    public String name;
    @SexFieldAnnotion(gender = SexFieldAnnotion.GenderType.Male)
    public String sex;
    private String department;
    @MethodAnnotion(value = "hello method")
    public void print() {
        if(this.getClass().isAnnotationPresent(MethodAnnotion.class)){
            MethodAnnotion methodAnnotion = this.getClass().getAnnotation(MethodAnnotion.class);
            if("hello method".equals(methodAnnotion.value())){
                System.out.println("hello 方法");
            }
        }
        System.out.println(this.name+" "+this.sex);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", department='" + department + '\'' +
                '}';
    }
}

通过reflect获取我们标记的数据

public class AnnotionUtils {
    public static String getInfo(Class<?> cs) {
        StringBuilder result = new StringBuilder();
        // 获取类的注解的值
        ClassAnnotation classAnnotationAnnotion = cs.getAnnotation(ClassAnnotation.class);
        result.append("calss ").append(cs.getName()).append(":").append(classAnnotationAnnotion.value()).append("\n");
        //变量
        Field[] declaredFields = cs.getDeclaredFields();
        for (Field field : declaredFields) {
            if (field.isAnnotationPresent(NameFieldAnnotion.class)) {
                NameFieldAnnotion annotation = field.getAnnotation(NameFieldAnnotion.class);
                String value = annotation.value();
                result.append("field ").append(field.getName()).append(":").append(value).append("\n");
            }
            if (field.isAnnotationPresent(SexFieldAnnotion.class)) {
                SexFieldAnnotion annotation = field.getAnnotation(SexFieldAnnotion.class);
                String value = annotation.gender().name();
                result.append("field ").append(field.getName()).append(":").append(value).append("\n");
            }
        }
        // 方法名
        Method[] methods = cs.getMethods();
        for(Method method:methods){
            if(method.isAnnotationPresent(MethodAnnotion.class)){
                MethodAnnotion methodAnnotion = method.getAnnotation(MethodAnnotion.class);
                String value = methodAnnotion.value();
                result.append("method").append(method.getName()).append(":").append(value).append("\n");
            }
        }
        return result.toString();
    }

    public static void main(String[] args){
        String info = getInfo(Person.class);
        System.out.println(info);
        Person person = new Person();
    }
}

自定义注解的用图

使用自定义注解赋值

使用建造器模式,来创建类。在建造器中通过reflect获取注解

public class AnnotionPutVal {
    public static Person buildePerson(){
        Person person = new Person();
        Field[] fields = person.getClass().getFields();
        for(Field field:fields){
            if(field.isAnnotationPresent(NameFieldAnnotion.class)){
                NameFieldAnnotion nameFieldAnnotion = field.getAnnotation(NameFieldAnnotion.class);
                person.name = nameFieldAnnotion.value();
            }
            if(field.isAnnotationPresent(SexFieldAnnotion.class)){
                SexFieldAnnotion sexFieldAnnotion = field.getAnnotation(SexFieldAnnotion.class);
                person.sex = sexFieldAnnotion.gender().toString();
            }
        }
        if(person.getClass().isAnnotationPresent(ClassAnnotation.class)){
            ClassAnnotation classAnnotation = person.getClass().getAnnotation(ClassAnnotation.class);
            if("hello class".equals(classAnnotation.value())){
                person.setDepartment("hello");
            }
        }
        return person;
    }
    public static void main(String[] args) {
        Person person = buildePerson();
        System.out.println(person.toString());
        person.print();
    }
}

aop和过滤器与拦截器 整合

原理:aop和过滤器与拦截器都可以在运行时执行额外的代码块,我们在额外的代码块,读取我们添加注解的值。
与aop和过滤器/拦截器整合
具体实现,可以参考https://zhuanlan.zhihu.com/p/213923786

posted @ 2022-04-20 16:00  度一川  阅读(132)  评论(0)    收藏  举报