操作JavaBean通过反射或者内省过于麻烦,开源组织apache开发了BeanUtils框架用来操作JavaBean。
使用BeanUtils需要导入第三方jar包,commons-beanutils-1.9.2.jar和其依赖的commons-logging-1.2.jar
下面是一个例子,为Person的各个属性赋值。
Person.java
package cn.cyy.beanutils;
import java.util.Date;
public class Person {
private String name;
private int age;
private String password;
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAb(){
return null;
}
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;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
下面是对Person对象赋值的操作
package cn.cyy.beanutils;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;
//使用beanUtils操作bean的属性(第三方jar包)
public class Demo1 {
//通过BeanUtils的setProperty方法为name属性赋值
@Test
public void test1() throws Exception{
Person p = new Person();
BeanUtils.setProperty(p, "name", "cyy");
System.out.println(p.getName());
}
//BeanUtils默认只支持8种基本数据类型的转换,如果需要转换日期,则需要注册转换器
//注册自定义日期转换器
@Test
public void test2() throws Exception{
String name = "yuli";
String password = "177";
int age = 18;
// String birthday = "1980-09-09";
String birthday = "";
Person p = new Person();
//注册自定义日期转换器
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class type, Object value) {
// TODO Auto-generated method stub
if(value == null){
return null;
}
if(!(value instanceof String)){
throw new ConversionException("只支持String类型的转换");
}
String str = (String) value;
if(str.trim().equals("")){
return null;
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
return df.parse(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}
}, Date.class);
BeanUtils.setProperty(p, "name", name);//只支持8种基本数据类型
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
System.out.println(p.getBirthday());
}
//注册Converter类,已经实现的日期转换器
@Test
public void test3() throws Exception{
String name = "yuli";
String password = "177";
int age = 18;
// String birthday = "1990-06-30";
String birthday = "";
Person p = new Person();
//注册Convertor已经实现的日期转换器
ConvertUtils.register(new DateLocaleConverter(), Date.class);
BeanUtils.setProperty(p, "name", name);//只支持8种基本数据类型
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
System.out.println(p.getBirthday());
}
//通过BeanUtils的populate方法,将Map集合里的值,填充Bean的属性
@Test
public void test4() throws IllegalAccessException, InvocationTargetException{
Map map = new HashMap();
map.put("name", "yuli");
map.put("password", "123");
map.put("age", "18");
map.put("birthday", "1989-08-08");
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person p = new Person();
BeanUtils.populate(p, map);//用map集合中的值,填充Bean的属性
System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getPassword());
System.out.println(p.getBirthday().toLocaleString());
}
}