下列那个类有获取PropertyDescriptor实例的方法_java反射怎么理解

下列那个类有获取PropertyDescriptor实例的方法_java反射怎么理解JAVA中反射机制(JavaBean的内省与BeanUtils库)内省(Introspector)是Java语言对JavaBean类属性、事件的一种缺省处理方法。  JavaBean是一种特殊的类,主要用于传递数据信息,这种类中的方法主要用于访问私有的字段,且方法名符合某种命名规则。如果在两个模块之间传递信息,可以将信息封装进JavaBean中,这种对象称为“值对象”(ValueOb…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

JAVA中反射机制(JavaBean的内省与BeanUtils库)

内省(Introspector) 是Java 语言对JavaBean类属性、事件的一种缺省处理方法。
  JavaBean是一种特殊的类,主要用于传递数据信息,这种类中的方法主要用于访问私有的字段,且方法名符合某种命名规则。如果在两个模块之间传递信息,可以将信息封装进JavaBean中,这种对象称为“值对象”(Value Object),或“VO”。方法比较少。这些信息储存在类的私有变量中,通过set()、get()获得。
例如类UserInfo :

package com.peidasoft.instrospector;  

public class UserInfo {  

    private long userId;  
    private String userName;  
    private int age;  
    private String emailAddress;  

    public long getUserId() {  
        return userId;  
    }  

    public void setUserId(long userId) {  
        this.userId = userId;  
    }  

    public String getUserName() {  
        return userName;  
    }  

    public void setUserName(String userName) {  
        this.userName = userName;  
    }  

    public int getAge() {  
        return age;  
    }  

    public void setAge(int age) {  
        this.age = age;  
    }  

    public String getEmailAddress() {  
        return emailAddress;  
    }  

    public void setEmailAddress(String emailAddress) {  
        this.emailAddress = emailAddress;  
    }  
}


在类UserInfo中有属性userName,那我们可以通过getUserName, setUserName来得到其值或者设置新的值。通过getUserName/setUserName来访问userName属性,这就是默认的规则。Java JDK中提供了一套API用来访问某个属性的getter/setter方法,这就是内省。

JDK内省类库:

 PropertyDescriptor类:(属性描述器)
  PropertyDescriptor类表示JavaBean类通过存储器导出一个属性。主要方法:
  1. getPropertyType(),获得属性的Class对象;
  2. getReadMethod(),获得用于读取属性值的方法;
   3. getWriteMethod(),获得用于写入属性值的方法;
  4. hashCode(),获取对象的哈希值;
  5. setReadMethod(Method readMethod),设置用于读取属性值的方法;
  6. setWriteMethod(Method writeMethod),设置用于写入属性值的方法。
  实例代码如下:

package com.peidasoft.instrospector;  

import java.beans.BeanInfo;  
import java.beans.Introspector;  
import java.beans.PropertyDescriptor;  
import java.lang.reflect.Method;  

public class BeanInfoUtil { 
     

    // 设置bean的某个属性值 
    public static void setProperty(UserInfo userInfo, String userName) throws Exception {  
        // 获取bean的某个属性的描述符 
        PropertyDescriptor propDesc = new PropertyDescriptor(userName, UserInfo.class);  
        // 获得用于写入属性值的方法 
        Method methodSetUserName = propDesc.getWriteMethod();  
        // 写入属性值 
        methodSetUserName.invoke(userInfo, "wong");  
        System.out.println("set userName:" + userInfo.getUserName());  
    }  

    // 获取bean的某个属性值 
    public static void getProperty(UserInfo userInfo, String userName) throws Exception {  
        // 获取Bean的某个属性的描述符 
        PropertyDescriptor proDescriptor = new PropertyDescriptor(userName, UserInfo.class);  
        // 获得用于读取属性值的方法 
        Method methodGetUserName = proDescriptor.getReadMethod();  
        // 读取属性值 
        Object objUserName = methodGetUserName.invoke(userInfo);  
        System.out.println("get userName:" + objUserName.toString());  
    }  
}

Introspector类:
  将JavaBean中的属性封装起来进行操作。在程序把一个类当做JavaBean来看,就是调用Introspector.getBeanInfo()方法,得到的BeanInfo对象封装了把这个类当做JavaBean看的结果信息,即属性的信息。
  getPropertyDescriptors(),获得属性的描述,可以采用遍历BeanInfo的方法,来查找、设置类的属性。具体代码如下:

import java.beans.BeanInfo;  
import java.beans.Introspector;  
import java.beans.PropertyDescriptor;  
import java.lang.reflect.Method;  

public class BeanInfoUtil { 
     

    // 通过内省设置bean的某个属性值 
    public static void setPropertyByIntrospector(UserInfo userInfo, String userName) throws Exception {  
        // 获取bean信息 
        BeanInfo beanInfo = Introspector.getBeanInfo(UserInfo.class);  
        // 获取bean的所有属性列表 
        PropertyDescriptor[] proDescrtptors = beanInfo.getPropertyDescriptors();  
        // 遍历属性列表,查找指定的属性 
        if (proDescrtptors != null && proDescrtptors.length > 0) {  
            for (PropertyDescriptor propDesc : proDescrtptors) {  
                // 找到则写入属性值 
                if (propDesc.getName().equals(userName)) {  
                    Method methodSetUserName = propDesc.getWriteMethod();  
                    methodSetUserName.invoke(userInfo, "alan");  // 写入属性值 
                    System.out.println("set userName:" + userInfo.getUserName());  
                    break;  
                }  
            }  
        }  
    }  

    // 通过内省获取bean的某个属性值 
    public static void getPropertyByIntrospector(UserInfo userInfo, String userName) throws Exception {  
        BeanInfo beanInfo = Introspector.getBeanInfo(UserInfo.class);  
        PropertyDescriptor[] proDescrtptors = beanInfo.getPropertyDescriptors();  
        if (proDescrtptors != null && proDescrtptors.length > 0) {  
            for (PropertyDescriptor propDesc : proDescrtptors) {  
                if (propDesc.getName().equals(userName)) {  
                    Method methodGetUserName = propDesc.getReadMethod();  
                    Object objUserName = methodGetUserName.invoke(userInfo);  
                    System.out.println("get userName:" + objUserName.toString());  
                    break;  
                }  
            }  
        }  
    }  
}

通过这两个类的比较可以看出,都是需要获得PropertyDescriptor,只是方式不一样:前者通过创建对象直接获得,后者需要遍历,所以使用PropertyDescriptor类更加方便。

package com.peidasoft.instrospector;  

public class BeanInfoTest { 
     

    /** * @param args the command line arguments */  
    public static void main(String[] args) {  
        UserInfo userInfo = new UserInfo();  
        userInfo.setUserName("peida");  
        try {  
            BeanInfoUtil.getProperty(userInfo, "userName");  
            BeanInfoUtil.setProperty(userInfo, "userName");  
            BeanInfoUtil.getProperty(userInfo, "userName");  
            BeanInfoUtil.setPropertyByIntrospector(userInfo, "userName");  
            BeanInfoUtil.getPropertyByIntrospector(userInfo, "userName");  
            BeanInfoUtil.setProperty(userInfo, "age");  // IllegalArgumentException 
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}

输出结果:

get userName:peida  
set userName:wong  
get userName:wong  
set userName:alan  
get userName:alan  
java.lang.IllegalArgumentException: argument type mismatch  
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)  
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
    at java.lang.reflect.Method.invoke(Method.java:483)  
    at com.peidasoft.instrospector.BeanInfoUtil.setProperty(BeanInfoUtil.java:22)  
    at com.peidasoft.instrospector.BeanInfoTest.main(BeanInfoTest.java:26)

说明:BeanInfoUtil.setProperty(userInfo,”age”);报错是应为age属性是int数据类型,而setProperty方法里面默认给age属性赋的值是String类型。所以会爆出argument type mismatch参数类型不匹配的错误信息。


 BeanUtils工具包:
  由上述可看出,内省操作非常的繁琐,所以所以Apache开发了一套简单、易用的API来操作Bean的属性——BeanUtils工具包。
  BeanUtils工具包:下载:http://commons.apache.org/beanutils/,注意:应用的时候还需要一个logging包http://commons.apache.org/logging/
  使用BeanUtils工具包完成上面的测试代码:

package com.peidasoft.instrospector;  

import java.lang.reflect.InvocationTargetException;  
import org.apache.commons.beanutils.BeanUtils;  
import org.apache.commons.beanutils.PropertyUtils;  

public class BeanInfoTest {  

    /** * @param args the command line arguments */  
    public static void main(String[] args) {  
        UserInfo userInfo = new UserInfo();  
        userInfo.setUserName("peida");  
        try {  
            BeanUtils.setProperty(userInfo, "userName", "peida");  
            System.out.println("set userName:" + userInfo.getUserName());  
            System.out.println("get userName:" + BeanUtils.getProperty(userInfo, "userName"));  
            BeanUtils.setProperty(userInfo, "age", 18);  
            System.out.println("set age:" + userInfo.getAge());  
            System.out.println("get age:" + BeanUtils.getProperty(userInfo, "age"));  
            System.out.println("get userName type:" + BeanUtils.getProperty(userInfo, "userName").getClass().getName());  
            System.out.println("get age type:" + BeanUtils.getProperty(userInfo, "age").getClass().getName());  
            PropertyUtils.setProperty(userInfo, "age", 8);  
            System.out.println(PropertyUtils.getProperty(userInfo, "age"));  
            System.out.println(PropertyUtils.getProperty(userInfo, "age").getClass().getName());  
            PropertyUtils.setProperty(userInfo, "age", "8");  // IllegalArgumentException 
        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {  
            e.printStackTrace();  
        }  
    }  
}  
运行结果:
[java] view plain copy
set userName:peida  
get userName:peida  
set age:18  
get age:18  
get userName type:java.lang.String  
get age type:java.lang.String  

java.lang.Integer  
Exception in thread "main" java.lang.IllegalArgumentException: Cannot invoke com.peidasoft.instrospector.UserInfo.setAge on bean class   
    'class com.peidasoft.instrospector.UserInfo' - argument type mismatch - had objects of type "java.lang.String" but expected signature "int"  
    at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2181)  
    at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2097)  
    at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1903)  
    at org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:2010)  
    at org.apache.commons.beanutils.PropertyUtils.setProperty(PropertyUtils.java:896)  
    at com.peidasoft.instrospector.BeanInfoTest.main(BeanInfoTest.java:32)  
