Java 内省introspector相关原理代码解析

所属分类: 软件编程 / java 阅读数: 89
收藏 0 赞 0 分享

1. JavaBean (有get/set属性,和默认构造器等规范的java类)

import java.util.Date;

public class Student {
  // 这是 字段
  private String name;
  private int age;
  private Date birthday;

  // 这是 属性 
  //(get、set开头的方法,getName、setName算一个属性,单独一个set或get也算一个属性)
  // 属性名为 去掉get、set后 第一个大写字母变小写字母。
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }
  
  public int getAbc(){ //注意这也是一个属性,属性名为 abc
    return 10;
  }
  /*
  public int getefg(){ //注意这也是一个属性,属性名为 efg
    return 10;
  }*/

  public Date getBirthday() {
    return birthday;
  }

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

}

测试

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;

public class Test1 {
  public static void main(String[] args) throws Exception {
    test05();
  }

  // 获取属性描述器 Introspector.getBeanInfo(Student.class).getPropertyDescriptors();
  private static void test01() throws Exception {
    BeanInfo bf = Introspector.getBeanInfo(Student.class);
    PropertyDescriptor[] pds = bf.getPropertyDescriptors();
    for (PropertyDescriptor pd : pds) {
      System.out.println(pd.getName());
    }
    /*
      abc
      age
      class //这个是Object类里的
      name
     */
  }
  
  // 使用内省 调用set、get方法
  private static void test02() throws Exception {
    Student stu = new Student();
    PropertyDescriptor pd = new PropertyDescriptor("name", Student.class);
    Method setter = pd.getWriteMethod();
    setter.invoke(stu, "tom");
    Method getter = pd.getReadMethod();
    System.out.println(getter.invoke(stu));
  }
  
  
  /**
   * 以上使用的 java源码里的 java.beans包
   * 接下来有更方便的,Apache 组织提供的 commons-beanutils-1.8.3.jar 
   * 导入:commons-beanutils-1.8.3.jar commons-logging-1.1.1.jar
   */
  private static void test03() throws Exception{
    Student stu = new Student();
    BeanUtils.setProperty(stu, "name", "白居易");
    System.out.println(stu.getName());
    String name = BeanUtils.getProperty(stu, "name");
    System.out.println(name);
    //BeanUtils 支持8中基本类型 自动转换
    BeanUtils.setProperty(stu, "age", 19);
    BeanUtils.setProperty(stu, "age", "18");
    System.out.println(stu.getAge());
    //PropertyUtils.setSimpleProperty(stu, name, value);
  }
  
  private static void test04() throws Exception{
    Student stu = new Student();
    //set/get 日期 Date
    ConvertUtils.register(new DateLocaleConverter(), Date.class);
    BeanUtils.setProperty(stu, "birthday", "1999-11-10");
    System.out.println(stu.getBirthday());
    String s = BeanUtils.getProperty(stu, "birthday");
    System.out.println(s);
  }
  
  /**
   * 一下整个赋值给 javaBean 对象,使用 BeanUtils.populate
   * @throws Exception
   */
  private static void test05() throws Exception{
    Student stu = new Student();
    Map m = new HashMap();
    m.put("name", "Lee");//注意:key名一定要与对象中的变量名一致
    m.put("age", "18");//注意:key名一定要与对象中的变量名一致
    m.put("birthday", "2020-7-4");//注意:key名一定要与对象中的变量名一致
    
    ConvertUtils.register(new DateLocaleConverter(), Date.class);
    BeanUtils.populate(stu, m);
    System.out.println(stu.getBirthday());
    
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多