I am doing a Spring Boot Project and using the OpenCSV library to parse some csvs into POJOs to be persisted to db.
OpenCSV uses the annotation @CsvCustomBindByName
to map a CSV field to a Java object.
The converter = DepartmentConverter.class
is a custom converter that is instantiated with:
Class<? extends AbstractBeanField<T,K>>.newInstance()
by the library, at runtime.
The problem is that because the custom field converter is instantiated reflectively by the OpenCSV library, it cant autowire beans because it is not registered in the Spring Context.
How can i make that dynamically instantiated converter be aware of the Spring context or the other way around. Some kind of interceptor? Thanks!
//Spring Managed class
public class Specialization {
@CsvCustomBindByName(required = true, converter = DepartmentConverter.class)
private Department department;
....
}
In my DepartmentConverter i need to use a Spring JPARepository to retrieve some data. DepartmentRepository can not be autowired.
@Component
public class DepartmentConverter extends AbstractBeanField<Department, String> {
@Autowired
private DepartmentRepository departmentRepository;
public DepartmentConverter() {
}
@Override protected Object convert(String val) throws CsvConstraintViolationException, ResourceNotFoundException {
//use departmentRepository
...
}
}