使项目读取器返回列表而不是单个对象-Spring Batch

use*_*259 4 spring-batch

问题是:如何在春季批处理中使项目阅读器交付列表而不是单个对象。

我搜索了一下,一些答案是修改项目阅读器以返回对象列表,并更改项目处理器以接受列表作为输入。

如何做/编码物品阅读器?

Mic*_*low 5

看看itemReader官方Spring Batch文档

public interface ItemReader<T> {

    T read() throws Exception, UnexpectedInputException, ParseException;

}
// so it is as easy as
public class ReturnsListReader implements ItemReader<List<?>> {
   public List<?> read() throws Exception {
      // ... reader logic
   }
}
Run Code Online (Sandbox Code Playgroud)

处理器的工作原理相同

public class FooProcessor implements ItemProcessor<List<?>, List<?>> {

    @Override
    public List<?> process(List<?> item) throws Exception {
        // ... logic
    }

}
Run Code Online (Sandbox Code Playgroud)

代替返回列表,处理器可以返回任何内容,例如字符串

public class FooProcessor implements ItemProcessor<List<?>, String> {

    @Override
    public String process(List<?> item) throws Exception {
        // ... logic
    }

}
Run Code Online (Sandbox Code Playgroud)