博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[新手学Java]使用内省(Introspector)操作JavaBean属性
阅读量:7103 次
发布时间:2019-06-28

本文共 860 字,大约阅读时间需要 2 分钟。

获取类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);}

转载于:https://www.cnblogs.com/WayneShao/p/5929817.html

你可能感兴趣的文章
《Struts2技术内幕》学习笔记
查看>>
开发指南专题八:JEECG微云高速开发平台数据字典
查看>>
CI框架 -- 核心文件 之 Output.php(输出类文件)
查看>>
动态更换view类的背景----StateListDrawable的应用
查看>>
scrapy-redis实现爬虫分布式爬取分析与实现
查看>>
Android仿微信UI布局视图(圆角布局的实现)
查看>>
docker
查看>>
OKR 方法 学习笔记
查看>>
CG资源网 - Maya教程
查看>>
http://blog.sina.com.cn/s/blog_62e1faba010147k4.html
查看>>
CSS默认可继承样式
查看>>
数据库中树形结构的表的设计
查看>>
关于Cocos2d-x的瓦片地图
查看>>
位置无关码
查看>>
find-k-pairs-with-smallest-sums
查看>>
情绪板携手视觉设计
查看>>
Atitit.php nginx页面空白 并返回500的解决
查看>>
http://blog.csdn.net/LANGXINLEN/article/details/50421988
查看>>
PHP高效率写法(详解原因)
查看>>
Swift 值类型/引用类型
查看>>