一、概述
第一次看到BeanUtils包,是在Struts项目中,作为Struts一个工具来使用的,
估计功能越弄越强,就移到Common项目中了吧。
BeanUtils一共有四个package:
org.apache.commons.beanutils
org.apache.commons.beanutils.converters
org.apache.commons.beanutils.locale
org.apache.commons.beanutils.locale.converters
后三个包主要是用于数据的转换,围绕着一个Converter接口,该接口只有一个方法:
java.lang.Object convert(java.lang.Class type, java.lang.Object value) ,
用于将一个value转换成另一个类型为type的Object。在一些自动化的应用中应该会有用。
这里不作评论,以后有兴趣了,或者觉得有用了,再行研究。
这里只讲第一个包。
-----------------
注:上面这段话为转载。
-----------------
二、BeanUtils基于实例的研究:
(1)、构造两个Bean
package objectdemo;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Street {
public Street() {
}
private String sid;
private String sname;
public String getSid() {
return sid;
}
public String getSname() {
return sname;
}
public void setSid(String sid) {
this.sid = sid;
}
public void setSname(String sname) {
this.sname = sname;
}
}
--------------------------------------------------------------------------------------------------
package objectdemo;
import java.util.Map;
public class ObjectDTO {
public ObjectDTO() {
}
private Integer id;
private String name;
private String password;
private String[] Fail;
private Map desc;
private Street street;
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
public Map getDesc() {
return desc;
}
public void setDesc(Map desc) {
this.desc = desc;
}
public String[] getFail() {
return Fail;
}
public void setFail(String[] Fail) {
this.Fail = Fail;
}
public Street getStreet() {
return street;
}
public void setStreet(Street street) {
this.street = street;
}
}
----------------------------------
(2)、测试:
package objectdemo;
import java.util.Map;
import java.util.HashMap;
import org.apache.commons.beanutils.BeanUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class TestObject {
public TestObject() {
}
public static void main(String[] args) throws InstantiationException,
NoSuchMethodException, IllegalAccessException, InvocationTargetException {
TestObject testObject1 = new TestObject();
ObjectDTO dto=new ObjectDTO();
Map map=new HashMap();
map.put("id",new Integer(2));
map.put("name","value1");
map.put("fail",new String[]{"a","b","c"});
map.put("password","value2");
Map hh=new HashMap();
hh.put("00","0aa");
hh.put("01","0ab");
map.put("desc",hh);
Street st=new Street();
st.setSid("sx0001");
st.setSname("xiaroom");
map.put("street",st);
testObject1.doCopy(dto,map);
System.out.println(dto.getId());
System.out.println(dto.getName());
System.out.println(dto.getPassword());
String[] f=dto.getFail();
for (int i=0;i<f.length;i++) {
System.out.println(f[i]);
}
}
public void doCopy(Object object,Map map) throws InvocationTargetException,
IllegalAccessException, NoSuchMethodException, InvocationTargetException,
IllegalAccessException, NoSuchMethodException, InvocationTargetException,
InstantiationException, IllegalAccessException {
// BeanUtils.populate(object,map); //(1)populate
// BeanUtils.copyProperties(object,map); //(2)copyProperties
BeanUtils.copyProperties(object,map); //(3)copyProperties
String id=BeanUtils.getProperty(object,"id"); //(4)getProperty
System.out.println("id="+id);
String name=BeanUtils.getProperty(object,"name");
System.out.println("name="+name);
String password=BeanUtils.getProperty(object,"password");
System.out.println("password="+password);
String[] fail=BeanUtils.getArrayProperty(object,"fail");//(5)getArrayProperty
System.out.println("fail[0]="+fail[0]);
System.out.println("fail's length="+fail.length);
Map hashMap=BeanUtils.describe(object);//(6)describe
System.out.println("hashMap's size="+hashMap.size());
String a=BeanUtils.getIndexedProperty(object,"fail",0);//(7)getIndexedProperty
System.out.println("fail[0]="+a);
String aa=BeanUtils.getIndexedProperty(object,"fail[1]");
System.out.println("fail[1]="+aa);
String ps=BeanUtils.getSimpleProperty(object,"password");//(8)getSimpleProperty
System.out.println("password="+ps);
BeanUtils.setProperty(object,"password","value22");//(9)setProperty
String pss=BeanUtils.getSimpleProperty(object,"password");
System.out.println("password="+pss);
ObjectDTO dto=(ObjectDTO) BeanUtils.cloneBean(object);//(10)cloneBean
System.out.println(dto.getClass().getName());
System.out.println("dto'id="+dto.getId());
System.out.println("dto'name="+dto.getName());
System.out.println("dto'password="+dto.getPassword());
String value00=BeanUtils.getMappedProperty(object,"desc","00");//(11)getMappedProperty
System.out.println("value00="+value00);
String sid=BeanUtils.getNestedProperty(object,"street.sid");//(12)getNestedProperty
System.out.println("street.sid="+sid);
String sname=BeanUtils.getNestedProperty(object,"street.sname");
System.out.println("street.sname="+sname);
}
}
(3)、输出如下:
。。。。。。
id=2
name=value1
password=value2
fail[0]=a
fail's length=3
hashMap's size=7
fail[0]=a
fail[1]=b
password=value2
password=value22
objectdemo.ObjectDTO
dto'id=2
dto'name=value1
dto'password=value22
value00=0aa
street.sid=sx0001
street.sname=xiaroom
2
value1
value22
a
b
c
第一次看到BeanUtils包,是在Struts项目中,作为Struts一个工具来使用的,
估计功能越弄越强,就移到Common项目中了吧。
BeanUtils一共有四个package:
org.apache.commons.beanutils
org.apache.commons.beanutils.converters
org.apache.commons.beanutils.locale
org.apache.commons.beanutils.locale.converters
后三个包主要是用于数据的转换,围绕着一个Converter接口,该接口只有一个方法:
java.lang.Object convert(java.lang.Class type, java.lang.Object value) ,
用于将一个value转换成另一个类型为type的Object。在一些自动化的应用中应该会有用。
这里不作评论,以后有兴趣了,或者觉得有用了,再行研究。
这里只讲第一个包。
-----------------
注:上面这段话为转载。
-----------------
二、BeanUtils基于实例的研究:
(1)、构造两个Bean
package objectdemo;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Street {
public Street() {
}
private String sid;
private String sname;
public String getSid() {
return sid;
}
public String getSname() {
return sname;
}
public void setSid(String sid) {
this.sid = sid;
}
public void setSname(String sname) {
this.sname = sname;
}
}
--------------------------------------------------------------------------------------------------
package objectdemo;
import java.util.Map;
public class ObjectDTO {
public ObjectDTO() {
}
private Integer id;
private String name;
private String password;
private String[] Fail;
private Map desc;
private Street street;
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
public Map getDesc() {
return desc;
}
public void setDesc(Map desc) {
this.desc = desc;
}
public String[] getFail() {
return Fail;
}
public void setFail(String[] Fail) {
this.Fail = Fail;
}
public Street getStreet() {
return street;
}
public void setStreet(Street street) {
this.street = street;
}
}
----------------------------------
(2)、测试:
package objectdemo;
import java.util.Map;
import java.util.HashMap;
import org.apache.commons.beanutils.BeanUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class TestObject {
public TestObject() {
}
public static void main(String[] args) throws InstantiationException,
NoSuchMethodException, IllegalAccessException, InvocationTargetException {
TestObject testObject1 = new TestObject();
ObjectDTO dto=new ObjectDTO();
Map map=new HashMap();
map.put("id",new Integer(2));
map.put("name","value1");
map.put("fail",new String[]{"a","b","c"});
map.put("password","value2");
Map hh=new HashMap();
hh.put("00","0aa");
hh.put("01","0ab");
map.put("desc",hh);
Street st=new Street();
st.setSid("sx0001");
st.setSname("xiaroom");
map.put("street",st);
testObject1.doCopy(dto,map);
System.out.println(dto.getId());
System.out.println(dto.getName());
System.out.println(dto.getPassword());
String[] f=dto.getFail();
for (int i=0;i<f.length;i++) {
System.out.println(f[i]);
}
}
public void doCopy(Object object,Map map) throws InvocationTargetException,
IllegalAccessException, NoSuchMethodException, InvocationTargetException,
IllegalAccessException, NoSuchMethodException, InvocationTargetException,
InstantiationException, IllegalAccessException {
// BeanUtils.populate(object,map); //(1)populate
// BeanUtils.copyProperties(object,map); //(2)copyProperties
BeanUtils.copyProperties(object,map); //(3)copyProperties
String id=BeanUtils.getProperty(object,"id"); //(4)getProperty
System.out.println("id="+id);
String name=BeanUtils.getProperty(object,"name");
System.out.println("name="+name);
String password=BeanUtils.getProperty(object,"password");
System.out.println("password="+password);
String[] fail=BeanUtils.getArrayProperty(object,"fail");//(5)getArrayProperty
System.out.println("fail[0]="+fail[0]);
System.out.println("fail's length="+fail.length);
Map hashMap=BeanUtils.describe(object);//(6)describe
System.out.println("hashMap's size="+hashMap.size());
String a=BeanUtils.getIndexedProperty(object,"fail",0);//(7)getIndexedProperty
System.out.println("fail[0]="+a);
String aa=BeanUtils.getIndexedProperty(object,"fail[1]");
System.out.println("fail[1]="+aa);
String ps=BeanUtils.getSimpleProperty(object,"password");//(8)getSimpleProperty
System.out.println("password="+ps);
BeanUtils.setProperty(object,"password","value22");//(9)setProperty
String pss=BeanUtils.getSimpleProperty(object,"password");
System.out.println("password="+pss);
ObjectDTO dto=(ObjectDTO) BeanUtils.cloneBean(object);//(10)cloneBean
System.out.println(dto.getClass().getName());
System.out.println("dto'id="+dto.getId());
System.out.println("dto'name="+dto.getName());
System.out.println("dto'password="+dto.getPassword());
String value00=BeanUtils.getMappedProperty(object,"desc","00");//(11)getMappedProperty
System.out.println("value00="+value00);
String sid=BeanUtils.getNestedProperty(object,"street.sid");//(12)getNestedProperty
System.out.println("street.sid="+sid);
String sname=BeanUtils.getNestedProperty(object,"street.sname");
System.out.println("street.sname="+sname);
}
}
(3)、输出如下:
。。。。。。
id=2
name=value1
password=value2
fail[0]=a
fail's length=3
hashMap's size=7
fail[0]=a
fail[1]=b
password=value2
password=value22
objectdemo.ObjectDTO
dto'id=2
dto'name=value1
dto'password=value22
value00=0aa
street.sid=sx0001
street.sname=xiaroom
2
value1
value22
a
b
c