知识点

org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils都提供了copyProperties方法,作用是将一个Bean对象中的数据封装到另一个属性结构相似的Bean对象中

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类型转换的情况是不同的,具体看以下案例

 

案例一

SourceDemo

package com.fortis.customer.test;

import java.util.Date;

import org.apache.commons.lang3.builder.ToStringBuilder;

public class SourceDemo {

    private int id;

    private String name;

    private String address;

    private Date createTime;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}

TargetDemo

package com.fortis.customer.test;

import java.util.Date;

import org.apache.commons.lang3.builder.ToStringBuilder;

public class TargetDemo {

    private Integer id;

    private String name;

    private String email;

    private Date createTime;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}

Test

package com.fortis.customer.test;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

public class Test {

    public static void main(String[] args) throws IllegalAccessException, InvocationTargetException {

        SourceDemo sourceDemo = new SourceDemo();
        sourceDemo.setId();
        sourceDemo.setName("姓名");
        sourceDemo.setAddress("地址");
        sourceDemo.setCreateTime(new Date());

        TargetDemo targetDemo = new TargetDemo();
        org.springframework.beans.BeanUtils.copyProperties(sourceDemo, targetDemo);
        System.out.println(targetDemo);

        org.apache.commons.beanutils.BeanUtils.copyProperties(targetDemo, sourceDemo);
        System.out.println(targetDemo);
    }
}

测试效果:

[id=,name=姓名,email=< :: CST ]
[id=,name=姓名,email=< :: CST ]

结论:基本类型与其对应的封装类型是可以相互转换的

案例二

SourceDemo

package com.fortis.customer.test;

import java.util.Date;

import org.apache.commons.lang3.builder.ToStringBuilder;

public class SourceDemo {

    private int id;

    private String name;

    private String address;

    private Date createTime;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}

TargetDemo

package com.fortis.customer.test;

import org.apache.commons.lang3.builder.ToStringBuilder;

public class TargetDemo {

    private Integer id;

    private String name;

    private String email;

    private String createTime;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}

Test

package com.fortis.customer.test;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

public class Test {

    public static void main(String[] args) throws IllegalAccessException, InvocationTargetException {

        SourceDemo sourceDemo = new SourceDemo();
        sourceDemo.setId();
        sourceDemo.setName("姓名");
        sourceDemo.setAddress("地址");
        sourceDemo.setCreateTime(new Date(0));

        TargetDemo targetDemo = new TargetDemo();
        org.springframework.beans.BeanUtils.copyProperties(sourceDemo, targetDemo);
        System.out.println(targetDemo);

        //org.apache.commons.beanutils.BeanUtils.copyProperties(targetDemo, sourceDemo);
        //System.out.println(targetDemo);
    }
}

测试效果:

Exception in thread "main" org.springframework.beans.FatalBeanException: Could not copy properties from source to target; nested exception is java.lang.IllegalArgumentException: argument type mismatch
    at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:)
    at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:)
    at com.fortis.customer.test.Test.main(Test.java:)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:)
    ...  more

结论:org.springframework.beans.BeanUtils.copyProperties不可以将java.util.Date类型转换成String类型

同时:也不可将java.sql.Date类型转换成String类型(因为网上搜的说可以支持java.sql.Date类型,我自己测试了一下发现不可以,还是信自己靠谱)

Exception in thread "main" org.springframework.beans.FatalBeanException: Could not copy properties from source to target; nested exception is java.lang.IllegalArgumentException: argument type mismatch
    at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:)
    at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:)
    at com.fortis.customer.test.Test.main(Test.java:)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:)
    ...  more

案例三

Test

package com.fortis.customer.test;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

public class Test {

    public static void main(String[] args) throws IllegalAccessException, InvocationTargetException {

        SourceDemo sourceDemo = new SourceDemo();
        sourceDemo.setId();
        sourceDemo.setName("姓名");
        sourceDemo.setAddress("地址");
        sourceDemo.setCreateTime(new Date(0));

        TargetDemo targetDemo = new TargetDemo();
        //org.springframework.beans.BeanUtils.copyProperties(sourceDemo, targetDemo);
        //System.out.println(targetDemo);

        org.apache.commons.beanutils.BeanUtils.copyProperties(targetDemo, sourceDemo);
        System.out.println(targetDemo);
    }
}

注:SourceDemo和TargetDemo与案例二相同

测试效果:java.util.Date

[id=,name=姓名,email=< :: CST ]

结论:org.apache.commons.beanutils.BeanUtils.copyProperties可以将java.util.Date类型转换成String类型

测试效果:java.sql.Date

[id=10,name=姓名,email=<null>,createTime=1970-01-01]

结论:org.apache.commons.beanutils.BeanUtils.copyProperties可以将java.sql.Date类型转换成String类型

注意:虽然都可以转换,但发现两者的日期格式还是不同的

案例四:

SourceDemo

package com.fortis.customer.test;

import org.apache.commons.lang3.builder.ToStringBuilder;

