BeanUtils copyProperties API忽略null和特定属性
Spring的BeanUtils.copyProperties()
提供了在复制bean时忽略特定属性的选项:
public static void copyProperties(Object source, Object target, String[] ignoreProperties) throws BeansException
Apache Commons BeanUtils是否提供类似的function?
使用Spring的BeanUtils.copyProperties()
时也可以忽略空值,我在Commons BeanUtils中看到了这个特性:
Date defaultValue = null; DateConverter converter = new DateConverter(defaultValue); ConvertUtils.register(converter, Date.class);
我可以用Spring的BeanUtils实现同样的目标吗?
如果要忽略null
-value,则必须在复制属性之前使用以下代码行:
BeanUtilsBean.getInstance().getConvertUtils().register(false, false, 0);
如果您使用org.springframework.beans.BeanUtils
,则可以使用方法copyProperties(Object source, Object target, String... ignoreProperties)
忽略特定copyProperties(Object source, Object target, String... ignoreProperties)
。 一个例子,
BeanUtils.copyProperties(sourceObj, targetObj, "aProperty", "another");