• 获取注解信息


    一、案例

    package com.massimo.reflection;
    
    
    import java.lang.annotation.*;
    import java.lang.reflect.Field;
    
    //练习反射操作注解
    public class Test10 {
    
        public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
            Class c1 = Class.forName("com.massimo.reflection.Student2");
    
            //通过反射获得注解
            Annotation[] annotations = c1.getAnnotations();
            for (Annotation annotation : annotations) {
                System.out.println(annotation);
            }
    
            //获得注解的value的值
            TableMassimo tableMassimo = (TableMassimo) c1.getAnnotation(TableMassimo.class);
            String value = tableMassimo.value();
            System.out.println(value);
    
            //获得类指定的注解
            Field f = c1.getDeclaredField("name");
            FieldMassimo annotation = f.getAnnotation(FieldMassimo.class);
            System.out.println(annotation.columnName());
            System.out.println(annotation.type());
            System.out.println(annotation.length());
        }
    }
    
    @TableMassimo("db_student")
    class Student2{
    
        @FieldMassimo(columnName = "db_id",type = "int",length = 10)
        private int id;
        @FieldMassimo(columnName = "db_age",type = "int",length = 10)
        private int age;
        @FieldMassimo(columnName = "db_name",type = "varchar",length = 3)
        private String name;
    
        public Student2() {
        }
    
        public Student2(int id, int age, String name) {
            this.id = id;
            this.age = age;
            this.name = name;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "Student2{" +
                    "id=" + id +
                    ", age=" + age +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
    
    //类名的注解
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @interface TableMassimo{
        String value();
    }
    
    //属性的注解
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @interface FieldMassimo{
        String columnName();
        String type();
        int length();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100

    效果:
    在这里插入图片描述

  • 相关阅读:
    JSR303和拦截器
    艾美捷PEG-2000 DMG解决方案
    macbook m1芯片 实现vscode下debug(解决无法读入的问题)
    MySQL--- DQL查询数据信息
    SpringBoot配置文件优先级
    微软确认:测试版用户可在Win11上运行Android应用
    JavaScript实现登录框定位
    gprof 分析程序执行时间和函数调用次数
    synchronized:解决死锁的问题
    倍福PLC基于CX5130实现数据的断电保持
  • 原文地址:https://blog.csdn.net/Massimo__JAVA/article/details/126961708