WayneShao

获取类bean中的所有属性:

@Test
//获取类bean中的所有属性
public void test1() throws Exception{
    BeanInfo info = Introspector.getBeanInfo(Person.class);
    PropertyDescriptor[] decriptors = info.getPropertyDescriptors();
    for(PropertyDescriptor decriptor : decriptors){
        //输出属性的名称
        System.out.println(decriptor.getName());
        //输出属性的类型
        System.out.println(decriptor.getPropertyType());
    }
        
}

读/写bean中某个属性:

@Test
//操纵bean中某个属性
public void test2() throws Exception{
    Person p=new Person();
    
    PropertyDescriptor decriptor = new PropertyDescriptor("username",Person.class);
    
    //得到属性的写方法
    Method method=decriptor.getWriteMethod();
    method.invoke(p, "张三");

    //得到属性的读方法
    method=decriptor.getReadMethod();
    String username= (String) method.invoke(p);
    System.out.println(username);
}

分类:

技术点:

相关文章:

  • 2021-09-01
  • 2021-05-15
  • 2021-11-29
  • 2021-11-29
  • 2022-12-23
  • 2021-08-27
  • 2021-11-29
  • 2021-11-29
猜你喜欢
  • 2021-11-29
  • 2021-06-18
  • 2021-11-29
  • 2021-11-29
  • 2022-12-23
  • 2021-11-29
  • 2022-01-13
相关资源
相似解决方案