BeanUtils
package com.in;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class BeanUtilsDeo {
private Date birthDay = new Date();
private int x;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
BeanUtilsDeo bu = new BeanUtilsDeo();
//设置单个属性
String x = BeanUtils.getProperty(bu, "x");
System.out.println(x);
BeanUtils.setProperty(bu, "x", "2");
System.out.println(bu.getX());
//设置级联属性
BeanUtils.setProperty(bu, "birthDay.time", "2012-10-21");
System.out.println(bu.getBirthDay().getTime());
//设置map的属性
Map map = new HashMap();
map.put("name", "s");
BeanUtils.setProperty(map, "name", "yy");
System.out.println(map.get("name"));
//设置单个属性
PropertyUtils.setProperty(bu, "x", 9);
System.out.println(bu.getX());
PropertyUtils.setProperty(bu, "birthDay", new Date());
System.out.println(bu.getBirthDay());
}
}