How to use Classifier with ClassifierCompositeItemWriter?

我只是一个虾纸丫 提交于 2020-01-10 06:08:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!