浅谈Java内省机制
作者:流楚丶格念 发布时间:2021-06-13 17:54:05
概念
JavaBean
在实际编程中,我们常常需要一些用来包装值对象的类,例如Student、 Employee、Order,这些 类中往往没有业务方法,只是为了把需要处理的实体对象进行封装,有这样的特征:
属性都是私有的;
有无参的public构造方法;
对私有属性根据需要提供公有的getXxx方法以及setXxx方法;
比如:属性名称为name,则有getName方法返回属性name值, setName方法设置name值;注意方法的名称通常是get或 set加上属性名称,并把属性名称的首字母大写;这些方法称为getters/setters;getters必须有返回值没有方法参数; setter值没有返回值,有方法参数;
例如下面的例子:
符合这些特征的类,被称为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());
}
运行结果如下:
我们也可以通过反射调用这里获取的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());
}
}
来源:https://blog.csdn.net/weixin_45525272/article/details/126211840


猜你喜欢
- 理解函数式接口以及 Lambda表达式的发展过程任何接口,只包含唯一一个抽象方法,就是函数式接口/** * lambdab表达式的发展 */
- 一、首先编写一个工具类Hello:public class Hello { public static void say(Str
- 防止程序运行多个实例的方法有多种,如:通过使用互斥量和进程名等.而我想要实现的是:在程序运行多个实例时激活的是第一个实例,使其获得焦点,并在
- 最长公共子序列(Longest Common Subsequence)定义:两个或多个已知数列的子序列集合中最长的就是最长公共子序列。其实说
- 静态方法代理:public delegate void DoGreeting(string name);class Program 
- 属性CascadeType.REFRESH:级联刷新,当多个用户同时作操作一个实体,为了用户取到的数据是实时的,在用实体中的数据之前就可以调
- 安卓验证码的简单实现我们经常在登录或者注册的时候要求输入验证码,这里简单介绍一下一种方法 效果如下首先是要获取 随机的四个字母组合,我这里是
- 1、 WIFI网卡的状态WIFI网卡的状态信息都以整型变量的形式存放在 android.net.wifi.WifiManager 类中,有以
- 1.springboot使用log4j2springboot使用的common-logging,底层兼容各种日志框架如,log4j2,slf
- 一、背景今天有小伙伴面试的时候被问到:Spring AOP中JDK 和 CGLib * 哪个效率更高?二、基本概念首先,我们知道Sprin
- 通常我们遇到的图片缩放需求,都是图片基于屏幕自适应后,进行缩放和移动,且图片最小只能是自适应的大小。最近遇到一个需求,要求图片只能在屏幕内缩
- RoomRoom主要分三个部分 database、dao和实体类entityEntityentity实体类定义时需要用到@Entity(ta
- 前言我们都知道浮点型变量在进行计算的时候会出现丢失精度的问题。如下一段代码:System.out.println(0.05 + 0.01);
- 目前 Android 已经不推荐使用下列方式创建 Notification实例:Notification notification = ne
- 最近项目中使用springboot+jwt实现登录权限控制,所以在这里记录一下防止以后忘记,毕竟好记性不如烂笔头嘛~。首先我们需要导入使用到
- 本文实例讲述了C#设计模式之Mediator中介者模式解决程序员的七夕缘分问题。分享给大家供大家参考,具体如下:一、理论定义中介者模式&nb
- 1、概括在博客中,我们将讨论如何让Spring Security OAuth2实现使用JSON Web Tokens。2、Maven 配置首
- 本文实例讲述了Java使用excel工具类导出对象功能。分享给大家供大家参考,具体如下:package com.gcloud.common;
- SpringBoot配置https(SSL证书)最近在做微信小程序,https是必须条件仅需三步SpringBoot2.x版本对比一下这个小
- 记得老师讲课的时候,经常会用PPT遥控翻页笔来遥控幻灯片来给我们讲课,当时觉得非常有趣,由于这段时间接触了VSTO相关的开发,了解到了Off