【BeanUtils】操作JavaBean

本文介绍如何使用Apache Commons BeanUtils库操作JavaBean属性,包括基本类型的设置与获取及日期类型的转换。通过示例展示了BeanUtils简化JavaBean操作的方法。

BeanUtils提供对 Java反射和自省API的包装。其主要目的是利用反射机制对JavaBean的属性进行处理。

Person.class

[java]   view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.xiaozhi.beanutils;  
  2.   
  3. import java.util.Date;  
  4.   
  5. public class Person {  
  6.     private String name;  
  7.     private String password;  
  8.     private int age;  
  9.     private Date birthday;  
  10.     public Date getBirthday() {  
  11.         return birthday;  
  12.     }  
  13.     public void setBirthday(Date birthday) {  
  14.         this.birthday = birthday;  
  15.     }  
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.     public String getPassword() {  
  20.         return password;  
  21.     }  
  22.     public int getAge() {  
  23.         return age;  
  24.     }  
  25.     public void setName(String name) {  
  26.         this.name = name;  
  27.     }  
  28.     public void setPassword(String password) {  
  29.         this.password = password;  
  30.     }  
  31.     public void setAge(int age) {  
  32.         this.age = age;  
  33.     }  
  34.   
  35. }  

Test.java

[java]   view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.xiaozhi.beanutils;  
  2.   
  3. import java.lang.reflect.InvocationTargetException;  
  4. import java.util.Date;  
  5.   
  6. import org.apache.commons.beanutils.BeanUtils;  
  7. import org.apache.commons.beanutils.ConvertUtils;  
  8. import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;  
  9.   
  10. public class Test {  
  11.   
  12.     public static void main(String[] args) throws Exception, InvocationTargetException {  
  13.         //使用beanUtils操纵bean的属性  
  14. //      Person person=new Person();  
  15. //      BeanUtils.setProperty(person, "name", "xiaozhi");  
  16. //      System.out.println(person.getName());  
  17. //      System.out.println(BeanUtils.getProperty(person, "name"));  
  18.           
  19.           
  20.          //beanUtilsz支持八种类型自动转换  
  21. //      String name="xiaozhi";  
  22. //      String age="22";  
  23. //      String password="123456";  
  24. //        
  25. //      Person person=new Person();  
  26. //      BeanUtils.setProperty(person, "name", name);  
  27. //      BeanUtils.setProperty(person, "age", age);  
  28. //      BeanUtils.setProperty(person, "password", password);  
  29. //        
  30. //      System.out.println(person.getName());  
  31. //      System.out.println(person.getAge());  
  32. //      System.out.println(person.getPassword());  
  33.           
  34.         //对于时间类型,要注册转换器  
  35.         String birthday ="1992-09-28";  
  36.         ConvertUtils.register(new DateLocaleConverter(), Date.class);  
  37.         Person person=new Person();  
  38.         BeanUtils.setProperty(person, "birthday", birthday);  
  39.         System.out.println(person.getBirthday());  
  40.     }  
  41. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值