浅谈Java内省机制

 更新时间:2022年08月08日 09:53:18   作者:流楚丶格念  
本文主要介绍了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) {
        

    }
}

相关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());
}

运行结果如下:

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());

运行结果如下所示:

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

内省属性的注意事项

  • 很多框架都使用了内省机制检索对象的属性,定义属性名字时,名字最好起码以两个小写字母开头,例如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());
    }
}

到此这篇关于浅谈Java内省机制的文章就介绍到这了,更多相关Java内省机制内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java判断integer是否为空的详细过程

    java判断integer是否为空的详细过程

    在java编写过程中,我们会使用到各种各样的表达式,在使用表达式的过程中,有哪些安全问题需要我们注意的呢?对java判断integer是否为空相关知识感兴趣的朋友一起来看看吧
    2023-02-02
  • Spring Boot中单例类实现对象的注入方式

    Spring Boot中单例类实现对象的注入方式

    这篇文章主要介绍了Spring Boot中单例类实现对象的注入方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • 实例讲解JAVA 模板方法模式

    实例讲解JAVA 模板方法模式

    这篇文章主要介绍了JAVA 模板方法模式的的相关资料,文中示例代码非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06
  • Dubbo异步调用的实现介绍

    Dubbo异步调用的实现介绍

    dubbo默认使用同步的方式调用。但在有些特殊的场景下,我们可能希望异步调用dubbo接口,从而避免不必要的等待时间,这时候我们就需要用到异步。那么dubbo的异步是如何实现的呢?下面就来看看这个问题
    2022-09-09
  • Java 基础:string中的compareTo方法

    Java 基础:string中的compareTo方法

    这篇文章主要介绍了Java 基础:string中的compareTo方法,文章围绕string中的compareTo方法的相关资料展开文章详细内容,希望对待大家有所帮助
    2021-12-12
  • springboot中请求路径配置在配置文件中详解

    springboot中请求路径配置在配置文件中详解

    这篇文章主要介绍了springboot中请求路径配置在配置文件中,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Java实现递归山脉

    Java实现递归山脉

    这篇文章主要为大家详细介绍了Java实现递归山脉,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • 浅析SpringBoot2.4 静态资源加载问题

    浅析SpringBoot2.4 静态资源加载问题

    这篇文章主要介绍了SpringBoot2.4 静态资源加载问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • Spring容器初始化及问题解决方案

    Spring容器初始化及问题解决方案

    这篇文章主要介绍了Spring容器初始化及问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • Java中clone方法使用笔记

    Java中clone方法使用笔记

    clone顾名思义是复制,在Java语言中,clone方法被对象调用,所以会复制对象,下面这篇文章主要给大家介绍了关于Java中clone方法使用的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-02-02

最新评论