有关BeanUtils的copyProperties与有关PropertyUtils的copyProperties

转:http://lu-zhou.spaces.live.com/Blog/cns!5985888977452F29!385.entry?wa=wsignin1.0&sa=95014330

 

注意:使用的struts1.3包下的类,不是用com.sun.org.apache.commons.beanutils.PropertyUtils下的类

 

建立一个struts工程;Admin Bean 和 User Bean 和一个测试类Test

 

package test; import java.util.Date; public class Admin { private Long id; private String name; private String password; private Date birthday; private Integer sex; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } }

 

package test; import java.util.Date; public class User { private Long id; private String name; private String password; private Date birthday; private Integer sex; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } }

 

package test; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.PropertyUtils; public class Test { public void testBeanUtilsCopyProperties() throws Exception { Admin admin = new Admin(); User user = new User(); user.setId(new Long(1)); user.setName("downpour"); System.out.println(); System.out.println("before copy:"); System.out.println("admin id:" + admin.getId()); System.out.println("admin name:" + admin.getName()); System.out.println("admin password:" + (admin.getPassword() == null)); System.out.println("admin birthday:" + admin.getBirthday()); System.out.println("admin sex:" + admin.getSex()); // BeanUtils.copyProperties(admin, user); PropertyUtils.copyProperties(admin, user); System.out.println(); System.out.println("------------------------------------------------"); System.out.println(); System.out.println("after copy:"); System.out.println("admin id:" + admin.getId()); System.out.println("admin name:" + admin.getName()); System.out.println("admin password:" + (admin.getPassword() == null)); System.out.println("admin birthday:" + admin.getBirthday()); System.out.println("admin sex:" + admin.getSex()); } public static void main(String[] args) { try { new Test().testBeanUtilsCopyProperties(); } catch (Exception e) { e.printStackTrace(); } } }

 

观察运行结果:

 

注意最后一行,明明没有给User赋值,做了一次copy,却带入了一个0到新的Admin中去。这不是强买强卖嘛?而且我特地用了java.lang.Integer的外覆类,那应该他是null,你就copy一个null过去呗,没事情你给我初始化一下做啥?汗~~~

后来看了源码,才发现,原来是类型转化惹的祸。由于在BeanUtils中,隐含了一个对于不同类型之间进行转化的步骤。也就是说,如果你的Source Bean中某个字段原本是String,而Dest Bean中的对应字段是Integer,它在可以进行转化时可以帮你做自动的转化。在这个转化中,会在中间变量中都先new一个default值set进去。昏过去。。。

看来这个方法在做copy时实在是不安全,尤其是对于那些null值本身有含义的情况下,只能舍弃这个方法。幸好,apache还有点人性,提供了一个叫做PropertyUtils的copyProperties方法。这个方法不会去做相应的类型转化。可以放心使用,不过如果你的Bean的类型不匹配,那么它会报错。

 

你可能感兴趣的:(exception,bean,Date,String,user,Integer)