BeanUtils用法举例

BeanUtils是一种很实用的封装好的javaBean, 是apache机构下的一个组件, 这里初略地讲述一下BeanUtils的实用性,以代码来讲述.

 

User.java

  1. package com.deng;  
  2.   
  3. /** 
  4.  * @author: 邓永胜 
  5.  * @date:  Jan 19, 2010 - 10:27:57 PM 
  6.  */  
  7. public class User {  
  8.   
  9.     private String username;   
  10.     private String password;  
  11.     private Long userId;  
  12.     private Profile profile;  
  13.     public String getUsername() {  
  14.         return username;  
  15.     }  
  16.     public void setUsername(String username) {  
  17.         this.username = username;  
  18.     }  
  19.     public String getPassword() {  
  20.         return password;  
  21.     }  
  22.     public void setPassword(String password) {  
  23.         this.password = password;  
  24.     }  
  25.       
  26.     public Long getUserId() {  
  27.         return userId;  
  28.     }  
  29.     public void setUserId(Long userId) {  
  30.         this.userId = userId;  
  31.     }  
  32.     public Profile getProfile() {  
  33.         return profile;  
  34.     }  
  35.     public void setProfile(Profile profile) {  
  36.         this.profile = profile;  
  37.     }  
  38.       
  39.       
  40.       
  41. }  

 

Profile.java

  1. package com.deng;  
  2.   
  3. import java.util.Date;  
  4. import java.util.Map;  
  5.   
  6. /** 
  7.  * @author: 邓永胜 
  8.  * @date:  Jan 19, 2010 - 10:28:22 PM 
  9.  */  
  10. public class Profile {  
  11.   
  12.     private String email ;  
  13.       
  14.     private Date birthDate;  
  15.       
  16.     private Address[] address;  
  17.       
  18.     private Map<String, String> phone;  
  19.   
  20.     public String getEmail() {  
  21.         return email;  
  22.     }  
  23.   
  24.     public void setEmail(String email) {  
  25.         this.email = email;  
  26.     }  
  27.   
  28.     public Date getBirthDate() {  
  29.         return birthDate;  
  30.     }  
  31.   
  32.     public void setBirthDate(Date birthDate) {  
  33.         this.birthDate = birthDate;  
  34.     }  
  35.   
  36.     public Address[] getAddress() {  
  37.         return address;  
  38.     }  
  39.   
  40.     public void setAddress(Address[] address) {  
  41.         this.address = address;  
  42.     }  
  43.   
  44.     public Map<String, String> getPhone() {  
  45.         return phone;  
  46.     }  
  47.   
  48.     public void setPhone(Map<String, String> phone) {  
  49.         this.phone = phone;  
  50.     }  
  51. }  

 

Address.java

  1. package com.deng;  
  2.   
  3. /** 
  4.  * @author: 邓永胜 
  5.  * @date:  Jan 19, 2010 - 10:32:11 PM 
  6.  */  
  7. public class Address {  
  8.   
  9.     private String country;  
  10.       
  11.     private String city;  
  12.       
  13.     private String addr;   
  14.       
  15.     private String postcode;  
  16.   
  17.     public Address(String country, String city, String addr, String postcode) {  
  18.         this.country = country;  
  19.         this.city = city;  
  20.         this.addr = addr;  
  21.         this.postcode = postcode;  
  22.     }  
  23.   
  24.     public Address() {  
  25.     }  
  26.   
  27.     public String getCountry() {  
  28.         return country;  
  29.     }  
  30.   
  31.     public void setCountry(String country) {  
  32.         this.country = country;  
  33.     }  
  34.   
  35.     public String getCity() {  
  36.         return city;  
  37.     }  
  38.   
  39.     public void setCity(String city) {  
  40.         this.city = city;  
  41.     }  
  42.   
  43.     public String getAddr() {  
  44.         return addr;  
  45.     }  
  46.   
  47.     public void setAddr(String addr) {  
  48.         this.addr = addr;  
  49.     }  
  50.   
  51.     public String getPostcode() {  
  52.         return postcode;  
  53.     }  
  54.   
  55.     public void setPostcode(String postcode) {  
  56.         this.postcode = postcode;  
  57.     }  
  58.       
  59.       
  60.       
  61. }  

 

