目次
序文
一般的なコードでは、次のように、1 つのオブジェクトの複数のプロパティ値が別のオブジェクトのプロパティに割り当てられることがよくあります。
Student student = new Student("张三", 1, Arrays.asList(80, 90));
Info info = new Info();
info.setName(student.getName());
info.setScore(student.getScore());
別のオブジェクトに割り当てるプロパティが複数ある場合、set/getをたくさん書く必要があり少々面倒ですが、SpringやApacheのBeanUtilsツールクラスを利用すると便利です。
1. 準備
学生クラス:
package com.hai.tang.model;
import java.util.List;
public class Student {
public String name;
public int id;
public List<Integer> score;
public Address address;
public Student() {}
public Student(String name,int id,List<Integer> score,Address address) {
this.name = name;
this.id = id;
this.score = score;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<Integer> getScore() {
return score;
}
public void setScore(List<Integer> score) {
this.score = score;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
アドレスクラス:
package com.hai.tang.model;
public class Address {
public String type;
public String value;
public Address(String type, String value) {
this.type = type;
this.value = value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "type:" + type + ",value:" + value;
}
}
1.ApacheのBeanUtilを使用する
1.Maven座標
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
2.使用する
import com.hai.tang.model.Address;
import com.hai.tang.model.Student;
import org.apache.commons.beanutils.BeanUtils;
import java.util.Arrays;
public class MainServer {
public static void main(String[] args) throws Exception {
Student student = new Student("张三", 1, Arrays.asList(80, 90), new Address("address", "test"));
Student studentNew = new Student();
//将student复制给studentNew
BeanUtils.copyProperties(studentNew, student);
//测试,两个对象的内存地址是否一样
System.out.println(student == studentNew);
//测试,修改属性看另一个会不会变
student.setAddress(new Address("aaa", "111"));
System.out.println(studentNew.getAddress());
System.out.println(student.getAddress());
}
}
出力:
false
type:address,value:test
type:aaa,value:111
2. 次の Spring の BeanUtil を使用します
1.Maven座標
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.27</version>
</dependency>
2.使用する
import com.hai.tang.model.Address;
import com.hai.tang.model.Student;
import org.springframework.beans.BeanUtils;
import java.util.Arrays;
public class MainServer {
public static void main(String[] args) throws Exception {
Student student = new Student("张三", 1, Arrays.asList(80, 90), new Address("address", "test"));
Student studentNew = new Student();
//将student复制给studentNew
BeanUtils.copyProperties(student,studentNew);
//测试,两个对象的内存地址是否一样
System.out.println(student == studentNew);
//测试,修改属性看另一个会不会变
student.setAddress(new Address("aaa", "111"));
System.out.println(studentNew.getAddress());
System.out.println(student.getAddress());
}
}
出力:
false
type:address,value:test
type:aaa,value:111
3. 説明
1. ApacheとSpringのBeanUtils.copyPropertiesメソッドの2つの仮パラメータが逆になっているので注意が必要です。
//apache 的 BeanUtil,将student复制给studentNew
BeanUtils.copyProperties(studentNew, student);
//Spring 的 BeanUtil,将student复制给studentNew
BeanUtils.copyProperties(student,studentNew);
2. 上記の Apache または Spring の BeanUtil の出力によると、2 つのオブジェクトのハッシュ コードは等しくなく、参照されるオブジェクトのアドレスも異なり、パッケージ化されたオブジェクトに対する操作はそれぞれに影響を与えないことがわかります。ディープコピーの効果を達成するための簡単なテストを BeanUtils に見ることができます。ただし、実際のプロジェクトでのテストはディープ コピーではなく、参照されるオブジェクトの値が変更され、他の値も変更される可能性があることに注意してください。したがって、コピー後にメンバー変数の値を変更する場合は、この時点で変更される可能性のある別の変数にも注意する必要があります。ディープ コピーとシャロー コピーに興味がある場合は、私の記事「Java シャロー コピーとディープ コピー-クローン」を参照してください。
3. また、Apache の BeanUtil ソースコードはリフレクション機構やカプセル化を使用しているため、パフォーマンスが若干遅いため、Spring の BeanUtil の使用を推奨します。