问题描述
在实现ClassifierCompositeItemwriter时遇到麻烦...
have trouble implementing a ClassifierCompositeItemwriter...
我正在读取基本的CSV文件,我想将其写入数据库.根据数据(名称+名称1)将其写入简单的ItemWriter或使用CompositeItemwriter(写入两个不同的表)...
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)...
这是我的: ClassifierCompositeItemwriter 参见>下面的错误消息
This is my : ClassifierCompositeItemwriter see > Error Message below
public ClassifierCompositeItemWriter<MyObject> classifierCompositeItemWriter() {
ClassifierCompositeItemWriter<MyObject> writer = new ClassifierCompositeItemWriter<MyObject>();
writer.setClassifier(new MySpecialClassifier());
return writer;
}
我尝试使用分类器接口
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一直告诉我:类型为ClassifierCompositeItemWriter的setClassifier(Classifier>)不适用于参数(MySpecialClassifier)
Eclipse keeps telling me :setClassifier(Classifier>) in the type ClassifierCompositeItemWriter is not applicable for the arguments (MySpecialClassifier)
推荐答案
您的分类器应如下所示:
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;
}
}
}
唯一的区别是implements Classifier<MyObject, ItemWriter<? super MyObject>>
而不是implements Classifier<MyObject, ItemWriter<MyObject>>
.
这是因为项目编写者不仅可以写入MyObject
项目,而且可以写入MyObject
的超类项目.
This is because an item writer can not only write MyObject
items but also items of a superclass of MyObject
.
这篇关于如何将Classifier与ClassifierCompositeItemWriter结合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!