本人感觉BeanUtils这个工具包太实用了,而且高端、大气、上档次。。我很喜欢。。
废话少说。。我参考了张孝祥老师的视频讲解然后再根据自己的想法写了一个测试的小程序。
首先使用BeanUtils工具包要加入两个jar包:commons-beanutils.jar和commons-logging.jar
然后就需要JavaBean程序了,使用了两个不同的JavaBean:
Person
import java.util.Date;
public class Person {
private int x;
//private Date birthday; ---java.lang.IllegalArgumentException: No bean specified
private Date birthday = new Date();//OK
public int getAge() {
return x;
}
public void setAge(int x) {
this.x = x;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
Dog
public class Dog {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
BeanUtils测试类--- BeanUtilsTest
import java.util.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class BeanUtilsTest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Person p1 = new Person();
BeanUtils.setProperty(p1, "age", "10");//只与JavaBean 有关,与x无关
System.out.println("getProperty-p1.age--"+BeanUtils.getProperty(p1,"age"));
//BeanUtils它很强大,支持级联属性操作(p下面有birthday属性,birthday下面有time属性--属性链一级级的)
BeanUtils.setProperty(p1, "birthday.time", "1111");//Date类有个setTime方法和getTime方法;
System.out.println("getProperty-p1.birthday.time--"+BeanUtils.getProperty(p1, "birthday.time"));
//-------------------------------
Person p2 = new Person();
BeanUtils.copyProperties(p2,p1);//可以把p1对象上的属性值拷贝到p2对象上
System.out.println("getProperty-p2.age--"+BeanUtils.getProperty(p2,"age"));
//-------------------------------
Dog d1 = new Dog();
BeanUtils.copyProperties(d1,p1); //要确保两个不同对象拥有相同的JavaBean属性
System.out.println("getProperty-d1.age--"+BeanUtils.getProperty(d1,"age"));
//-------------------------------
//把一个JavaBean的属性转换为Map,这个转换用的很多--describe(Object bean)
Map prop1 = BeanUtils.describe(p1); //map{age=10; birthday=...} 键值对
Set en = prop1.entrySet();
System.out.println("BeanUtils.describe(p1)--"+en);
//-------------------------------
//BeanUtils也可以设置Map集合里的value值
//(BeanUtils也可以操作Map,因为Map和JavaBean很类似--Map的key就相当于JavaBean的属性,并且Map和JavaBean可以相互转换)
Map prop2 = new HashMap();
prop2.put("name", "zxx");
BeanUtils.setProperty(prop2, "name", "xxz");
System.out.println("Map prop2.get(name)--"+prop2.get("name")); //xxz
//-------------------------------
//也可以把Map转换为JavaBean---populate(Object bean, Map properties)
//--把Map里的value值填充到JavaBean的属性值
Dog d2 = new Dog();
BeanUtils.populate(d2, prop2);
System.out.println("d2.getAge()--"+d2.getAge());
System.out.println("BeanUtils.populate(Dog d2, Map prop2)---d2.getAge()--"+d2.getName());
//-------------------------------
//PropertyUtils的使用,观察与BeanUtils的区别
// PropertyUtils.setProperty(d2, "age", "100");//Exception: argument type mismatch
PropertyUtils.setProperty(d2, "age", 100);//OK
System.out.println("PropertyUtils.setProperty--"+d2.getAge());
//返回的类型是java.lang.Integer
String type = PropertyUtils.getProperty(d2, "age").getClass().getName();
System.out.println("PropertyUtils.getProperty--age--type--"+type);
/*BeanUtils是以字符串的形式对JavaBean属性进行操作,BeanUtils内部会自动进行类型转换
* PropertyUtils是以JavaBean属性本身的类型进行操作。
* 如果BeanUtils类型转换的不准确的话或者转换过程中出现问题,你可以使用PropertyUtils
* */
}
}
程序输出结果:
getProperty-p1.age--10
getProperty-p1.birthday.time--1111
getProperty-p2.age--10
getProperty-d1.age--10
BeanUtils.describe(p1)--[birthday=Thu Jan 01 08:00:01 CST 1970, age=10, class=class com.beanutils.Person]
Map prop2.get(name)--xxz
d2.getAge()--0
BeanUtils.populate(Dog d2, Map prop2)---d2.getAge()--xxz
PropertyUtils.setProperty--100
PropertyUtils.getProperty--age--type--java.lang.Integer