BeanUtils提供对Java反射和自省API的包装。其主要目的是利用反射机制对JavaBean的属性进行处理。我们知道,一个JavaBean通常包含了大量的属性,很多情况下,对JavaBean的处理导致大量get/set代码堆积,增加了代码长度和阅读代码的难度。
下面通过代码来理解BeanUtils
首先创建两个类
Person.java
- package com.org;
- import java.util.Date;
- public class Person {
- private String name;
- private String sex;
- private int age;
- private Date birthday;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- 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;
- }
- }
- package com.org;
- import java.util.Date;
- public class Person {
- private String name;
- private String sex;
- private int age;
- private Date birthday;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- 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;
- }
- }
Student.java
- package com.org;
- import java.util.Date;
- public class Student {
- private String name;
- private int age;
- private String address;
- private Date birthday;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- }
- package com.org;
- import java.util.Date;
- public class Student {
- private String name;
- private int age;
- private String address;
- private Date birthday;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- }
测试类 BeanUtilTest.java
- package com.org;
- import java.lang.reflect.InvocationTargetException;
- import java.util.Date;
- //import com.sun.org.apache.commons.beanutils.BeanUtils;
- import org.apache.commons.beanutils.BeanUtils;
- public class BeanUtilTest {
- public static void main(String[] args) {
- Person per = new Person();
- Student stu = new Student();
- per.setName("zhangsan");
- per.setSex("男");
- per.setAge(20);
- per.setBirthday(new Date());
- stu.setName("wuangwu");
- stu.setAddress("北京市");
- try {
- BeanUtils.copyProperties(stu, per);
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(stu.getName());
- System.out.println(stu.getAge());
- System.out.println(stu.getAddress());
- System.out.println(stu.getBirthday());
- }
- }
- package com.org;
- import java.lang.reflect.InvocationTargetException;
- import java.util.Date;
- //import com.sun.org.apache.commons.beanutils.BeanUtils;
- import org.apache.commons.beanutils.BeanUtils;
- public class BeanUtilTest {
- public static void main(String[] args) {
- Person per = new Person();
- Student stu = new Student();
- per.setName("zhangsan");
- per.setSex("男");
- per.setAge(20);
- per.setBirthday(new Date());
- stu.setName("wuangwu");
- stu.setAddress("北京市");
- try {
- BeanUtils.copyProperties(stu, per);
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(stu.getName());
- System.out.println(stu.getAge());
- System.out.println(stu.getAddress());
- System.out.println(stu.getBirthday());
- }
- }
程序输出结果为:
- zhangsan
- 20
- 北京市
- Fri Nov 25 13:03:29 CST 2011
- zhangsan
- 20
- 北京市
- Fri Nov 25 13:03:29 CST 2011
可见Student自己设置的值都被Person给覆盖了
再做下面两个测试
把Person中的setName和setAge 改为setname setage程序输出结果为
- zhangsan
- 20
- 北京市
- Fri Nov 25 13:08:34 CST 2011
- zhangsan
- 20
- 北京市
- Fri Nov 25 13:08:34 CST 2011
结果没有改变
而如果改为setNAme, setAGe结果为:
- wuangwu
- 0
- 北京市
- Fri Nov 25 13:10:25 CST 2011
- wuangwu
- 0
- 北京市
- Fri Nov 25 13:10:25 CST 2011
我们发现结果改变了 内容没有被覆盖
注: 当我们加载BeanUtils类时 可能会报如下错误
- Exception in thread "main" java.lang.ExceptionInInitializerError
- at com.org.BeanUtilTest.main(BeanUtilTest.java:101)
- Caused by: com.sun.org.apache.commons.logging.LogConfigurationException: com.sun.org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException) (Caused by com.sun.org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException))
- at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
- at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
- at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
- at com.sun.org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
- at com.sun.org.apache.commons.beanutils.BeanUtils.<clinit>(BeanUtils.java:111)
- ... 1 more
- Caused by: com.sun.org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException)
- at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:397)
- at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
- ... 5 more
- Caused by: java.lang.NullPointerException
- at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:374)
- ... 6 more
- Exception in thread "main" java.lang.ExceptionInInitializerError
- at com.org.BeanUtilTest.main(BeanUtilTest.java:101)
- Caused by: com.sun.org.apache.commons.logging.LogConfigurationException: com.sun.org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException) (Caused by com.sun.org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException))
- at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
- at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
- at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
- at com.sun.org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
- at com.sun.org.apache.commons.beanutils.BeanUtils.<clinit>(BeanUtils.java:111)
- ... 1 more
- Caused by: com.sun.org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException)
- at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:397)
- at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
- ... 5 more
- Caused by: java.lang.NullPointerException
- at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:374)
- ... 6 more
产生错误的原因是引入的包不对,应引入 import org.apache.commons.beanutils.BeanUtils;
而不应是import com.sun.org.apache.commons.beanutils.BeanUtils;
另外所需jar包为:
commons-beanutils-1.8.3.jar
commons-logging-1.1.1.jar
还有一个很邪门的问题 实在令我不解
就是如果把Person,Student 测试类写在一起的话 会产生问题
类似与这样的结构
- class Person{
- .....
- }
- class Student{
- .....
- }
- public class BeanUtilTest {
- public static void main(String[] args) {
- ......
- }
- }
- 上一篇mysql存储过程示例
- 下一篇java自定义异常
-
猜你在找
- 【精品课程】竟有如此全面的软考资料?“碉堡”了!
- 【精品课程】HTML 5全掌控
- 【精品课程】Cocos2d-x实战-手把手教你上线项目-迷失航线
- 【精品课程】Swift语言进阶
- 【精品课程】深入浅出MySQL入门必备
src="http://blog.csdn.net/common/ad.html?t=4&containerId=ad_cen&frmId=ad_frm_0" style="border-width: 0px; overflow: hidden; width: 746px; height: 97px;" scrolling="no" id="ad_frm_0" frameborder="0">
src="http://blog.csdn.net/common/ad.html?t=5&containerId=ad_bot&frmId=ad_frm_1" style="border-width: 0px; overflow: hidden; width: 746px; height: 0px;" scrolling="no" id="ad_frm_1" frameborder="0">
核心技术类目
全部主题
Hadoop
AWS
移动游戏
Java
Android
iOS
Swift
智能硬件
Docker
OpenStack
VPN
Spark
ERP
IE10
Eclipse
CRM
JavaScript
数据库
Ubuntu
NFC
WAP
jQuery
BI
HTML5
Spring
Apache
.NET
API
HTML
SDK
IIS
Fedora
XML
LBS
Unity
Splashtop
UML
components
Windows Mobile
Rails
QEMU
KDE
Cassandra
CloudStack
FTC
coremail
OPhone
CouchBase
云计算
iOS6
Rackspace
Web App
SpringSide
Maemo
Compuware
大数据
aptech
Perl
Tornado
Ruby
Hibernate
ThinkPHP
HBase
Pure
Solr
Angular
Cloud Foundry
Redis
Scala
Django
Bootstrap
- 个人资料
-
- 访问:60534次
- 积分:1228
- 等级:
- 排名:第16917名
- 原创:68篇
- 转载:25篇
- 译文:0篇
- 评论:5条
- 文章搜索
暂无评论