commons beanutils-动态设置属性
beanutils其实就是对反射的封装.下面做个动态设置属性的实验.
beanutils其实就是对反射的封装.下面做个动态设置属性的实验.
package test.common.beanutils;
import org.apache.commons.beanutils.PropertyUtils;
import test.common.db.Var;
public class BeanTest {
public static void main(String[] args) throws Exception {
DataValue dv = new DataValue();
for (int i = 1; i <= 5; i++) {
PropertyUtils.setProperty(dv, "data" + String.format("%02d", i), i);
}
PropertyUtils.setProperty(dv, "var", new Var());
PropertyUtils.setProperty(dv, "var.id", "phl");
PropertyUtils.setProperty(dv, "var.date_Time", "2010-08-04");
// 打印看看
for (int i = 1; i <= 5; i++) {
System.out.println(PropertyUtils.getProperty(dv, "data" + String.format("%02d", i)));
}
System.out.println(PropertyUtils.getProperty(dv, "var.date_Time"));
}
}
package test.common.beanutils;
import test.common.db.Var;
public class DataValue {
public int data01;
public int data02;
public int data03;
public int data04;
public int data05;
public Var var;
public int getData01() {
return data01;
}
public void setData01(int data01) {
this.data01 = data01;
}
public int getData02() {
return data02;
}
public void setData02(int data02) {
this.data02 = data02;
}
public int getData03() {
return data03;
}
public void setData03(int data03) {
this.data03 = data03;
}
public int getData04() {
return data04;
}
public void setData04(int data04) {
this.data04 = data04;
}
public int getData05() {
return data05;
}
public void setData05(int data05) {
this.data05 = data05;
}
public Var getVar() {
return var;
}
public void setVar(Var var) {
this.var = var;
}
}
package test.common.db;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
public class Var {
private String id;
private float value_t;
private String Date_Time;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public float getValue_t() {
return value_t;
}
public void setValue_t(float valueT) {
value_t = valueT;
}
public String getDate_Time() {
return Date_Time;
}
public void setDate_Time(String dateTime) {
Date_Time = dateTime;
}
}