Java内省和beanUtil
来源:互联网 发布:winner2016淘宝造物节 编辑:程序博客网 时间:2024/06/11 14:09
Java内省和beanUtil
Java内省
- 为什么要学内省
开发框架时,经常需要使用java对象的属性来封装程序的数据,每次都使用反射技术完成此类操作过于麻烦,所以sun公司开发了一套API,专门用于操作java对象的属性。内省是用于操作java对象的属性的,那么以下问题我们必须要清楚
问题一:什么是Java对象的属性和属性的读写方法?
问题二: 如何通过内省访问到javaBean的属性 ?
- 通过PropertyDescriptor类操作Bean的属性.
publicstaticvoid testPropertyDescriptor() throws Exception{Person p = new Person();PropertyDescriptor propertyDescriptor = new PropertyDescriptor("id",Person.class);//获取属性的写的方法。Method writeMethod = propertyDescriptor.getWriteMethod();Method readMethod = propertyDescriptor.getReadMethod();propertyDescriptor.getReadMethod();writeMethod.invoke(p, 12);System.out.println(readMethod.invoke(p, null));}
- 通过Introspector类获得Bean对象的 BeanInfo,然后通过 BeanInfo 来获取属性的描述器( PropertyDescriptor ),通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后通过反射机制来调用这些方法。
publicstaticvoid testIntrospector() throws Exception{BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);PropertyDescriptor[] descriptor = beanInfo.getPropertyDescriptors();for(PropertyDescriptor itemProperty : descriptor){System.out.println(itemProperty.getReadMethod().getName());}}
BeanUtils
- 存在的问题 sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景开发了一套简单、易用的API操作Bean的属性——BeanUtils。
publicstaticvoid main(String[] args) throws Exception {Person p = new Person();ConvertUtils.register(new Converter() {@OverridepublicObject convert(Class type, Object value) { try {if(value!=null){ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MM dd"); Date d = dateFormat.parse((String) value); return d; }} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}}, Date.class);BeanUtils.setProperty(p,"id","110");BeanUtils.setProperty(p,"name","狗娃");BeanUtils.setProperty(p, "birthDay","1992 12 12");System.out.println(p.getId() +"=="+ p.getName()+"======"+p.getBirthDay());}
开发框架时,经常需要使用java对象的属性来封装程序的数据,每次都使用反射技术完成此类操作过于麻烦,所以sun公司开发了一套API,专门用于操作java对象的属性。内省是用于操作java对象的属性的,那么以下问题我们必须要清楚
问题一:什么是Java对象的属性和属性的读写方法?
问题二: 如何通过内省访问到javaBean的属性 ?
publicstaticvoid testPropertyDescriptor() throws Exception{Person p = new Person();PropertyDescriptor propertyDescriptor = new PropertyDescriptor("id",Person.class);//获取属性的写的方法。Method writeMethod = propertyDescriptor.getWriteMethod();Method readMethod = propertyDescriptor.getReadMethod();propertyDescriptor.getReadMethod();writeMethod.invoke(p, 12);System.out.println(readMethod.invoke(p, null));}
publicstaticvoid testIntrospector() throws Exception{BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);PropertyDescriptor[] descriptor = beanInfo.getPropertyDescriptors();for(PropertyDescriptor itemProperty : descriptor){System.out.println(itemProperty.getReadMethod().getName());}}
publicstaticvoid main(String[] args) throws Exception {Person p = new Person();ConvertUtils.register(new Converter() {@OverridepublicObject convert(Class type, Object value) { try {if(value!=null){ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MM dd"); Date d = dateFormat.parse((String) value); return d; }} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}}, Date.class);BeanUtils.setProperty(p,"id","110");BeanUtils.setProperty(p,"name","狗娃");BeanUtils.setProperty(p, "birthDay","1992 12 12");System.out.println(p.getId() +"=="+ p.getName()+"======"+p.getBirthDay());}
0 0
- Java内省和beanUtil
- 黑马程序员--内省、javaBean以及BeanUtil
- java 反射和内省
- java反射和内省
- java反射和内省
- java反射和内省
- java反射和内省
- java的内省和反射
- java之反射和内省
- java的反射和内省
- BeanUtil
- BeanUtil
- BeanUtil
- BeanUtil
- BeanUtil
- BeanUtil
- BeanUtil
- beanutil
- ArrayList集合内容查重
- "巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场(重现) HDU 5707
- self/super关键字/实例变量修饰符/description
- [INS-30131] 执行安装程序验证所需的初始设置失败[绝对解决]
- C++第6次作业
- Java内省和beanUtil
- tableau sdk java的测试
- Impala与Hive的比较
- iOS 证书权限分配
- 剑指offer19--依次打印队列二叉树中的元素
- 简单样例之:div+css+javascript+php+mysql
- LeetCode Largest Number
- ptrace基础
- 贪心 ? OR DP?