原文地址: http://blog.csdn.net/itmyhome/article/details/7011656
BeanUtils提供对Java反射和自省API的包装。其主要目的是利用
反射机制对JavaBean的属性进行处理。我们知道,一个JavaBean通常包含了大量的属性,很多情况下,对JavaBean的处理导致大量get/set代码堆积,增加了代码长度和阅读代码的难度。
下面通过代码来
理解BeanUtils
首先创建两个类
Person.java
class="java" name="code">
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;
}
}
测试类 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());
}
}
程序输出结果为:
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
结果没有改变
而如果改为setNAme, setAGe结果为:
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
产生错误的原因是引入的包不对,应引入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) {
......
}
}
出现不了我们所需要的结果,郁闷中....