2
A
回答
1
更新:
我剛剛發佈Super CSV 2.2.0,使CsvBeanReader和CsvDozerBeanReader既填充現有的bean。好極了!
我是一名超級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;
}
然後,您可以編寫自己的read()
方法(基於CsvBeanReader的那些),接受bean實例(而不是他們的階級),並調用接受一個實例populateBean()
。
我將離開這個作爲練習你,但如果你有任何問題,只是問:)
相關問題
- 1. 定製的bean的實例化邏輯
- 2. SpringApplication無法實例化bean
- 3. Spring bean實例化排序
- 4. 實例化春bean對象
- 5. SpringMvc實例化Bean - Singleton
- 6. 無法實例化bean類
- 7. 請求作用域bean的實例化
- 8. bean實例化失敗;嵌套異常是org.springframework.beans.BeanInstantiationException:無法實例化Bean類
- 9. 實例化空csv變量
- 10. Java - 在給定示例中使用super()
- 11. bean的實例化失敗:指定的類是一個接口
- 12. 是否有可能使Spring僅在繼承中實例化父bean(而不是實例化子bean)?
- 13. JSF2 ApplicationScope bean實例化時間?
- 14. Spring 3 bean實例化序列
- 15. 未實例化會話範圍的bean
- 16. 如何用Jackson自定義方式實例化bean?
- 17. 在Bean中僅實例化一次值
- 18. Spring中的Bean實例化通知
- 19. JavaServer Faces Bean實例化順序
- 20. 如何實例化泛型spring bean?
- 21. 何時讓Spring實例化一個bean?
- 22. 無法實例化bean類:BeanInstantiationException
- 23. BeanInstantiationException:無法實例化bean類
- 24. Hibernate SessionFactory:無法實例化bean
- 25. 日誌彈簧bean實例化
- 26. 無法實例化bean類[org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]
- 27. 無法實例化bean類org.springframework.validation.BindingResult
- 28. 收集的Spring bean實例化異常
- 29. BeanInstantiationException:無法實例化bean類[org.springframework.security.config.SecurityNamespaceHandler]
- 30. 實例化@postconstruct內部的@autowired bean內部控制器,spring
我創建了一個[功能要求(http://sourceforge.net/p/supercsv/功能請求/ 28 /)爲此:) –