Spring's BeanUtils.copyProperties usage

You need to note that the BeanUtils tool class is available in the spring-bean-4.3.12.RELEASE.jar and commons-beanutils.jar packages, but the parameters of the two classes are different.

At the same time, it is found that most of the Internet's explanations of this knowledge point are wrong. I hope our programmers are more attentive to their blogs! ! !

spring-bean-4.3.12.RELEASE.jar

/**
	 * Copy the property values of the given source bean into the target bean.
	 * <p>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.
	 * <p>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);
	}

The first parameter is the object to be copied, and the second parameter is the target object

commons-beanutils.jar

  /**
     * <p>Copy property values from the origin bean to the destination bean
     * for all cases where the property names are the same.</p>
     *
     * <p>For more details see <code>BeanUtilsBean</code>.</p>
     *
     * @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 <code>dest</code> or
     *  <code>orig</code> argument is null or if the <code>dest</code>
     *  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);
    }

First, let's look at the tool classes provided by 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 +
                '}';
    }
}

output result

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

Process finished with exit code 0

When I wanted to try to use the commons-beanutils toolkit for testing, I found that Ali has been deprecated, so I will not test it anymore



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325443815&siteId=291194637