【spring】BeanUtils属性クローンcopyProperties


まずspringの5つのコアのjarパッケージをプロジェクトに導入するspring-core-3.2.0.RELEASE.jar  spring-beans-3.2.0.RELEASE.jar  spring-context-3.2.0.RELEASE.jar  spring-expression-3.2.0.RELEASE.jar
Personクラスを新規作成
public class Person {
    private Double avgScore;
    private String courseName;
    private int num;
    private Date createTime;


    public Double getAvgScore() {
        return avgScore;
    }
    public void setAvgScore(Double avgScore) {
        this.avgScore = avgScore;
    }
    public String getCourseName() {
        return courseName;
    }
    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return "Person [avgScore=" + avgScore + ", courseName=" + courseName
                + ", num=" + num + ", createTime=" + createTime + "]";
    }

}

Studentクラスを新規作成
public class Student extends Person{
    private String studentName;

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }


    @Override
    public String toString() {
        return "Student [studentName=" + studentName + ", getAvgScore()="
                + getAvgScore() + ", getCourseName()=" + getCourseName()
                + ", getNum()=" + getNum() + ", getCreateTime()="
                + getCreateTime() + ", toString()=" + super.toString()
                + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
                + "]";
    }


}

テストクラスの作成
public class Test {

    @Test
    public void test(){
        Person p=new Person();
        p.setAvgScore(3.0);
        p.setCourseName("courseName");
        p.setCreateTime(new Date());
        p.setNum(2);

        Student s=new Student();
        s.setStudentName("studentName");
        BeanUtils.copyProperties(p, s);

        System.out.println(p);
        System.out.println("-------------------");
        System.out.println(s);
    }


}

実行結果は
Person [avgScore=3.0, courseName=courseName, num=2, createTime=Wed Aug 30 08:41:56 CST 2017]
-------------------
Student [studentName=studentName, getAvgScore()=3.0, getCourseName()=courseName, getNum()=2, getCreateTime()=Wed Aug 30 08:41:56 CST 2017, toString()=Person [avgScore=3.0, courseName=courseName, num=2, createTime=Wed Aug 30 08:41:56 CST 2017], getClass()=class cn.eaglezsx.test.Student, hashCode()=5784348]

Personオブジェクトの属性値がStudentにコピーされていることがわかります
需要:リスト内のオブジェクトの属性値を別のリストにコピーします.
    @Test
    public void t2(){

        List pL=new ArrayList<>();

        Person p=new Person();
        p.setAvgScore(3.0);
        p.setCourseName("courseName");
        p.setCreateTime(new Date());
        p.setNum(2);

        Person p2=new Person();
        p2.setAvgScore(4.0);

        pL.add(p);
        pL.add(p2);

        List sL=new ArrayList<>();

        for(Person person:pL){
            Student student=new Student();
            BeanUtils.copyProperties(person, student);
            sL.add(student);
        }


        System.out.println(pL);
        System.out.println("-----------------------");
        System.out.println(sL);

    }

実行結果は
[Person [avgScore=3.0, courseName=courseName, num=2, createTime=Wed Aug 30 08:48:49 CST 2017], Person [avgScore=4.0, courseName=null, num=0, createTime=null]]
-----------------------
[Student [studentName=null, getAvgScore()=3.0, getCourseName()=courseName, getNum()=2, getCreateTime()=Wed Aug 30 08:48:49 CST 2017, toString()=Person [avgScore=3.0, courseName=courseName, num=2, createTime=Wed Aug 30 08:48:49 CST 2017], getClass()=class cn.eaglezsx.test.Student, hashCode()=32846040], Student [studentName=null, getAvgScore()=4.0, getCourseName()=null, getNum()=0, getCreateTime()=null, toString()=Person [avgScore=4.0, courseName=null, num=0, createTime=null], getClass()=class cn.eaglezsx.test.Student, hashCode()=5890565]]

pLの総オブジェクトの値がsLにコピーされていることがわかります.
 
変換元:https://blog.csdn.net/zsx157326/article/details/77693220