问题
have trouble implementing a ClassifierCompositeItemwriter...
I am reading a basic CSV File and i want to write them do a database. Depending on the data (Name + Name1) either write it to a simple ItemWriter or use a compositeItemwriter (that writes to two different Tables)...
This is my : ClassifierCompositeItemwriter see > Error Message below
public ClassifierCompositeItemWriter<MyObject> classifierCompositeItemWriter() {
ClassifierCompositeItemWriter<MyObject> writer = new ClassifierCompositeItemWriter<MyObject>();
writer.setClassifier(new MySpecialClassifier());
return writer;
}
I tried to implement a Classifier using the Classifier Interface
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.classify.Classifier;
@SuppressWarnings("serial")
public class MySpecialClassifier implements Classifier<MyObject, ItemWriter<MyObject>> {
@Autowired
ItemWriter<MyObject> writer1;
@Autowired
ItemWriter<MyObject> writer2;
@Override
public ItemWriter<MyObject> classify(MyObject obj) {
if (!obj.getName().isEmpty() && !obj.getName1().isEmpty()) {
return writer1;
} else {
return writer2;
}
}
}
Eclipse keeps telling me : setClassifier(Classifier>) in the type ClassifierCompositeItemWriter is not applicable for the arguments (MySpecialClassifier)
回答1:
You classifier should be like this:
public class MySpecialClassifier implements Classifier<MyObject, ItemWriter<? super MyObject>> {
@Autowired
ItemWriter<MyObject> writer1;
@Autowired
ItemWriter<MyObject> writer2;
@Override
public ItemWriter<MyObject> classify(MyObject obj) {
if (!obj.getName().isEmpty() && !obj.getName1().isEmpty()) {
return writer1;
} else {
return writer2;
}
}
}
The only difference is implements Classifier<MyObject, ItemWriter<? super MyObject>>
instead of implements Classifier<MyObject, ItemWriter<MyObject>>
.
This is because an item writer can not only write MyObject
items but also items of a superclass of MyObject
.
来源:https://stackoverflow.com/questions/53501152/how-to-use-classifier-with-classifiercompositeitemwriter