1.BeanUtils介绍
它是由Apache组织开发的一套操作JavaBean的API。通过BeanUtils库能够完成内省的一切功能,并且优化。通过其才操作JavaBean,可以大大简化代码的编写
BeanUtils:http://commons.apache.org/proper/commons-beanutils/index.html
Common Logging:http://commons.apache.org/proper/commons-logging/download_logging.cgi
2.下载及导入
下载BeanUtils库
commons-beanutils-1.8.3-bin
commons-beanutils-1.8.3-src
在项目中新建lib文件夹,将commons-beanutils-1.8.3复制过来,并build path到项目中
下载Common Logging
commons-logging-1.1.3-bin
复制到lib文件夹,并build path到项目总
3.BeanUtils工具包中的常用类:
BeanUtils
ConvertUtils.register(Convert convert,Class clazz)
自定义转换器
内置转换器
4.Demo
(1)假设有一个学生类
package web.java.beanutils; import java.util.Date; public class Student { private String name; private String sno; private int age; private Date birthday; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
(2)利用BeanUtils来操作JavaBean
package web.java.beanutils; import java.lang.reflect.InvocationTargetException; import java.text.ParseException; import java.text.SimpleDateFormat; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.Converter; import org.junit.Test; public class Demo1 { @Test public void test1(){ Student s = new Student(); BeanUtils bu = new BeanUtils(); ConvertUtils.register(new Converter(){ /* * 参数一:java.util.Date.class(目标类型) * 参数二:传入的参数类型,即java.lang.String */ public Object convert(Class arg0, Object arg1) { String strBirthday = (String)arg1; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { return sdf.parse(strBirthday); } catch (ParseException e) { e.printStackTrace(); } return null; } }, java.util.Date.class); try { bu.setProperty(s, "name", "well"); bu.setProperty(s, "age", "21"); bu.setProperty(s, "birthday","1990-10-15"); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } System.out.println(s.getName()+" "+s.getAge() + " "+ s.getBirthday()); //output:well 21 Mon Oct 15 00:00:00 CST 1990 } }
还有一种简化代码的方式:
利用Converter接口的子类来直接实现,而非实现Converter接口来实现。
如下,直接使用DateLocaleConverter()类
package web.java.beanutils; import java.lang.reflect.InvocationTargetException; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.locale.converters.DateLocaleConverter; import org.junit.Test; public class Demo2 { @Test public void test1(){ Student s = new Student(); BeanUtils bu = new BeanUtils(); ConvertUtils.register(new DateLocaleConverter(), java.util.Date.class); try { bu.setProperty(s, "name", "well"); bu.setProperty(s, "age", "21"); bu.setProperty(s, "birthday","1990-10-15"); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } System.out.println(s.getName()+" "+s.getAge() + " "+ s.getBirthday()); //output:well 21 Mon Oct 15 00:00:00 CST 1990 } }
还有一个问题是,输出的时间为CST时间,而非我们熟悉的中国常用的时间表示形式,这里可以通过如下方法调用
s.getBirthday().toLocaleString()
这样,可以修正时间的显示(注意,这个方法已经过时了!)
5.几点总结
(1)BeanUtils框架能够对String<---->基本数据类型自动转化
(2)BeanUtils框架能够自定义转换器:ConvertUtils.register( 转换规则 ,目标对象的Class)
(3)向BeanUtils框架注册自定义转换器必须放在bu.setProperty()代码之前
(4)使用BeanUtils内置String->Date的转换器:ConvertUtils.register(new DateLocaleConverter(),java.util.Date.class);