CSVBeanReader
은 주어진 유형의 bean을 읽기 위해 methods을 노출합니다.Super CSV로 사용자 정의 빈 인스턴스화
오브젝트 유형이 아닌 실제 오브젝트 인스턴스를 전달할 수 있습니까 (예 : bean 인스턴스화를 사용자 정의 하시겠습니까?).
는CSVBeanReader
은 주어진 유형의 bean을 읽기 위해 methods을 노출합니다.Super CSV로 사용자 정의 빈 인스턴스화
오브젝트 유형이 아닌 실제 오브젝트 인스턴스를 전달할 수 있습니까 (예 : bean 인스턴스화를 사용자 정의 하시겠습니까?).
는업데이트 : 난 그냥 Super CSV 2.2.0을 발표했습니다
, 모두 CsvBeanReader 및 CsvDozerBeanReader 수 있도록 기존의 콩을 채 웁니다. 예!
저는 Super CSV 개발자입니다. Super CSV (CsvBeanReader 및 CsvDozerBeanReader)와 함께 제공되는 리더를 사용하여이 작업을 수행 할 방법이 없으며 이전에 기능 요청으로 올라 오지 않았습니다. feature request을 제출하면 다음 달에 추가 할 생각입니다 (이번 달에 나가기를 희망합니다).
가장 빠른 해결책은 CsvBeanReader의 원본을 자신의 것으로 복사하고 필요에 따라 수정할 수있는 자신 만의 CsvBeanReader를 작성하는 것입니다.
populateBean()
메서드를 2 가지 방법으로 리팩토링하는 것으로 시작합니다 (과부하되어 다른 메서드를 호출 할 수 있습니다).
/**
* Instantiates the bean (or creates a proxy if it's an interface), and maps the processed columns to the fields of
* the bean.
*
* @param clazz
* the bean class to instantiate (a proxy will be created if an interface is supplied), using the default
* (no argument) constructor
* @param nameMapping
* the name mappings
* @return the populated bean
* @throws SuperCsvReflectionException
* if there was a reflection exception while populating the bean
*/
private <T> T populateBean(final Class<T> clazz, final String[] nameMapping) {
// instantiate the bean or proxy
final T resultBean = instantiateBean(clazz);
return populateBean(resultBean, nameMapping);
}
/**
* Populates the bean by mapping the processed columns to the fields of the bean.
*
* @param resultBean
* the bean to populate
* @param nameMapping
* the name mappings
* @return the populated bean
* @throws SuperCsvReflectionException
* if there was a reflection exception while populating the bean
*/
private <T> T populateBean(final T resultBean, final String[] nameMapping) {
// map each column to its associated field on the bean
for(int i = 0; i < nameMapping.length; i++) {
final Object fieldValue = processedColumns.get(i);
// don't call a set-method in the bean if there is no name mapping for the column or no result to store
if(nameMapping[i] == null || fieldValue == null) {
continue;
}
// invoke the setter on the bean
Method setMethod = cache.getSetMethod(resultBean, nameMapping[i], fieldValue.getClass());
invokeSetter(resultBean, setMethod, fieldValue);
}
return resultBean;
}
그런 다음 (대신 동급의) 빈 인스턴스를 받아, (CsvBeanReader에서 사람에 따라) 자신의 read()
방법을 작성하고, 인스턴스를 받아들이는 populateBean()
를 호출 할 수 있습니다.
나는 http://sourceforge.net/p/supercsv/ (여러분에게 연습으로 남겨,하지만 당신은 질문이 있으면 그냥이 [기능 요청]을 만들었습니다 :)
요청할 것 기능 요청/28 /)이 :) –