commons beanutils之复制Bean属性
本文来自:http://fhd001.iteye.com/blog/440930
问题
你有两个相同类型的bean的实例,并需要将其中一个bean的属性复制到另一个中去.
解决方案
使用PropertyUtils.copyProperties()方法从一个bean复制属性到另一个bean.其中第一个参数
指定目的bean,而第二个参数是源bean:
Book book = new Book();
book.setName("Prelude to Poundation");
book.setAuthorName("Asimov");
Book destinationBook = new Book();
PropertyUtils.copyProperties(destinationBook,book);
PropertyUtils.copyProperties()方法从源bean实例中检索出所有属性的值,然后将
这些值赋给目的的实例中的相应属性.如果上面的示例中Book bean含有一个Author类型
的author属性,copyProperties()将把相同的引用对象赋给目的bean.换句话说,该方法
没有克隆bean属性的值.如例:
Author author = new Author();
author.setName("Zinsser");
Book book = new Book();
book.setName("On Writing well");
book.setAuthor(author);
Book destinationBook = new Book();
PropertyUtils.copyProperties(destionationBook,book);
if(book.getAuthor() == destinationBook.getAuthor()){
System.out.println("Author objects identical");
}
现在,两个对象中的author属性值都是Author类同一实例的引用.copyProperyties()并没
有复制bean属性的值.
copyProperties()方法还可以将一个Map的内容复制进bean中,前提是Map各键一一对应于目的
bean的各简单属性:
Map mapOrops = HashMap();
mapProps.put("name","The Art of computer rogramming");
mapProps.put("author","Knuth");
Book destinationBook = new Book();
PropertyUtils.copyProperties(destinationBook,mapProps);
- package test4;
- import java.lang.reflect.InvocationTargetException;
- import java.util.HashMap;
- import java.util.Map;
- import org.apache.commons.beanutils.PropertyUtils;
- /**
- * commons beanutils之复制Bean属性
- * @author fhd001
- */
- public class PropertyUtilsTest7 {
- public static void main(String[] args) {
- /*
- * 例子1 .
- */
- Person p1 = new Person();
- p1.setAge(12 );
- p1.setName("fhd" );
- Person p2 = new Person();
- try {
- PropertyUtils.copyProperties(p2, p1);
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- }
- System.out.println("p2.age: " +p2.getAge()+ "\tp2.name:" +p2.getName());
- /*
- * 例子2 .证明没有克隆bean属性
- */
- Person p3 = new Person();
- p3.setAge(34 );
- p3.setName("ldh" );
- Book b1 = new Book();
- b1.setAuthor(p3);
- b1.setBookName("java" );
- Book b2 = new Book();
- try {
- PropertyUtils.copyProperties(b2, b1);
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- }
- if (b1.getAuthor() == b2.getAuthor()) {
- System.out.println("证明: b1.getAuthor()与b2.getAuthor()返回同一个对象." );
- }
- /*
- * 例子3 .将map中的内容复制到bean中.
- */
- Map<String,Object> m1 = new HashMap<String,Object>();
- m1.put("age" , 31 );
- m1.put("name" , "fhd" );
- Person p6 = new Person();
- try {
- PropertyUtils.copyProperties(p6, m1);
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- }
- System.out.println("p6.name: " +p6.getName()+ "\tp6.age: " +p6.getAge());
- }
- }
package test4; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; import org.apache.commons.beanutils.PropertyUtils; /** * commons beanutils之复制Bean属性 * @author fhd001 */ public class PropertyUtilsTest7 { public static void main(String[] args) { /* * 例子1. */ Person p1 = new Person(); p1.setAge(12); p1.setName("fhd"); Person p2 = new Person(); try { PropertyUtils.copyProperties(p2, p1); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } System.out.println("p2.age: "+p2.getAge()+"\tp2.name:"+p2.getName()); /* * 例子2.证明没有克隆bean属性 */ Person p3 = new Person(); p3.setAge(34); p3.setName("ldh"); Book b1 = new Book(); b1.setAuthor(p3); b1.setBookName("java"); Book b2 = new Book(); try { PropertyUtils.copyProperties(b2, b1); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } if (b1.getAuthor() == b2.getAuthor()) { System.out.println("证明: b1.getAuthor()与b2.getAuthor()返回同一个对象."); } /* * 例子3.将map中的内容复制到bean中. */ Map<String,Object> m1 = new HashMap<String,Object>(); m1.put("age", 31); m1.put("name", "fhd"); Person p6 = new Person(); try { PropertyUtils.copyProperties(p6, m1); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } System.out.println("p6.name: "+p6.getName()+"\tp6.age: "+p6.getAge()); } }
- p2.age: 12 p2.name:fhd
- 证明: b1.getAuthor()与b2.getAuthor()返回同一个对象.
- p6.name: fhd p6.age: 31
======================================
s.setSchoolId("11");
s.setSchoolName("11");
s.setIntroduce("ddd");
Map<String, String> formMap = new HashMap<String, String>();
formMap.put("schoolId", "22");
formMap.put("schoolName", "22");
PropertyUtils.copyProperties(s, formMap);
System.out.println(s.getIntroduce());
System.out.println(s.getSchoolName());
22
1. public class ObjectTransWrap {
2. public static Object wrap(Object actionObj,Class OperatorAction) throws Exception, InstantiationException{
3. Object o = OperatorAction.newInstance();
4. BeanInfo beanInfo = Introspector.getBeanInfo(actionObj.getClass());
5. PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
6. Method get = null;
7. Method set = null;
8. for (int i = 0; i < properties.length; i++) {
9. try{
10. get = properties[i].getReadMethod();
11. set = o.getClass().getMethod( properties[i].getWriteMethod().getName(), new Class[]{properties[i].getPropertyType()});
12. }catch(Throwable a){
13. continue;
14. }
15. if(set!=null&&get!=null){
16. set.invoke(o, new Object[]{get.invoke(actionObj, new Object[]{})});
17. set = null;
18. get = null;
19. }
20. }
21. return o;
22.
23. }