首页 > 解决方案 > CompositeItemWriter 和 Wrapper

问题描述

  1. reader -> 注入一个名为 Person 的类
  2. processor -> injest Person,并返回一个包装对象,它有 3 个字段,它们都是 Person 类型(1. inputPerson,2. outputPerson,3. output)
  3. writer -> injest 这个包装器,应该将前两个字段写入一个文件中,第三个字段作为 xml 写入第二个文件中。

那就是我为这个问题编写的代码:

  @Bean
  public CompositeItemWriter<Wrapper> compositeItemWriter(){
    CompositeItemWriter writer = new CompositeItemWriter();
    writer.setDelegates(Arrays.asList(firstTwoWriters(), thirdWriter));
    return writer;
  }

@Bean
public StaxEventItemWriter<Wrapper> firstTwoWriters() {
     StaxEventItemWriter<Wrapper> xmlFileWriter = new StaxEventItemWriter<>();
    xmlFileWriter.setRootTagName("something");
    String outputName = applicationArguments.getOptionValues("output").get(0);
    FileSystemResource outputResource = new FileSystemResource(outputName);
    xmlFileWriter.setResource(outputResource);
    Jaxb2Marshaller personMarshaller = new Jaxb2Marshaller();
    scoringMapMarshaller.setClassesToBeBound(Person.class);
    xmlFileWriter.setMarshaller(personMarshaller );
}

问题是,我无法选择该作者应该使用哪个字段(inputPerson、outputPerson 或 output)(哪个字段应该转换为 xml)。任何想法,我该怎么做?(如果可能的话,举个例子)

标签: springspring-batch

解决方案


推荐阅读