BeanUtils copyProperties API忽略null和特定属性

Aru*_*run 11 java mapping spring apache-commons-beanutils

Spring BeanUtils.copyProperties()提供了在复制bean时忽略特定属性的选项:

public static void copyProperties(Object source,
                 Object target,
                 String[] ignoreProperties) throws BeansException
Run Code Online (Sandbox Code Playgroud)

Apache Commons BeanUtils是否提供类似的功能?

使用Spring时也可以忽略空值BeanUtils.copyProperties(),我在Commons BeanUtils中看到这个功能:

Date defaultValue = null;
DateConverter converter = new DateConverter(defaultValue);
ConvertUtils.register(converter, Date.class);
Run Code Online (Sandbox Code Playgroud)

我可以用Spring的BeanUtils实现同样的目标吗?

小智 7

如果要忽略null-value,则必须在复制属性之前使用以下代码行:

BeanUtilsBean.getInstance().getConvertUtils().register(false, false, 0);
Run Code Online (Sandbox Code Playgroud)


Geo*_*lou 6

如果您使用,则org.springframework.beans.BeanUtils可以使用方法忽略特定的属性copyProperties(Object source, Object target, String... ignoreProperties)。一个例子,

BeanUtils.copyProperties(sourceObj, targetObj, "aProperty", "another");
Run Code Online (Sandbox Code Playgroud)