public class SourceDemo {

    private int id;

    private String name;

    private String address;

    private String createTime;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}

TargetDemo

package com.fortis.customer.test;

import java.util.Date;

import org.apache.commons.lang3.builder.ToStringBuilder;

public class TargetDemo {

    private Integer id;

    private String name;

    private String email;

    private Date createTime;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}

Test

package com.fortis.customer.test;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

    public static void main(String[] args) throws IllegalAccessException, InvocationTargetException {

        SourceDemo sourceDemo = new SourceDemo();
        sourceDemo.setId();
        sourceDemo.setName("姓名");
        sourceDemo.setAddress("地址");
        sourceDemo.setCreateTime()));

        TargetDemo targetDemo = new TargetDemo();
        org.springframework.beans.BeanUtils.copyProperties(sourceDemo, targetDemo);
        System.out.println(targetDemo);

        //org.apache.commons.beanutils.BeanUtils.copyProperties(targetDemo, sourceDemo);
        //System.out.println(targetDemo);
    }
}

测试效果:

Exception in thread "main" org.springframework.beans.FatalBeanException: Could not copy properties from source to target; nested exception is java.lang.IllegalArgumentException: argument type mismatch
    at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:)
    at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:)
    at com.fortis.customer.test.Test.main(Test.java:)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:)
    ...  more

结论:org.springframework.beans.BeanUtils.copyProperties不可以将String类型转换成java.util.Date类型

同时:也不可将String类型转换成java.sql.Date类型

案例五

Test

package com.fortis.customer.test;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

    public static void main(String[] args) throws IllegalAccessException, InvocationTargetException {

        SourceDemo sourceDemo = new SourceDemo();
        sourceDemo.setId(10);
        sourceDemo.setName("姓名");
        sourceDemo.setAddress("地址");
        sourceDemo.setCreateTime(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(0)));

        TargetDemo targetDemo = new TargetDemo();
        //org.springframework.beans.BeanUtils.copyProperties(sourceDemo, targetDemo);
        //System.out.println(targetDemo);

        org.apache.commons.beanutils.BeanUtils.copyProperties(targetDemo, sourceDemo);
        System.out.println(targetDemo);
    }
}

测试效果:java.util.Date

log4j:WARN No appenders could be found for logger (org.apache.commons.beanutils.converters.BooleanConverter).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.apache.commons.beanutils.ConversionException: DateConverter does not support default String to 'Date' conversion.
    at org.apache.commons.beanutils.converters.DateTimeConverter.toDate(DateTimeConverter.java:)
    at org.apache.commons.beanutils.converters.DateTimeConverter.convertToType(DateTimeConverter.java:)
    at org.apache.commons.beanutils.converters.AbstractConverter.convert(AbstractConverter.java:)
    at org.apache.commons.beanutils.converters.ConverterFacade.convert(ConverterFacade.java:)
    at org.apache.commons.beanutils.BeanUtilsBean.convert(BeanUtilsBean.java:)
    at org.apache.commons.beanutils.BeanUtilsBean.copyProperty(BeanUtilsBean.java:)
    at org.apache.commons.beanutils.BeanUtilsBean.copyProperties(BeanUtilsBean.java:)
    at org.apache.commons.beanutils.BeanUtils.copyProperties(BeanUtils.java:)
    at com.fortis.customer.test.Test.main(Test.java:)

结论:org.apache.commons.beanutils.BeanUtils

看错误日志,发现说是默认的转换器是不可以将String类型转换成java.util.Date,可能自己配置一个转换器就可以转了吧,我没有测试过

测试效果:java.sql.Date

log4j:WARN No appenders could be found for logger (org.apache.commons.beanutils.converters.BooleanConverter).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.apache.commons.beanutils.ConversionException: String must be in JDBC format [yyyy-MM-dd] to create a java.sql.Date
    at org.apache.commons.beanutils.converters.DateTimeConverter.toDate(DateTimeConverter.java:)
    at org.apache.commons.beanutils.converters.DateTimeConverter.convertToType(DateTimeConverter.java:)
    at org.apache.commons.beanutils.converters.AbstractConverter.convert(AbstractConverter.java:)
    at org.apache.commons.beanutils.converters.ConverterFacade.convert(ConverterFacade.java:)
    at org.apache.commons.beanutils.BeanUtilsBean.convert(BeanUtilsBean.java:)
    at org.apache.commons.beanutils.BeanUtilsBean.copyProperty(BeanUtilsBean.java:)
    at org.apache.commons.beanutils.BeanUtilsBean.copyProperties(BeanUtilsBean.java:)
    at org.apache.commons.beanutils.BeanUtils.copyProperties(BeanUtils.java:)
    at com.fortis.customer.test.Test.main(Test.java:)

结论:org.apache.commons.beanutils.BeanUtils

看错误日志,发现说是需要将String类型转成java.sql.Date,使用simpleDateFormat是将String类型转成java.util.Date,可能转成java.sql.Date就可以了吧,我没测试过

