BeanUtils. copy Properties 는 집합 을 복사 하 는 솔 루 션 을 지원 하지 않 습 니 다.

11448 단어 구덩이.
작업 중 에 Spring 의 도구 류 인 BeanUtils. copy Properties 를 자주 사용 하여 bean 속성 을 복사 합 니 다. 이곳 의 복 제 는 얕 은 복사 에 속 합 니 다.집합 과 배열 을 복사 할 수 없습니다.본 고 는 이 도구 에 대해 약간의 테스트 를 진행 할 것 이다.글 끝 에 집합 속성 을 복제 하 는 해결 방안 을 제시 할 것 이다.
  • 준비 작업: 테스트 준비 에 필요 한 클래스
  • @Data
    public class Class {
        private People[] member;
        private People teacher;
        private List<People> student;
    }
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class People {
        private Integer id;
        private String name;
        private Integer age;
        private Integer sex;
    }
    
  • 테스트 코드: BeanUtils. copy Properties 가 복사 배열 과 집합 을 지원 하 는 지, 그리고 솔 루 션
  • 을 테스트 합 니 다.
    public static void main(String[] args) {
    	//        
    	People[] member = new People[3];
    	member[0] = new People(1, "  ", 30, 1);
    	member[1] = new People(2, "  ", 15, 1);
    	member[2] = new People(3, "  ", 15, 1);
    	People[] member1 = new People[]{};
    	BeanUtils.copyProperties(member, member1);
    	System.out.println("        :" + (member1.length == 0 ? false : true));
    	//   List   (Map     ,   )
    	List student = new ArrayList<>();
    	student.add(member[1]);
    	student.add(member[2]);
    	List student1 = new ArrayList<>();
    	BeanUtils.copyProperties(student, student1);
    	System.out.println("BeanUtils.copyProperties      List:" + (student1.isEmpty() ? false : true));
    	//   JSON    List   (    List,   Map              ,         ,    )
    	student1 = JSON.parseArray(JSON.toJSONString(student), People.class);
    	System.out.println("  JSON    List:" + student1);
    	System.out.println("  JSON       :" + (student.get(0) != student1.get(0) ? true : false));
    	//        
    	Class source = new Class();
    	source.setMember(member);
    	source.setTeacher(member[0]);
    	source.setStudent(student);
    	Class target = new Class();
    	BeanUtils.copyProperties(source, target);
    	System.out.println("BeanUtils.copyProperties     :" + (source.getMember() != target.getMember() ? true : false));
    }
    
  • 테스트 결과
  •         :false
    BeanUtils.copyProperties      List:false
      JSON    List:[People(id=2, name=  , age=15, sex=1), People(id=3, name=  , age=15, sex=1)]
      JSON       :true
    BeanUtils.copyProperties     :false
    

    List 에 대한 복 제 는 JSON 도 구 를 통 해 하 는 것 을 제외 하고 가장 간단 한 것 은 집합 속성 을 순환 적 으로 복제 하 는 것 이다. 다음은 두 가지 방법의 효율 을 측정 한다.
    public static void main(String[] args) {
    	int count = 1;
    	System.out.println("      :" + count);
    	List<People> source = new LinkedList<>();
    	List<People> target = new LinkedList<>();
    	long start;
    	
    	for (int i = 0; i < count; i++) {
    	    source.add(new People(1, "ly", 25, 1));
    	}
    	
    	start = System.nanoTime();
    	target = JSON.parseArray(JSON.toJSONString(source), People.class);
    	System.out.println("JSON:" + (System.nanoTime() - start));
    	
    	start = System.nanoTime();
    	for (int i = 0; i < count; i++) {
    	    People p = new People();
    	    BeanUtils.copyProperties(source.get(i), p);
    	    target.add(p);
    	}
    	System.out.println("BeanUtils.copyProperties" + (System.nanoTime() - start));
    }
    

    count = 1, 10, 100, 1000, 10000, 100000 의 결 과 를 각각 테스트 합 니 다.함께 실행 하 는 데 영향 을 미 치 는 것 을 방지 하기 위해 매번 한 가지 복제 방법의 한 가지 상황 만 테스트 하고 총 12 회 집행 한다.비 교 를 통 해 JSON 을 통 해 BeanUtils 보다 속성 이 빠 르 고,
          :1
    JSON:154767336
    Bean:275182853
          :10
    JSON:165678435
    Bean:275301421
          :100
    JSON:167937206
    Bean:328461161
          :1000
    JSON:187832969
    Bean:315815289
          :10000
    JSON:297461312
    Bean:362763360
          :100000
    JSON:562035707
    Bean:5815319343
    

    복사 List, Map 을 다음 과 같이 해결 합 니 다.
    public static  List copyList(List list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList();
        }
        return JSON.parseArray(JSON.toJSONString(list), list.get(0).getClass());
    }
    
    public static Map copyMap(Map map) {
        return JSON.parseObject(JSON.toJSONString(map));
    }
    

    좋은 웹페이지 즐겨찾기