2009-06-05 5 views
1

BeanUtils.copyProperties를 사용하여 한 객체의 전체 내용을 다른 객체로 복사합니다.BeanUtils.copyProperties에는 중첩 된 변수가 없습니다.

여기에는 값이 복사되는 도메인 개체에 사용자 지정 형식 Xref의 개체 집합이 포함되어 있습니다. 이 사용자 정의 유형에는 다양한 클래스 유형의 다양한 필드가있는 내장 클래스가 있습니다.

포함 된 개체에서 캡슐화 된 개체의 필드 중 하나가 복사되지 않습니다. 하지만 내가 필요한 다른 모든 것들은 복사해야합니다. 예를 들어와

:

class Source { 
private Set<Xref> xref; 
... 
} 

class Xref { 
... 
public static class primaryKey { 
... 
private MyObj obj; 
} 
} 

class MyObj { 
private Integer id; 
... 
} 

나는 "SourceExtended"에 "소스"개체의 내용을 복사 BeanUtils.copyProperties를 사용하려고하면 그 이름을 사용하여이 source.xrefs.get의 값을 객체 (0) .getPrimaryKey(). getObj(). getId()가 복사되지 않습니다. 원본 개체에는 값이 있지만 대상 개체에는 null이 있습니다. ...

어떤 아이디어가 있습니까 ???

감사합니다. Javadocs에서

답변

7

:이 방법은 속성과 너무 복잡 속성의 "얕은 복사"를 수행하도록 구성되어

참고 (예를 들어, 중첩 된 것들) 복사되지 않습니다.

+0

는 알았어요! 나는 그 때 그 일을 해결할 것이다. 고맙습니다. – Lancelot

3

다음은 Spring에서이를 처리하는 방법입니다. 도움이 될지도 몰라. 내 메소드는 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); 
} 

봄의 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); 
} 
관련 문제