Caused by: java.lang.IllegalArgumentException: argument type mismatch  
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)  
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
    at java.lang.reflect.Method.invoke(Method.java:483)  
    at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2116)  
    ... 5 more

说明:
  1. 获得属性的值,例如,BeanUtils.getProperty(userInfo, “userName”),返回字符串。
  2. 设置属性的值,例如,BeanUtils.setProperty(userInfo, “age”, 8),参数是字符串或基本类型自动包装。设置属性的值是字符串,获得的值也是字符串,不是基本类型。   3. BeanUtils的特点:
  1). 对基本数据类型的属性的操作:在WEB开发、使用中,录入和显示时,值会被转换成字符串,但底层运算用的是基本类型,这些类型转到动作由BeanUtils自动完成。
  2). 对引用数据类型的属性的操作:首先在类中必须有对象,不能是null,例如,private Date birthday=new Date();。操作的是对象的属性而不是整个对象,例如,BeanUtils.setProperty(userInfo, “birthday.time”, 111111);

package com.peidasoft.Introspector;  
import java.util.Date;  

public class UserInfo { 
     

    private Date birthday = new Date(); // 引用类型的属性,不能为null 

    public void setBirthday(Date birthday) {  
        this.birthday = birthday;  
    }  
    public Date getBirthday() {  
        return birthday;  
    }        
}

