JavaBean的两种内省操作
来源:互联网 发布:电力大数据的几种形式 编辑:程序博客网 时间:2024/06/07 01:01
反射技术实际是已经能够完全满足我们对javaBean的各种操作了,但是为了方便JDK还是为我们提供了一套操纵JavaBean的API,我们称这套API为内省操作(Introspector)
下面介绍两种JavaBean的内省操作
先定义个JavaBean类
public class userbean {private int age;private String name;public userbean() {super();}public userbean(int age, String name) {super();this.age = age;this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
第一种简单内省
public class Introspector1 {/* * JavaBean的简单内省 */public static void main(String[] args) throws Exception {userbean bean = new userbean(20, "w");String propertyName = "name"; // 属性名// JavaBean的set方法String setValue = "wzl";setProperties(propertyName, bean, setValue);// JavaBean的get方法getProperties(propertyName, bean);}private static void getProperties(String propertyName, Object bean)throws Exception {PropertyDescriptor pd = new PropertyDescriptor(propertyName,bean.getClass());Method methodGet = pd.getReadMethod();Object retVal = methodGet.invoke(bean);System.out.println(retVal);}private static void setProperties(String propertyName, Object bean,Object setValue) throws Exception {PropertyDescriptor pd = new PropertyDescriptor(propertyName,bean.getClass());// 得到Javabean属性描述符Method methodSet = pd.getWriteMethod();methodSet.invoke(bean, setValue);}}
第二种复杂内省
public class Introspector2 {/* * JavaBean的复杂内省 */public static void main(String[] args) throws Exception {userbean bean = new userbean(20, "w");String propertyName = "name"; // 属性名// JavaBean的set方法String setValue = "wzl";setProperties(propertyName, bean, setValue);// JavaBean的get方法getProperties(propertyName, bean);}private static void getProperties(String propertyName, Object bean)throws Exception {// 把一个Java类当作JavaBean来看BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());// 得到所有的属性描述PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();Object retVal = new Object();for (PropertyDescriptor pd : pds) {if (pd.getName().equals(propertyName)) {Method methodGet = pd.getReadMethod();retVal = methodGet.invoke(bean);}}System.out.println(retVal);}private static void setProperties(String propertyName, Object bean,Object setValue) throws Exception {// 把一个Java类当作JavaBean来看BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());// 得到所有属性描述PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();for (PropertyDescriptor pd : pds) {if (pd.getName().equals(propertyName)) {Method methodSet = pd.getWriteMethod();methodSet.invoke(bean, setValue);}}}}
1 0
- JavaBean的两种内省操作
- JAVABean的内省操作
- javaBean的内省操作
- javaBean的内省操作
- javaBean的内省操作
- javaBean的内省操作
- JavaBean的内省操作
- JavaBean的内省操作
- javabean的内省操作
- JAVABean的内省操作
- JavaBean的简单内省操作
- 内省 操作javabean的属性
- JavaBean的复杂内省操作
- 内省操作javabean的属性
- 内省操作javabean的属性
- 30-31内省访问JavaBean属性的两种方法
- JavaBean内省:认识JavaBean与内省、对JavaBean的内省操作、Beanutils工具包
- JavaBean的简单内省操作 以及复杂内省操作
- 使用正则表达式导入RegexKitLite.m出现警告的问题
- Oracle数字、日期截取函数TRUNC
- iOS开发中的一些技巧
- UVa 12563 - Jin Ge Jin Qu hao(01背包)
- MySQL exists的用法介绍
- JavaBean的两种内省操作
- linux mint 安装搜狗输入法
- 硬盘的存储原理和内部架构
- HashMap的遍历两种常用的方法,那就是使用keyset及entryset来进行遍历
- touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event使用
- android导别人的项目,style报错
- Java实现的排序算法及比较 [冒泡,选择,插入,归并,希尔,快排]
- **DTD语法结构总结**
- 文件过滤的一些方式