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 /)为此:) –