注意:我这里说的转换,单纯是指这一行代码sourceDemo.setCreateTime(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(0)));而不是两个Bean的类型转换

总之,无论是org.springframework.beans.BeanUtils还是org.apache.commons.beanutils.BeanUtils,默认情况下都不能将String类型转成Date类型

案例六:

SourceDemo:

  private String name;

TargetDemo:

  private String name = “马超”;

  private String email = "张飞";

复制之后的结果:name = null,email = “张飞”

SourceDemo:

  private String name = “诸葛亮”;

TargetDemo:

  private String name = “马超”;

  private String email = "张飞";

复制之后的结果:name = “诸葛亮”,email = “张飞”

注:使用两种BeanUtils效果相同

org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils的copyProperties用法区别的更多相关文章

  1. Apache Commons Beanutils 一 (使用PropertyUtils访问Bean属性)

    BeanUtils简要描述 beanutils,顾名思义,是java bean的一个工具类,可以帮助我们方便的读取(get)和设置(set)bean属性值.动态定义和访问bean属性: 细心的话,会发 ...

  2. Apache Commons Beanutils 三 (BeanUtils、ConvertUtils、CollectionUtils...)

    前言 前面已经学习了Apache Commons Beanutils包里的PropertyUtils和动态bean,接下来将学习剩下的几个工具类,个人觉得还是非常实用的,特别是CollectionUt ...

  3. Apache Commons 工具类介绍及简单使用

    转自:http://www.cnblogs.com/younggun/p/3247261.html Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下 ...

  4. Apache Commons 工具类简单使用

    Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. 组件 功能介绍 BeanUtils 提供了对于 ...

  5. Apache Commons 工具集介绍

    Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. 组件 功能介绍 BeanUtils 提供了对于 ...

  6. Apache Commons 工具类介绍及简单使用(转载)

    原文链接 http://www.cnblogs.com/younggun/p/3247261.html Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动 ...

  7. Java:Apache Commons 工具类介绍及简单使用

    Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. Commons简介 组件 功能介绍 commo ...

  8. 一篇关于apache commons类库的详解

    1.1. 开篇 在Java的世界,有很多(成千上万)开源的框架,有成功的,也有不那么成功的,有声名显赫的,也有默默无闻的.在我看来,成功而默默无闻的那些框架值得我们格外的尊敬和关注,Jakarta C ...

  9. 一篇关于apache commons类库的详解[转]

    1.1. 开篇 在Java的世界,有很多(成千上万)开源的框架,有成功的,也有不那么成功的,有声名显赫的,也有默默无闻的.在我看来,成功而默默无闻的那些框架值得我们格外的尊敬和关注,Jakarta C ...

随机推荐

  1. sorted新用法

  2. BZOJ1828[USACO 2010 Mar Gold 2.Barn Allocation]——贪心+线段树

    题目描述 输入 第1行:两个用空格隔开的整数:N和M * 第2行到N+1行:第i+1行表示一个整数C_i * 第N+2到N+M+1行: 第i+N+1行表示2个整数 A_i和B_i 输出 * 第一行: ...

  3. BZOJ2724 [Violet]蒲公英(分块)

    区间众数.分块,预处理任意两块间所有数的众数,和每块中所有数的出现次数的前缀和.查询时对不是整块的部分暴力,显然只有这里出现的数可能更新答案.于是可以优美地做到O(n√n). #include< ...

  4. 【长期更新】 PHP题目

    1.要求在一组数中,插入一个新数,并维护原来的排序方式不变 <?php //1.要求在一组数中,插入一个新数,并维护原来的排序方式不变 function insertArr($arr,$val) ...

  5. nagios 配置 check_traffic 流量监控模块(Server 端)

    安装软件包yum -y install net-snmp*chkconfig nrpe onchkconfig snmpd onchkconfig nagios on 修改snmp参数,vi /etc ...

  6. Leetcode 70.爬楼梯 By Python

    假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解释: 有两 ...

  7. 洛谷 P3705 [SDOI2017]新生舞会 解题报告

    P3705 [SDOI2017]新生舞会 题目描述 学校组织了一次新生舞会,\(Cathy\)作为经验丰富的老学姐,负责为同学们安排舞伴. 有\(n\)个男生和\(n\)个女生参加舞会买一个男生和一个 ...

  8. tf 数据读取

    tf.train.batch( tensors, batch_size, num_threads=1, capacity=32, enqueue_many=False, shapes=None, dy ...

  9. Android 屏幕手势滑动中onFling()函数的技巧分析

    关于如何处理手势操作以及那四个基本固定的顺序我就不讲解了,这里直接跳到我们获得瞬间滑动后回调onFling()这个抽象函数时,应该如何根据参数比较准确的判断滑动方向.如果你没有前面的基础知识,你可以去 ...

  10. 关于next.js中的css

    css进行了全局和局部的限制 export default () => ( <div className='hello'> <p>Hello World</p> ...