Spring Batch CompositeItemWriter如何正确设置writer委托

时间:2017-07-27 18:04:04

标签: java spring-batch

您好我正在尝试将两个编写器添加到spring batch compositeitemwriter中。我收到一个错误说:

The method setDelegates(List<ItemWriter<? super MyObject>>) in the type CompositeItemWriter<MyObject> is not applicable for the arguments (List<ItemWriter<MyObject>>)

我的代码

public ItemWriter<MyObject> myWriter(){
    ItemWriter<MyObject> myWriter = new myWriter();
    return myWriter;
}

public ItemWriter<MyObject> myOtherWriter(){
    ItemWriter<MyObject> myOtherWriter = new MyOtherWriter();
    return myOtherWriter;
}

public CompositeItemWriter<MyObject> compositeItemWriter(){
    CompositeItemWriter<MyObject> writer = new CompositeItemWriter<MyObject>();
    List<ItemWriter<MyObject>> writerList = new ArrayList<ItemWriter<MyObject>>();
    writerList.add(myWriter());
    writerList.add(myOtherWriter());
    writer.setDelegates(writerList); //<-- ERROR HERE
    return writer;
}

我找不到任何没有使用xml的复合编写器的强代码示例。我知道我的错误与泛型类型参数有关,我几乎不知道。任何人都可以解释为什么上面的代码不起作用,可能如何解决它? Multiple itemwriters in Spring batch这是我能找到的最接近的,但它使用的泛型类型也会给我一个错误。

感谢您的帮助!我是春季批次的新手。

2 个答案:

答案 0 :(得分:0)

尝试修改......

List<ItemWriter<MyObject>> writerList = new ArrayList<ItemWriter<MyObject>>();

... with:

List<ItemWriter<? super MyObject>> writerList = new ArrayList<ItemWriter<? super MyObject>>();

CompositeItemWriter#setDelegates会以List<ItemWriter<? super T>>形式列出一个列表。

请参阅https://discuss.appium.io/t/how-to-click-enter-after-entering-some-text/3136/4

答案 1 :(得分:0)

以防万一您找不到合适的解决方案。 我会这样:

public ItemWriter<MyObject> myWriter() {
    ItemWriter<MyObject> myWriter = new JdbcBatchItemWriter<MyObject>(); //  <-- Example item writer 1
    return myWriter;
}

public ItemWriter<MyObject> myOtherWriter() {
    ItemWriter<MyObject> myOtherWriter = new JdbcBatchItemWriter<MyObject>(); //  <-- Example item writer 2
    return myOtherWriter;
}

public CompositeItemWriter<MyObject> compositeItemWriter() {
    CompositeItemWriter<MyObject> writer = new CompositeItemWriter<MyObject>();
    writer.setDelegates(Arrays.asList(myWriter(),myOtherWriter())); //<-- NO ERROR HERE :)
    return writer;
}

我希望有帮助。