gpt4 book ai didi

java - BeanUtils.populate 是否有递归版本?

转载 作者:行者123 更新时间:2023-12-02 07:56:41 28 4
gpt4 key购买 nike

org.apache.commons.beanutils.BeanUtils:

BeanUtils.populate(Object bean, Map<String,? extends Object> properties);
Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs.

是否有 BeanUtils.populate 的递归版本?

class Person {
String name;
Address address;
}
class Address {
String street;
int number
}

这样调用 Person 上的 populate 时,它​​也会填充地址(假设 Address 有一个默认构造函数)。

最佳答案

您可以使用点(.) 来完成此操作。假设您想在地址对象内设置街道,请指定属性名称,如 address.street 例如

Map<String, String> propertyMap = new HashMap<String, String>();
propertyMap.put("name", "James Bond");
propertyMap.put("address.street", "London");
propertyMap.put("address.number", "007");

BeanUtilsBean.getInstance().populate(bean, propertyMap);

关于java - BeanUtils.populate 是否有递归版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24890914/

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