Java中BeanUtils.copyProperties()用法和需要导入maven依赖

1、导入的maven依赖包

        <!-- BeanUtils的依赖 -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.4</version>
        </dependency>

2、示例代码

package cn.ljxwtl.设计模式.原型模式;
 
import cn.ljxwtl.设计模式.原型模式.entity.DeepPersonWithInputStream;
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import org.apache.commons.beanutils.BeanUtils;
 
import java.lang.reflect.InvocationTargetException;
 
/**
 * @author: wtl
 * @License: (C) Copyright 2022, wtl Corporation Limited.
 * @Contact: [email protected]
 * @Date: 2022/4/17 18:00
 * @Version: 1.0
 * @Description:
 */
public class BeanUtilsTest {
 
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
 
        DeepPersonWithInputStream deepPersonWithInputStream = new DeepPersonWithInputStream("王小明", 30, Boolean.TRUE, "北京市昌平区", new ByteInputStream());
 
        DeepPersonWithInputStream deepPersonWithInputStreamCopy = new DeepPersonWithInputStream();
        BeanUtils.copyProperties(deepPersonWithInputStreamCopy,deepPersonWithInputStream);
 
        System.out.println(deepPersonWithInputStream.hashCode());
        System.out.println(deepPersonWithInputStreamCopy.hashCode());
 
        System.out.println("---------------------------------");
 
        System.out.println(deepPersonWithInputStream);
        System.out.println(deepPersonWithInputStreamCopy);
 
        System.out.println(deepPersonWithInputStream.getInputStream().hashCode());
        System.out.println(deepPersonWithInputStreamCopy.getInputStream().hashCode());
    }
}

猜你喜欢

转载自blog.csdn.net/m1195900241/article/details/125066232