两种BeanUtils不同与用法
来自:https://www.cnblogs.com/ahri-gx/p/10551340.html
1)两者的copyProperties方法参数位置不同
org.springframework.beans.BeanUtils: copyProperties(sourceDemo, targetDemo)
org.apache.commons.beanutils.BeanUtils: copyProperties(targetDemo, sourceDemo)
2)要求两个Bean的属性名相同,且有对应的setXxx方法和getXxx方法。其实底层原理是使用sourceDemo的getXxx方法和targetDemo的setXxx方法
3)sourceDemo有的属性而targetDemo没有的属性,不会封装到targetDemo对象中;
targetDemo有的属性而sourceDemo没有的属性,会封装到targetDemo中,数据为默认值(注意基本类型默认值与引用类型默认值不同)
4)类型转换问题
a)基本类型与其对应的封装类型可以相互转换
b)org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils对于String和Date类型转换的情况是不同;
日期可以查看这个:https://blog.csdn.net/qidasheng2012/article/details/84562732
spring的:
public static void main(String[] args) throws IllegalAccessException, InvocationTargetException {
SourceDemo sourceDemo = new SourceDemo();
sourceDemo.setId(10);
sourceDemo.setName("姓名");
sourceDemo.setAddress("地址");
sourceDemo.setCreateTime(new Date());
TargetDemo targetDemo = new TargetDemo();
BeanUtils.copyProperties(sourceDemo, targetDemo);
System.out.println(targetDemo);
org.apache.commons.beanutils.BeanUtils.copyProperties(targetDemo, sourceDemo);
}