gpt4 book ai didi

java - Spring BeanUtils 使用 List 字段复制属性

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:06 24 4
gpt4 key购买 nike

我有如下所示的 Foo 和 Item 类。

import java.util.ArrayList;
import java.util.List;

public class Foo {
private Long id;
private List<Item> items;

public Foo(Long id) {
this.id = id;
this.items = new ArrayList<Item>();
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public List<Item> getItems() {
return items;
}

public void setItems(List<Item> items) {
this.items = items;
}
}


public class Item {
private String bar;

public Item(String bar) {
this.bar = bar;
}

public String getBar() {
return bar;
}

public void setBar(String bar) {
this.bar = bar;
}

@Override
public String toString() {
return "Item{" + "bar='" + bar + '\'' + '}';
}
}

当我使用 spring BeanUtils 复制 Foo 类时,列表字段的引用没有改变。

import org.springframework.beans.BeanUtils;

public class SimpleCopyMain {

public static void main(String[] args) {
Foo foo = new Foo(1L);
foo.getItems().add(new Item("item1"));
foo.getItems().add(new Item("item2"));

Foo fooSnapShot = new Foo(100L);
BeanUtils.copyProperties(foo,fooSnapShot);

foo.setId(999L);
System.out.println("fooSnapShot id field value is not changing as expected : " + fooSnapShot.getId());

foo.getItems().add(new Item("item3"));

System.out.println("fooSnapShot items value is changing unexpectedly : " + fooSnapShot.getItems());
}
}

SimpleCopyMain 类的输出如下:

fooSnapShot id field value is not changing as expected : 1
fooSnapShot items value is changing unexpectedly : [Item{bar='item1'}, Item{bar='item2'}, Item{bar='item3'}]

但是,当我为列表字段创建一个新实例并逐个复制引用时,我得到了预期的行为。

import java.util.ArrayList;

import org.springframework.beans.BeanUtils;

public class CopyMain {

public static void main(String[] args) {
Foo foo = new Foo(1L);
foo.getItems().add(new Item("item1"));
foo.getItems().add(new Item("item2"));

Foo fooSnapShot = new Foo(100L);
BeanUtils.copyProperties(foo, fooSnapShot);

fooSnapShot.setItems(new ArrayList<Item>(foo.getItems().size()));
for(int i = 0; i < foo.getItems().size(); i++){
Item anItem = new Item("");
BeanUtils.copyProperties(foo.getItems().get(i), anItem);
fooSnapShot.getItems().add(anItem);
}

foo.setId(999L);
System.out.println("fooSnapShot id field value is not changing as expected : " + fooSnapShot.getId());

foo.getItems().add(new Item("item3"));

System.out.println("fooSnapShot items value is is not changing : " + fooSnapShot.getItems());
}
}

这是输出:

fooSnapShot id field value is not changing as expected : 1
fooSnapShot items value is is not changing : [Item{bar='item1'}, Item{bar='item2'}]

还有我的 pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.question</groupId>
<artifactId>beanutils</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
</dependencies>
</project>

为什么 spring beanutils 不克隆列表字段?

最佳答案

根据 BeanUtil copyProperities 方法的实现,Spring 正在通过 Getters 和 Setters 复制您的数据。如果您有像 Integer 这样的基元,那没问题,但是对于您的 List 字段,您正在 Setter 中传递引用。

如果你想让它工作,你需要将你的 setter 更改为:

public void setItems(List<Item> items) {
this.items = new ArrayList<>(items);
}

这也会进行浅拷贝,但您不会有列表引用。

关于java - Spring BeanUtils 使用 List 字段复制属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42469618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com