BeanUtils:通过反射控制JavaBean属性的实用方法。
1.cloneBean(Object) Object
克隆一个bean是基于可用属性的get和set方法,即使这个bean类没有实现Cloneable接口方法的实现是有BeanUtilBean的newbean = bean.getClass().newInstance();
对newbean的属性设值采用PropertyUtilsBean.copyProperties内部采用java的内省调用get/set方法进行获取bean属性/设置newbean属性。
API:
Note: this method creates a shallow clone.采用浅复制的方式
实例:
1 public class Address 2 { 3 private String province; 4 private String city; 5 //getter/setter 6 } 7 public class Person 8 { 9 private Integer id; 10 private String name; 11 private Address address; 12 //getter/setter 13 } 14 public void test1() 15 { 16 Person bean = new Person(); 17 Address address = new Address(); 18 address.setProvince("hunan"); 19 address.setCity("huaihua"); 20 bean.setId(1); 21 bean.setName("name1"); 22 bean.setAddress(address); 23 try 24 { 25 //----test shadow clone bean---- 26 Person bean2 = (Person) BeanUtils.cloneBean(bean); 27 bean2.getAddress().setProvince("beijing"); 28 bean2.getAddress().setCity("beijing"); 29 System.out.println(bean.getAddress()+":::"+bean2.getAddress()); 30 31 }catch(Exception e) 32 { 33 e.printStackTrace(); 34 } 35 }
结果:
Address [province=beijing, city=beijing]:::Address [province=beijing, city=beijing]
2.copyProperties(Object dest, Object orig)
复制属性值从源bean到目标bean对于所有的属性,采用的是shallow copy方式,内部采用内省的方式获取get/set方法来获取源对象的属性和对目标对象的设值。
实例:
1 @Test 2 public void testcopyProperties() 3 { 4 Person bean = new Person(); 5 Address address = new Address(); 6 address.setProvince("hunan"); 7 address.setCity("huaihua"); 8 bean.setId(1); 9 bean.setName("name1"); 10 bean.setAddress(address); 11 Person dest = new Person(); 12 try 13 { 14 BeanUtils.copyProperties(dest, bean); 15 System.out.println("dest "+dest); 16 }catch(Exception e) 17 { 18 e.printStackTrace(); 19 } 20 }
结果:
dest Person [id=1, name=name1, address=Address [province=hunan, city=huaihua]]
实例:
1 public class Student 2 { 3 private Integer id; 4 private String name; 5 private Date date; 6 //gettter/settter 7 } 8 @Test 9 public void testcopyProperties() 10 { 11 Person bean = new Person(); 12 Address address = new Address(); 13 address.setProvince("hunan"); 14 address.setCity("huaihua"); 15 bean.setId(1); 16 bean.setName("name1"); 17 bean.setAddress(address); 18 Student dest = new Student(); 19 try 20 { 21 BeanUtils.copyProperties(dest, bean); 22 System.out.println(dest); 23 }catch(Exception e) 24 { 25 e.printStackTrace(); 26 } 27 }
结果:
Student [id=1, name=name1, date=null]
3.copyProperty(Object bean, String name, Object value)
复制一个具体的属性值到具体的bean中,并执行所需的任何类型转换。采用内省的方式对bean进行设值。
4.Map<String, String> describe(Object bean)
返回指定的实体提供一个读方法的全部属性集。注:任何一个类都是Object的子类,在子类中存在getClass(),会被继承读方法,同时也会被作为集合的属性。
实例:
1 @Test 2 public void testDescribe() 3 { 4 Person bean = new Person(); 5 bean.setId(1); 6 bean.setName("world"); 7 try 8 { 9 Map<String, String> map = BeanUtils.describe(bean); 10 System.out.println("map: "+map); 11 } catch (IllegalAccessException e) 12 { 13 e.printStackTrace(); 14 } catch (InvocationTargetException e) 15 { 16 e.printStackTrace(); 17 } catch (NoSuchMethodException e) 18 { 19 e.printStackTrace(); 20 } 21 }
结果:
map: {address=null, name=world, id=1, class="class" com.beanutil.px.Person}
5.String getProperty(Object bean, String name)
获取bean指定的属性值,内部采用内省的方式获取调用get方法
6. populate(Object bean, Map<String, ? extends Object> properties)
采用Map作为参数对bean进行设值。内部通过遍历Map调用setProperty(bean, name, value)设值
String[] getArrayProperty(Object bean, String name)
返回一个指定的数组属性,作为String[]返回
String getIndexedProperty(Object bean, String name)
返回bean中属性的索引值
String getMappedProperty(Object bean, String name)
String getMappedProperty(Object bean, String name, String key)