Listing 9-70. XML Configuration for CustomerXmlHeaderCallback
<beans:property name="marshaller" ref="customerMarshaller" />
<beans:property name="rootTagName" value="customers" />
<beans:property name="headerCallback" ref="customerHeaderCallback"/>
When you execute the multiresource job from the previous example using the header configuration
in Listing 9-70, each of the output files begins with the XML fragment as shown in Listing 9-71.
Listing 9-71. XML Header
<?xml version="1.0" encoding="UTF-8"?>
<author name="Michael Minella"/>
As you can see, adding an XML fragment at either the start or end of an XML file is quite easy.
Implement the StaxWriterCallback interface and configure the ItemWriter to call it as either the header
or the footer, and you're done!
Header and Footer Records in a Flat File
Next you can look at adding headers and footers to a flat file. Unlike the XML header and footer
generation that use the same interface for either, writing a header in a flat file requires the
implementation of a different interface than that of a footer. For the header, you implement the
org.springframework.batch.item.file.FlatFileHeaderCallback interface; and for the footer, you
implement the org.springframework.batch.item.file.FlatFileFooterCallback interface. Both consist
of a single method: writeHeader and writeFooter , respectively. Let's look at how to write a footer that
writes the number of records you've processed in the current file.
For this example, you use the MultiResourceItemWriter to write files with 10 formatted records in
each record plus a single footer record that states how many records were written in each file. To be able
to keep count of the number of items you've written into a file, you need to decorate the writers with the
appropriate functionality. Figure 9-10 shows the layering of the ItemWriters required for the example.