一、简介:

        BeanUtils提供对Java反射和自省API的包装。其主要目的是利用反射机制对JavaBean的属性进行处理。我们知道,一个JavaBean通常包含了大量的属性,很多情况下,对JavaBean的处理导致大量get/set代码堆积,增加了代码长度和阅读代码的难度。

 

 

二、用法:

        

        如果你有两个具有很多相同属性的JavaBean,一个很常见的情况就是Struts里的PO对象(持久对象)和对应的ActionForm。例如:一个用户注册页面,有一个User实体类和一个UserActionForm,我们一般会在Action里从ActionForm构造一个PO对象,传统的方式是使用类似下面的语句对属性逐个赋值:

 

 


Java代码 


1. // 获取 ActionForm 表单数据   
2.   
3. UserActionForm uForm = (UserActionForm) form;   
4.   
5.   
6.   
7. // 构造一个User对象   
8.   
9. User user = new
10.   
11.   
12.   
13. // 逐一赋值 
14. user.setUsername(uForm.getUsername);   
15.   
16. user.setPassword(uForm.getPassword);   
17.   
18. user.setAge(uForm.getAge);   
19.   
20. ...........   
21.   
22.   
23.   
24. ...........   
25.   
26.   
27.   
28. // 然后调用JDBC、或操作Hibernate 持久化对象User到数据库 
29.   
30. ...


[java] 


1. <span style="font-size: large;">// 获取 ActionForm 表单数据  
2.   
3. UserActionForm uForm = (UserActionForm) form;  
4.   
5.   
6.   
7. // 构造一个User对象  
8.   
9. User user = new User();  
10.   
11.   
12.   
13. // 逐一赋值  
14. user.setUsername(uForm.getUsername);  
15.   
16. user.setPassword(uForm.getPassword);  
17.   
18. user.setAge(uForm.getAge);  
19.   
20. ...........  
21.   
22.   
23.   
24. ...........  
25.   
26.   
27.   
28. // 然后调用JDBC、或操作Hibernate 持久化对象User到数据库  
29.   
30. ...  
31. </span>

  

通过这样的方法如果表单数据N多、100、1000(夸张点。哈哈)、、、、那我们不是要写100、、、1000行set、get了。谁都不愿意这样做。

 

而我们使用 BeanUtils.copyProperties() 方法以后,代码量大大的减少,而且整体程序看着也简洁明朗,代码如下:

 


Java代码 



1. // 获取 ActionForm 表单数据 
2.   
3. UserActionForm uForm = (UserActionForm) form;   
4.   
5.     
6.   
7. // 构造一个User对象 
8.   
9. User user = new
10.   
11.   
12. // 赋值 
13.   
14.     
15.   
16. BeanUtils.copyProperties(user, uForm);   
17.   
18.   
19.   
20.   
21.   
22. // 然后调用JDBC、或操作Hibernate 持久化对象User到数据库 
23.   
24. .......


[java] 


1. <span style="font-size: large;">// 获取 ActionForm 表单数据  
2.   
3. UserActionForm uForm = (UserActionForm) form;  
4.   
5.    
6.   
7. // 构造一个User对象  
8.   
9. User user = new User();  
10.   
11.   
12. // 赋值  
13.   
14.    
15.   
16. BeanUtils.copyProperties(user, uForm);  
17.   
18.   
19.   
20.   
21.   
22. // 然后调用JDBC、或操作Hibernate 持久化对象User到数据库  
23.   
24. .......  
25. </span>

很方便是吧。

  注:如果User和UserActionForm 间存在名称不相同的属性,则BeanUtils不对这些属性进行处理,需要手动处理。例如:

User类里面有个createDate 创建时间字段,而UserActionForm里面无此字段。BeanUtils.copyProperties()不会对此字段做任何处理。必须要自己手动处理。

        ---------------------------<>-----------------------------------


public static void 
  transform 
 (Object dist, Object orig) 
 
    throws RuntimeException 
 
  { 
 
    try 
 
    { 
 
      BeanUtils.copyProperties(dist, orig); 
 
    } catch (IllegalAccessException e) { 
 
      throw new RuntimeException("访问出现异常!"); 
 
    } catch (InvocationTargetException e) { 
 
      if ((e.getCause() instanceof NullPointerException)) { 
 
        throw new RuntimeException("源对象[" + orig.getClass().getName() + "]属性[" + e.getCause().getMessage() + "]存在空值,而目标对象[" + dist.getClass().getName() + "]不允许此值为空!"); 
 
      } 
 
      throw new RuntimeException("源对象[" + orig.getClass().getName() + "]转换目标对象[" + dist.getClass().getName() + "]失败!"); 
 
    } 
 
  }