BeanUtilExample.java

 

  1. package com.deng;  
  2.   
  3. import java.lang.reflect.InvocationTargetException;  
  4. import java.util.GregorianCalendar;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7. import org.apache.commons.beanutils.*;  
  8.   
  9. /** 
  10.  * @author: 邓永胜 
  11.  * @date: Jan 19, 2010 - 10:51:40 PM 
  12.  */  
  13. public class BeanUtilExample {  
  14.   
  15.     /** 
  16.      * @param args 
  17.      */  
  18.     public User prepareDate() {  
  19.         Profile profile = new Profile();  
  20.   
  21.         profile.setEmail("dys0411q1@126.com");  
  22.         profile.setBirthDate(new GregorianCalendar(20461111).getTime());  
  23.   
  24.         Map<String, String> phone = new HashMap<String, String>();  
  25.         phone.put("home""6861543");  
  26.         phone.put("office""123456");  
  27.         phone.put("mobile""15920054876");  
  28.         profile.setPhone(phone);  
  29.   
  30.         Address address1 = new Address("中国""北京""天安门一路""1001");  
  31.         Address address2 = new Address("中国""深圳""深南大道""1002");  
  32.         Address[] address = { address1, address2 };  
  33.         profile.setAddress(address);  
  34.   
  35.         User user = new User();  
  36.         user.setUserId(new Long(123456789));  
  37.         user.setUsername("deng");  
  38.         user.setPassword("123456");  
  39.         user.setProfile(profile);  
  40.   
  41.         return user;  
  42.     }  
  43.   
  44.     public static void main(String[] args) {  
  45.         BeanUtilExample example = new BeanUtilExample();  
  46.         User user = example.prepareDate();  
  47.         try {  
  48.             System.out.println(BeanUtils.getProperty(user, "userId")); // 输出user类的userId的值  
  49.             System.out.println(PropertyUtils.getProperty(user, "userId"));  
  50.             System.out.println(BeanUtils.getProperty(user, "username"));  
  51.             System.out.println(BeanUtils.getProperty(user, "password"));  
  52.             System.out.println(BeanUtils.getProperty(user, "profile"));  
  53.             System.out.println(BeanUtils.getProperty(user, "profile.email"));  
  54.             System.out.println(BeanUtils.getProperty(user, "profile.birthDate"));  
  55.             //phone的输出格式:{mobile=15920054876, office=123456, home=6861543}  
  56.             System.out.println(BeanUtils.getProperty(user, "profile.phone"));   
  57.             System.out.println(BeanUtils.getProperty(user, "profile.phone(mobile)"));   
  58.             System.out.println(BeanUtils.getProperty(user, "profile.phone(office)"));   
  59.             System.out.println(BeanUtils.getProperty(user, "profile.phone(home)"));   
  60.             System.out.println(BeanUtils.getProperty(user, "profile.address[0].country"));  
  61.             System.out.println(BeanUtils.getProperty(user, "profile.address[0].city"));  
  62.             System.out.println(BeanUtils.getProperty(user, "profile.address[0].postcode"));  
  63.               
  64.             User user2 = new User();  
  65.             BeanUtils.copyProperties(user2, user);  
  66.             System.out.println(BeanUtils.getProperty(user2, "userId")); // 输出user类的userId的值  
  67.             System.out.println(PropertyUtils.getProperty(user2, "userId"));  
  68.             System.out.println(BeanUtils.getProperty(user2, "username"));  
  69.             System.out.println(BeanUtils.getProperty(user2, "password"));  
  70.             System.out.println(BeanUtils.getProperty(user2, "profile"));  
  71.             System.out.println(BeanUtils.getProperty(user2, "profile.email"));  
  72.             System.out.println(BeanUtils.getProperty(user2, "profile.birthDate"));  
  73.             //phone的输出格式:{mobile=15920054876, office=123456, home=6861543}  
  74.             System.out.println(BeanUtils.getProperty(user2, "profile.phone"));   
  75.             System.out.println(BeanUtils.getProperty(user2, "profile.phone(mobile)"));   
  76.             System.out.println(BeanUtils.getProperty(user2, "profile.phone(office)"));   
  77.             System.out.println(BeanUtils.getProperty(user2, "profile.phone(home)"));   
  78.             System.out.println(BeanUtils.getProperty(user2, "profile.address[0].country"));  
  79.             System.out.println(BeanUtils.getProperty(user2, "profile.address[0].city"));  
  80.             System.out.println(BeanUtils.getProperty(user2, "profile.address[0].postcode"));  
  81.               
  82.         } catch (IllegalAccessException e) {  
  83.             e.printStackTrace();  
  84.         } catch (InvocationTargetException e) {  
  85.             e.printStackTrace();  
  86.         } catch (NoSuchMethodException e) {  
  87.             e.printStackTrace();  
  88.         }  
  89.     }  
  90.   
  91. }  

 

 BeanUtils的一个很重要的用法是可以逐连读取各个类的属性值以及内嵌的属性值,这里我所理解的内嵌的属性值是指一个类中的属性是另一个类, 比如类User,它包括了Profile这个属性类, Profile这个类的其中一个属性值为Address, 这个Address又是一个类, 如果想要取得Address类里面的某一个属性值时, 在传统的方法中是不能达到的. 这里的所有类及其属性都设置了值

User user = new User();

user.getProfile().getAddress(),   这里只能取得Address这个类,并不能导航到下面的属性值, 而BeanUtils可以做到

 

下面列举一下LazyDynaBean 的用法 

LazyDynaBeanExample.java

 

  1. package com.deng;  
  2.   
  3. import java.lang.reflect.InvocationTargetException;  
  4. import org.apache.commons.beanutils.BeanUtils;  
  5. import org.apache.commons.beanutils.LazyDynaBean;  
  6.   
  7. /** 
  8.  * @author: 邓永胜 
  9.  * @date:  Jan 19, 2010 - 11:45:17 PM 
  10.  */  
  11. public class LazyDynaBeanExample {  
  12.   
  13.     public static void main(String[] args) {  
  14.   
  15.         LazyDynaBean person = new LazyDynaBean();  
  16.         person.set("name""deng");  
  17.         person.set("password""123");  
  18.           
  19.         System.out.println(person.get("name"));  
  20.         System.out.println(person.get("password"));  
  21.           
  22.         try {  
  23.             System.out.println(BeanUtils.getProperty(person, "name"));  
  24.             System.out.println(BeanUtils.getProperty(person, "password"));  
  25.         } catch (IllegalAccessException e) {  
  26.             e.printStackTrace();  
  27.         } catch (InvocationTargetException e) {  
  28.             e.printStackTrace();  
  29.         } catch (NoSuchMethodException e) {  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33.   
  34. }  

 可以动态地生成类,并设置值和取得值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值