Java BeanUtils.copyProperties是否缺少深度嵌套的变量?
我正在使用BeanUtils.copyProperties将一个对象的整个内容复制到另一个继承自该对象的内容中 这是上下文,从中复制值的域对象包含一组自定义类型Xref的对象。该自定义类型具有一个嵌入类,其中包含各种类类型的各种字段 由于某种原因,封装在嵌入对象中的对象中的一个字段无法复制。但我所需要的大部分东西都被复制过来了 举个例子:Java BeanUtils.copyProperties是否缺少深度嵌套的变量?,java,apache-commons-beanutils,Java,Apache Commons Beanutils,我正在使用BeanUtils.copyProperties将一个对象的整个内容复制到另一个继承自该对象的内容中 这是上下文,从中复制值的域对象包含一组自定义类型Xref的对象。该自定义类型具有一个嵌入类,其中包含各种类类型的各种字段 由于某种原因,封装在嵌入对象中的对象中的一个字段无法复制。但我所需要的大部分东西都被复制过来了 举个例子: class Source { private Set<Xref> xref; ... } class Xref { ... public sta
class Source {
private Set<Xref> xref;
...
}
class Xref {
...
public static class primaryKey {
...
private MyObj obj;
}
}
class MyObj {
private Integer id;
...
}
类源代码{
私有集外部参照;
...
}
类外部参照{
...
公共静态类主密钥{
...
私人MyObj obj;
}
}
MyObj类{
私有整数id;
...
}
如果我尝试使用BeanUtils.copyProperties将“Source”对象的内容复制到“SourceExtended”对象中,则使用这些名称不会复制Source.xrefs.get(0)的值。getPrimaryKey().getObj().getId()的值。
在原始对象中它有一个值,但在目标对象中它为null
知道为什么吗
谢谢。来自:
请注意,此方法旨在执行属性的“浅复制”,因此不会复制复杂属性(例如嵌套属性)
这里是我如何处理这个与春天。可能会有所帮助。我的方法是Spring的shallowCopyFieldState的副本,但允许使用字段过滤器。忽略静力学和期末考试 我的方法
public static void shallowCopyFieldState(final Object src, final Object dest, final FieldFilter filter)
throws IllegalArgumentException {
if (src == null) {
throw new IllegalArgumentException("Source for field copy cannot be null");
}
if (dest == null) {
throw new IllegalArgumentException("Destination for field copy cannot be null");
}
if (!src.getClass().isAssignableFrom(dest.getClass())) {
throw new IllegalArgumentException("Destination class [" + dest.getClass().getName()
+ "] must be same or subclass as source class [" + src.getClass().getName() + "]");
}
org.springframework.util.ReflectionUtils.doWithFields(src.getClass(),
new org.springframework.util.ReflectionUtils.FieldCallback() {
public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException {
org.springframework.util.ReflectionUtils.makeAccessible(field);
final Object srcValue = field.get(src);
field.set(dest, srcValue);
}
}, filter);
}
Spring的doWithFields:
/**
* Invoke the given callback on all fields in the target class,
* going up the class hierarchy to get all declared fields.
* @param targetClass the target class to analyze
* @param fc the callback to invoke for each field
* @param ff the filter that determines the fields to apply the callback to
*/
public static void doWithFields(Class targetClass, FieldCallback fc, FieldFilter ff)
throws IllegalArgumentException {
// Keep backing up the inheritance hierarchy.
do {
// Copy each field declared on this class unless it's static or file.
Field[] fields = targetClass.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
// Skip static and final fields.
if (ff != null && !ff.matches(fields[i])) {
continue;
}
try {
fc.doWith(fields[i]);
}
catch (IllegalAccessException ex) {
throw new IllegalStateException(
"Shouldn't be illegal to access field '" + fields[i].getName() + "': " + ex);
}
}
targetClass = targetClass.getSuperclass();
}
while (targetClass != null && targetClass != Object.class);
}
/**
*对目标类中的所有字段调用给定回调,
*向上移动类层次结构以获取所有声明的字段。
*@param targetClass要分析的目标类
*@param fc为每个字段调用的回调
*@param ff确定要应用回调的字段的筛选器
*/
公共静态无效字段(类targetClass、FieldCallback fc、FieldFilter ff)
抛出IllegalArgumentException{
//继续备份继承层次结构。
做{
//复制此类上声明的每个字段,除非它是静态字段或文件字段。
Field[]fields=targetClass.getDeclaredFields();
for(int i=0;i