一些操作JavaBean的方法
2016-06-10 00:04
344 查看
关于JavaBean的介绍就不说了,直接上代码
测试类
Person实体:
自己封装的操作JavaBean的工具类BeanUtil
package com.home.util;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
*
* @description 关于JavaBean的一些操作方法
* @author fj
* @createTime 2016年6月9日
*/
public class BeanUtil {
/**
*
* @description 使用PropertyDescriptor获取JavaBean对象的指定属性的值
* @param obj
* JavaBean对象
* @param propertyName
* 属性名
* @return get方法返回的值
* @throws IntrospectionException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static Object getProperty(Object obj, String propertyName) throws IntrospectionException, IllegalAccessException, InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass());// 获取javaBean的属性描述
Method methodGet = pd.getReadMethod(); // 得到get方法
if (methodGet != null) {
return methodGet.invoke(obj); // 调用get方法
}
return null;
}
/**
*
* @description 使用PropertyDescriptor设置JavaBean对象的指定属性的值
* @param obj
* JavaBean对象
* @param propertyName
* 属性名
* @param value
* 需要设置的值
* @throws IntrospectionException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void setProperty(Object obj, String propertyName, Object value)
throws IntrospectionException, IllegalAccessException, InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass());// 获取javaBean的属性描述
Method methodSet = pd.getWriteMethod(); // 得到set方法
if (methodSet != null) {
methodSet.invoke(obj, value);// 调用set方法
}
}
/**
*
* @description 把一个对象转换为map,key为字段,value为字段的值
* @param obj JavaBean对象
* @return map
*/
public static Map<String, Object> beanToMap(Object obj) {
Map<String, Object> result = new HashMap<String, Object>();
if (obj == null) {
return result;
}
try {
Class<?> clazz = obj.getClass();
BeanInfo info = Introspector.getBeanInfo(clazz);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
String propertyName = pd.getName();
if (propertyName.equals("class")) {
continue;
}
Method methodGet = pd.getReadMethod();
if (methodGet != null) {
result.put(propertyName, methodGet.invoke(obj));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
测试类
package com.home.test; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.PropertyUtils; import com.home.util.BeanUtil; public class Main { public static void main(String[] args) throws Exception { Person person = new Person("小飞", 23); String propertyName1 = "name"; String propertyName2 = "age"; // 这是属性链,birthday是Date类型的,Date中有setTime(long time)方法 String propertyName3 = "birthday.time"; // 1.以下使用PropertyDescriptor操作JavaBean BeanUtil.setProperty(person, propertyName1, "小明"); System.out.println(BeanUtil.getProperty(person, propertyName1)); // 2.使用BeanInfo操作JavaBean,方法里面一样可以获取和设置属性值 System.out.println(BeanUtil.beanToMap(person)); // 3.使用apache BeanUtils操作JavaBean BeanUtils.setProperty(person, propertyName1, "小红"); BeanUtils.setProperty(person, propertyName3, "1111112222"); System.out.println(BeanUtils.getProperty(person, propertyName1)); System.out.println(BeanUtils.getProperty(person, propertyName3)); // BeanUtils操作中参数是String类型的 返回结果也是String类型 System.out.println(BeanUtils.getProperty(person, propertyName3).getClass().getName()); // 4.使用apache PropertyUtils操作JavaBean PropertyUtils.setProperty(person, propertyName1, "小高"); PropertyUtils.setProperty(person, propertyName2, 25); PropertyUtils.setProperty(person, propertyName3, 1111111111); System.out.println(PropertyUtils.getProperty(person, propertyName1)); System.out.println(PropertyUtils.getProperty(person, propertyName2)); System.out.println(PropertyUtils.getProperty(person, propertyName3)); // 操作的是什么类型就返回什么类型 System.out.println(PropertyUtils.getProperty(person, propertyName2).getClass().getName()); } }
Person实体:
package com.home.test; import java.util.Date; public class Person { private String name; private int age; private Date birthday = new Date();// 这里要创建对象! public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Person(String name, int age) { super(); this.name = name; this.age = age; } 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; } @Override public String toString() { return this.name + " " + this.age + " " + this.birthday; } }
自己封装的操作JavaBean的工具类BeanUtil
package com.home.util;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
*
* @description 关于JavaBean的一些操作方法
* @author fj
* @createTime 2016年6月9日
*/
public class BeanUtil {
/**
*
* @description 使用PropertyDescriptor获取JavaBean对象的指定属性的值
* @param obj
* JavaBean对象
* @param propertyName
* 属性名
* @return get方法返回的值
* @throws IntrospectionException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static Object getProperty(Object obj, String propertyName) throws IntrospectionException, IllegalAccessException, InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass());// 获取javaBean的属性描述
Method methodGet = pd.getReadMethod(); // 得到get方法
if (methodGet != null) {
return methodGet.invoke(obj); // 调用get方法
}
return null;
}
/**
*
* @description 使用PropertyDescriptor设置JavaBean对象的指定属性的值
* @param obj
* JavaBean对象
* @param propertyName
* 属性名
* @param value
* 需要设置的值
* @throws IntrospectionException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void setProperty(Object obj, String propertyName, Object value)
throws IntrospectionException, IllegalAccessException, InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass());// 获取javaBean的属性描述
Method methodSet = pd.getWriteMethod(); // 得到set方法
if (methodSet != null) {
methodSet.invoke(obj, value);// 调用set方法
}
}
/**
*
* @description 把一个对象转换为map,key为字段,value为字段的值
* @param obj JavaBean对象
* @return map
*/
public static Map<String, Object> beanToMap(Object obj) {
Map<String, Object> result = new HashMap<String, Object>();
if (obj == null) {
return result;
}
try {
Class<?> clazz = obj.getClass();
BeanInfo info = Introspector.getBeanInfo(clazz);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
String propertyName = pd.getName();
if (propertyName.equals("class")) {
continue;
}
Method methodGet = pd.getReadMethod();
if (methodGet != null) {
result.put(propertyName, methodGet.invoke(obj));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
相关文章推荐
- Spring--REST
- 初步学习使用MAVEN和IDEA写第一个JAVA程序
- myeclipse 删除历史工作空间记录
- spring重定向传参
- JAVA--IO
- 《Java编程思想》第四版读书笔记 第十四章 类型信息
- java中有无参数和返回值的方法
- java的重修之路
- JAVA正则表达式匹配,替换,查找,切割
- MyEclipse无法自动编译
- Java并发编程—— 一个线程的内心独白
- 将eclipse的代码提交到Github
- 新手整合springmvc+velocity过程
- String的转换,替换和分隔
- SpringMVC异步处理之@Async(附源代码 - 单元测试通过)
- struts2找不到对应action错误的解决
- JAVA基础再回首(二)——流程控制语句、跳转控制语句
- dubbo 入门,与zookeeper maven spring整合
- 入门java script
- String类部分函数详解