Java内省之第三方包BeanUtils
Java自带了一个内省的API,但是在实际项目中经常使用一个更为强大的第三方包:
Apache的BeanUtils组件。下面先给出官方的下载链接。
https://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi(2016-05-22:commons-beanutils-1.9.2-bin.zip)
同时,beanutils依赖于另外一个第三方包:commons-logging,下面是链接地址.
https://commons.apache.org/proper/commons-logging/download_logging.cgi (2016-05-22:commons-logging-1.2-bin.zip)
关于两个日志的比较与说明,参链接:http://blog.csdn.net/courage89/article/details/29649801
以下给出简单的使用Demo:测试前首先下载好BeanUtils的jar和logging的jar并引入到项目中。
JavaBean:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.rt.gwq.introspector; 2 3 public class Person { 4 5 private String name ; 6 private String password ; 7 private int age ; 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 public String getPassword() { 15 return password; 16 } 17 public void setPassword(String password) { 18 this.password = password; 19 } 20 public int getAge() { 21 return age; 22 } 23 public void setAge(int age) { 24 this.age = age; 25 } 26 27 }
BeanUtils Demo:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.rt.gwq.introspector; 2 3 import java.lang.reflect.InvocationTargetException; 4 import java.text.ParseException; 5 import java.text.SimpleDateFormat; 6 import java.time.LocalDate; 7 import java.util.Date; 8 9 import org.apache.commons.beanutils.BeanUtils; 10 import org.apache.commons.beanutils.ConversionException; 11 import org.apache.commons.beanutils.ConvertUtils; 12 import org.apache.commons.beanutils.Converter; 13 import org.junit.Test; 14 15 public class Demo_BeanUtils { 16 17 /** 18 * BeanUtils 使用Deom 存取基本数据类型的属性 19 * @throws Exception 20 */ 21 @Test 22 public void test1() throws Exception{ 23 24 Person p = new Person(); 25 BeanUtils.setProperty(p, "age",23); 26 27 String age = BeanUtils.getProperty(p, "age"); 28 29 System.out.println(age); 30 } 31 32 /** 33 * 转换器:BeanUtils String转换时间示例 34 * @throws IllegalAccessException 35 * @throws InvocationTargetException 36 * @throws NoSuchMethodException 37 * @throws ParseException 38 */ 39 @Test 40 public void test2() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, ParseException{ 41 42 Person p = new Person(); 43 String date = "1999-01-01"; 44 45 //注册转换器 46 ConvertUtils.register(new Converter() { 47 48 @Override 49 public Object convert(Class type, Object value) { 50 String str = (String) value; 51 //为null 52 if(value==null){ 53 return null; 54 } 55 //空字符串 56 if(str.toString().trim()==""){ 57 return null; 58 } 59 //飞字符串对象 60 if(!(value instanceof String)){ 61 throw new ConversionException("只支持String类型"); 62 } 63 //装换为时间格式 64 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); 65 try { 66 return sf.parse(str); 67 } catch (ParseException e) { 68 throw new RuntimeException(e); 69 } 70 71 } 72 }, Date.class); 73 74 //存取值 75 BeanUtils.setProperty(p, "birthday", date); 76 Date d = p.getBirthday(); 77 //验证 78 System.out.println(d.toLocaleString()); 79 80 } 81 82 }