package com.peidasoft.Beanutil; 

import java.lang.reflect.InvocationTargetException; 
import org.apache.commons.beanutils.BeanUtils; 
import com.peidasoft.Introspector.UserInfo; 

public class BeanUtilTest {  
    public static void main(String[] args) {  
        UserInfo userInfo=new UserInfo(); 
         try {  
            BeanUtils.setProperty(userInfo, "birthday.time","111111"); // 操作对象的属性,而不是整个对象 
            Object obj = BeanUtils.getProperty(userInfo, "birthday.time"); 
            System.out.println(obj); 
        }   
         catch (IllegalAccessException e) {  
            e.printStackTrace(); 
        }   
         catch (InvocationTargetException e) {  
            e.printStackTrace(); 
        }  
        catch (NoSuchMethodException e) {  
            e.printStackTrace(); 
        }  
    }  
}

PropertyUtils类和BeanUtils不同在于,运行getProperty、setProperty操作时,没有类型转换,使用属性的原有类型或者包装类。由于age属性的数据类型是int,所以方法PropertyUtils.setProperty(userInfo,”age”, “8”)会爆出数据类型不匹配,无法将值赋给属性。


2018.8.27

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/187187.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 舆情监测分析系统_舆情监测系统

    舆情监测分析系统_舆情监测系统一、引言1.1目的  编写此文档的目的是确认舆情分析系统的需求及系统边界,指导系统的设计。1.2项目信息项目名称:舆情分析系统项目提出者:指导教师开发者:东北大学软件学院大数据班T09实训项目组(lzf、lcx)用户:舆情分析员、系统管理员1.3缩写说明1.4术语定义1.5参考资料新浪舆情通:https://yqt.mdata.net/二、舆情分析系统概述2.1舆情分析系统介绍  我们的舆情分析系统主要包括舆情总缆分析、舆情搜索、文章分析、文章评论分析、事件

    2025年11月21日
    2
  • C++11 decltype 的用法

    C++11 decltype 的用法文章目录decltype的意义decltype的用法1.推导规则2.举例说明3.模版案例更多细节问题C++14取消decltype其他decltype的意义参考博客:C++11新标准:decltype关键字有时我们希望从表达式的类型推断出要定义的变量类型,但是不想用该表达式的值初始化变量(如果要初始化就用auto了)。为了满足这一需求,C++11新标准引入了decl…

    2025年9月6日
    4
  • idea激活码2021-激活码分享

    (idea激活码2021)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~MLZP…

    2022年3月21日
    69
  • 约束条件(constraint)「建议收藏」

    约束条件(constraint)「建议收藏」1.为啥使用约束条件:约束条件也叫完整性约束条件,当对表中的数据做DML操作时会验证数据是否违反约束条件.如果违反了DML操作会失败.约束条件可以应用于表中的一列或几列,应用于整个表或几个表之间.约束条件分类:非空(NOTNULL),唯一(UNIQUE),主键(PRIMARYKEY),外键(FOREIGNKEY),检查(CHECK).其中NOTNULL只能应用于列.

    2022年10月13日
    2
  • 汇编语言王爽第四版实验4答案_王爽汇编语言实验11

    汇编语言王爽第四版实验4答案_王爽汇编语言实验11汇编语言王爽第四版课后检测点课后实验持续更新~~实验4[bx]和loop的使用1编程,向内存0:200~0:23f依次传送数据0~63(3fh)assumecs:codecodesegment movax,0 movds,ax;设置ds=0 movbx,200h;设置从200h开始 movcx,64;循环64次 moval,0;传送的数据 s: mov[bx],al incbx incal loops movax,4c00h

    2025年6月21日
    2
  • 蓝桥杯单片机必备知识—–(4)pcf8591–DAC

    蓝桥杯单片机必备知识—–(4)pcf8591–DAC

    2021年4月13日
    810

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号