• Java 内省机制


    概念

    JavaBean

    在实际编程中,我们常常需要一些用来包装值对象的类,例如Student、 Employee、Order,这些 类中往往没有业务方法,只是为了把需要处理的实体对象进行封装,有这样的特征:

    • 属性都是私有的

    • 有无参的public构造方法

    • 对私有属性根据需要提供公有的getXxx方法以及setXxx方法

      比如:属性名称为name,则有getName方法返回属性name值, setName方法设置name值;注意方法的名称通常是get或 set加上属性名称,并把属性名称的首字母大写;这些方法称为getters/setters;getters必须有返回值没有方法参数; setter值没有返回值,有方法参数;

      例如下面的例子:

    image-20220807140502129

    符合这些特征的类,被称为JavaBean

    内省

    内省(Inspector)机制就是基于反射的基础, Java语言对Bean类属性、事件的一种缺省处理方法

    只要类中有getXXX方法,或者setXXX方法,或者同时有getXXX及setXXX方法,其中getXXX方 法没有方法参数,有返回值; setXXX方法没有返回值,有一个方法参数;那么内省机制就认为 XXX为一个属性;

    例如下面代码

    Employee类中根本没有声明age属性,仅仅是声明了这样的getter和setter.内省机制就认为age是属性

    package com.shixun.introspector;
    
    public class Employee {
        private String name;
        private Double score;
    
        // age将被内省认为是属性
        public int getAge(){
            return 30;
        }
    
        // name将被内省认为是属性
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        // score将被内省认为是属性
        public Double getScore() {
            return score;
        }
    
        public void setScore(Double score) {
            this.score = score;
        }
    
        public static void main(String[] args) {
            
    
        }
    }
    
    
    • 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

    相关API

    与Java内省有关的主要类及接口有:

    • java.beans.Introspector类: 为获得JavaBean属性、事件、方法提供了标准方法;通常使用其中的getBeanInfo方法返回BeanInfo对象;

    • Java.beans.BeanInfo接口:不能直接实例化,通常通过Introspector类返回该类型对象,提供了返回属性描述符对象(PropertyDescriptor)、方法描述符对象(MethodDescriptor) 、 bean描述符(BeanDescriptor)对象的方法;

    • Java.beans.PropertyDescriptor类:用来描述一个属性,该属性有getter及setter方法;
      可以使用PropertyDescriptor类的方法获取属性相关的信息,例如getName方法返回属性的名字:

      PropertyDescriptor类中定义了方法可以获取该属性的getter和setter方法

      方法方法描述
      Method getReadMethod()回属性对应的getter方法对象;
      Method getWriteMethod()回属性对应的setter方法对象;

    下面我们来用代码深入探究一下:

    代码案例:获取属性相关信息

    Employee如上面代码所示,继续编写主函数进行测试

    首先用BeanInfo接口获取BeanInfo的对象,再通过BeanInfo对象获取PropertyDescriptor属性描述

     //获取BeanInfo的对象
     BeanInfo employeeBeanInfo = Introspector.getBeanInfo(Employee.class);
     //通过BeanInfo对象获取PropertyDescriptor属性描述
     PropertyDescriptor[] propertyDescriptors = employeeBeanInfo.getPropertyDescriptors();
     System.out.println("通过Inspector内省机制获取JavaBean属性======= 打印所有信息 ====================");
     Arrays.stream(propertyDescriptors).forEach(f->{
         System.out.println("====================================");
         System.out.println("属性名:"+f.getName());
         System.out.println("类型:"+f.getPropertyType());
         System.out.println("get方法:"+f.getReadMethod());
         System.out.println("set方法:"+f.getWriteMethod());
     });
    
    // 或者用增强for
    System.out.println("通过Inspector内省机制获取JavaBean属性======= 打印所有信息 ====================");
    for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
    	System.out.println("====================================");
        System.out.println("名字:" + propertyDescriptor.getName());
        System.out.println("类型:" + propertyDescriptor.getPropertyType());
        System.out.println("get方法:" + propertyDescriptor.getReadMethod());
        System.out.println("set方法:" + propertyDescriptor.getWriteMethod());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    运行结果如下:

    image-20220807152404930

    我们也可以通过反射调用这里获取的get或set方法

    //创建Employee的对象
    Class<?> clazz = Class.forName("com.shixun.introspector.Employee");
    Object employee = clazz.newInstance();
    
    //遍历属性描述对象
    for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
        //打印属性名称
        System.out.println(propertyDescriptor.getName());
        //判断属性名称是不是name
        if (propertyDescriptor.getName().equals("name")) {
            //setter方法
            Method writeMethod = propertyDescriptor.getWriteMethod();
            //调用setName方法
            writeMethod.invoke(employee, "jack");
            //getter方法
            Method readMethod = propertyDescriptor.getReadMethod();
            //调用getName方法
            Object nameValue = readMethod.invoke(employee);
            System.out.println("name属性的值为:" + nameValue);
        }
        //判断属性名称是否为score
        if (propertyDescriptor.getName().equals("score")) {
            //setter方法
            Method scoreWriteMethod = propertyDescriptor.getWriteMethod();
            //调用setScore方法
            scoreWriteMethod.invoke(employee, new Double(3000));
            //getter方法
            Method scoreReadMethod = propertyDescriptor.getReadMethod();
            Object scoreValue = scoreReadMethod.invoke(employee);
            System.out.println("score属性的值为:" + scoreValue);
        }
    }
    System.out.println("当前对象的信息:"+employee.toString());
    
    • 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

    运行结果如下所示:

    image-20220807152759503

    全部代码附在最下方!!!!!!

    内省属性的注意事项

    • 很多框架都使用了内省机制检索对象的属性,定义属性名字时,名字最好起码以两个小写字母开
      头,例如stuName,而不要使用sName,某些情况下,可能会导致检索属性失败;

    • 内省机制检索属性时,是根据getter和setter方法确认属性名字,而不是根据类里声明
      的成员变量名称决定;

    完整代码

    package com.shixun.introspector;
    
    import java.beans.BeanInfo;
    import java.beans.IntrospectionException;
    import java.beans.Introspector;
    import java.beans.PropertyDescriptor;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.Arrays;
    
    public class Employee {
        private String name;
        private Double score;
    
        // age将被内省认为是属性
        public int getAge() {
            return 30;
        }
    
        // name将被内省认为是属性
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        // score将被内省认为是属性
        public Double getScore() {
            return score;
        }
    
        public void setScore(Double score) {
            this.score = score;
        }
    
        @Override
        public String toString() {
            return "Employee{" +
                    "name='" + name + '\'' +
                    ", score=" + score +
                    '}';
        }
    
        public static void main(String[] args) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException {
            //获取BeanInfo的对象
            BeanInfo employeeBeanInfo = Introspector.getBeanInfo(Employee.class);
    
            //通过BeanInfo对象获取PropertyDescriptor属性描述
            PropertyDescriptor[] propertyDescriptors = employeeBeanInfo.getPropertyDescriptors();
    //        System.out.println("通过Inspector内省机制获取JavaBean属性======= 打印所有信息 ====================");
    //        Arrays.stream(propertyDescriptors).forEach(f->{
    //            System.out.println("====================================");
    //            System.out.println("属性名:"+f.getName());
    //            System.out.println("类型:"+f.getPropertyType());
    //            System.out.println("get方法:"+f.getReadMethod());
    //            System.out.println("set方法:"+f.getWriteMethod());
    //        });
    //
    //
    //
    //        System.out.println("通过Inspector内省机制获取JavaBean属性======= 打印所有信息 ====================");
    //
    //        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
    //            System.out.println("名字:" + propertyDescriptor.getName());
    //            System.out.println("类型:" + propertyDescriptor.getPropertyType());
    //            System.out.println("get方法:" + propertyDescriptor.getReadMethod());
    //            System.out.println("set方法:" + propertyDescriptor.getWriteMethod());
    //        }
    
            //创建Employee的对象
            Class<?> clazz = Class.forName("com.shixun.introspector.Employee");
            Object employee = clazz.newInstance();
    
            //遍历属性描述对象
            for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
                //打印属性名称
                System.out.println(propertyDescriptor.getName());
                //判断属性名称是不是name
                if (propertyDescriptor.getName().equals("name")) {
                    //setter方法
                    Method writeMethod = propertyDescriptor.getWriteMethod();
                    //调用setName方法
                    writeMethod.invoke(employee, "jack");
                    //getter方法
                    Method readMethod = propertyDescriptor.getReadMethod();
                    //调用getName方法
                    Object nameValue = readMethod.invoke(employee);
                    System.out.println("name属性的值为:" + nameValue);
                }
                //判断属性名称是否为score
                if (propertyDescriptor.getName().equals("score")) {
                    //setter方法
                    Method scoreWriteMethod = propertyDescriptor.getWriteMethod();
                    //调用setScore方法
                    scoreWriteMethod.invoke(employee, new Double(3000));
                    //getter方法
                    Method scoreReadMethod = propertyDescriptor.getReadMethod();
                    Object scoreValue = scoreReadMethod.invoke(employee);
                    System.out.println("score属性的值为:" + scoreValue);
                }
            }
    
            System.out.println("当前对象的信息:"+employee.toString());
        }
    }
    
    
    • 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
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
  • 相关阅读:
    【附源码】计算机毕业设计SSM数据分析教学网站
    快读《ASP.NET Core技术内幕与项目实战》EFCore2.5:集合查询原理揭秘IQueryable和IEnumerable
    Android学习---zygote(上)
    Python合并多Excel文件
    Elasticsearch之索引简单应用
    【JAVA】MyBatisPlus
    流水线作业
    spring mvc \ spring boot \ spring cloud
    深入理解 Spring 事务:入门、使用、原理
    矩阵分析与应用+张贤达
  • 原文地址:https://blog.csdn.net/weixin_45525272/article/details/126211840