BeanUtils被广泛的使用与三大框架。
主要解决的问题是:把对象的属性数据封装到对象中。该工具提供字符式操作为对象属性提供封装。
BeanUtils底层使用反射机制实现数据访问及封装
使用前的准备工作
将commons-beanutils-1.9.3中的commons-beanutils-1.9.3.jar
将commons-logging-1.2中的commons-logging-1.2.jar
导入项目中
commons-beanutils依赖包下载
主要方法
static void copyProperty(Object bean, String name, Object value)
Copy the specified property value to the specified destination bean, performing any type conversion that is required.
方法参数 | 使用 |
---|---|
static void copyProperty (Object bean, String name, Object value) | 将指定的属性值复制到指定的目标bean,执行所需的任何类型转换。 |
static void setProperty (Object bean, String name, Object value) | 和上一个方法使用 |
static void populate (Object bean, Map < String,? extends Object> properties) | 将指定的map集合内的元素赋值到目标bean |
static void copyProperties (Object dest, Object orig) | 在属性名相同的所有情况下,将属性值从原始bean复制到目标bean。(底层进行深克隆) |
对深克隆浅克隆不理解详情见 –> 深克隆和浅克隆详细讲解
javabean对象
package com.xiaoqiang;
import java.util.Date;
public class Student {
private String name;
private String sex;
private int age;
private Date birthday;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String name, String sex, int age, Date birthday) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Student [name=" + name + ", sex=" + sex + ", age=" + age + ", birthday=" + birthday + "]";
}
}
BeanUtils组件使用
package com.xiaoqiang;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
public class TestBean {
public static void main(String[] args) throws Exception {
Student student = (Student) Class.forName("com.xiaoqiang.Student").newInstance();
BeanUtils.copyProperty(student, "name", "小强");
BeanUtils.copyProperty(student, "sex", "男");
BeanUtils.copyProperty(student, "age", "18");
BeanUtils.copyProperty(student, "age", 19);
//后写的相当于重新赋值
//BeanUtils.copyProperty(student, "aa", "a");
//录入属性不存在会产生 java.lang.NoClassDefFoundError
//BeanUtils.copyProperty(student, "age", "11a");
//若录入存在,数据类型无法转换成功则为转化为默认值
Student studentCopy = new Student();
BeanUtils.copyProperties(studentCopy, student);
//底层实现深克隆 若不理解
//详情见本人另一片文章深克隆浅克隆解析
studentCopy.setName("老王");
System.out.println(student);
System.out.println(studentCopy);
//通过map集合向对象插入数据
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "老罗");
map.put("age", 17);
map.put("sex", "男");
Student s = (Student) Class.forName("com.xiaoqiang.Student").newInstance();
BeanUtils.populate(s, map);
System.out.println(s);
}
}
BeanUtils注册日期转化器
package com.xiaoqiang;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
public class Registre {
public static void main(String[] args) throws Exception, InvocationTargetException {
Student student = (Student) Class.forName("com.xiaoqiang.Student").newInstance();
BeanUtils.copyProperty(student, "name", "小强");
BeanUtils.copyProperty(student, "sex", "男");
BeanUtils.copyProperty(student, "age", 18);
// //注册日期转换器
// ConvertUtils.register(new Converter() {
//
// @Override
// public <T> T convert(Class<T> type, Object value) {
// if(type != Date.class){
// return null;
// }
// if("".equals(value.toString().trim())){
// return null;
// }
// //value 为传入带转换值返回需要转换的指定数据类型
// SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd");
//
// try {
// return (T) format.parse(value.toString());
// } catch (ParseException e) {
// e.printStackTrace();
// }
// return null;
// }
// }, Date.class);
//可以采用Lambda表达式简化匿名内部类代码
//使用系统提供的日期转化器
ConvertUtils.register(new DateLocaleConverter(), Date.class);
BeanUtils.copyProperty(student, "birthday", "1997-06-13");
//自己写的类型只能使用自己创建的类型转化器
System.out.println(student);
}
}