Spring的BeanUtils.copyProperties用法

大家需要注意BeanUtils工具类在spring-bean-4.3.12.RELEASE.jar和commons-beanutils.jar包中都有,但是两个类的参数是不一样的。

同时发现网对这个知识点讲解上大部分是错误的。希望我们的程序猿们对自己的博客都用心一点!!!

spring-bean-4.3.12.RELEASE.jar

/**
	 * Copy the property values of the given source bean into the target bean.
	 * 

Note: The source and target classes do not have to match or even be derived * from each other, as long as the properties match. Any bean properties that the * source bean exposes but the target bean does not will silently be ignored. *

This is just a convenience method. For more complex transfer needs, * consider using a full BeanWrapper. * @param source the source bean * @param target the target bean * @throws BeansException if the copying failed * @see BeanWrapper */ public static void copyProperties(Object source, Object target) throws BeansException { copyProperties(source, target, null, (String[]) null); }

第一个参数是待拷贝的对象,第二个参数是目标对象

commons-beanutils.jar

  /**
     * 

Copy property values from the origin bean to the destination bean * for all cases where the property names are the same.

* *

For more details see BeanUtilsBean.

* * @param dest Destination bean whose properties are modified * @param orig Origin bean whose properties are retrieved * * @throws IllegalAccessException if the caller does not have * access to the property accessor method * @throws IllegalArgumentException if the dest or * orig argument is null or if the dest * property type is different from the source type and the relevant * converter has not been registered. * @throws InvocationTargetException if the property accessor method * throws an exception * @see BeanUtilsBean#copyProperties */ public static void copyProperties(final Object dest, final Object orig) throws IllegalAccessException, InvocationTargetException { BeanUtilsBean.getInstance().copyProperties(dest, orig); }

首先我们先看spring-bean-4.3.12.RELEASE.jar提供的工具类

import org.springframework.beans.BeanUtils;

public class Test
{
    public static void main(String ... args){
        A a = new A();
        a.setName("tom");
        a.setAge(123);
        B b = new B();
        BeanUtils.copyProperties(a,b);
        System.out.println(b);
    }
}

class A{
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "A{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class B{
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "B{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

输出结果

B{name='tom', age=123}

Process finished with exit code 0

在我想尝试使用commons-beanutils工具包进行测试的时候,发现阿里已经不推荐使用,所以不再进行测试

Spring的BeanUtils.copyProperties用法_第1张图片


你可能感兴趣的:(笔记)