Figure 9-10. Using multiple layers of ItemWriters to accomplish writing the footers
As Figure 9-10 shows, the writer that does the heavy lifting in this example is a regular
FlatFileItemWriter . But you decorate that ItemWriter with your own implementation that keeps track
of the number of items that have been written and implement the FlatFileFooterCallback interface.
Finally you use MultiResourceItemWriter to create the resources as needed.
Because the only code you need to write for this example is the CustomerWriterWithFooter class, you
can start there. This class implements two interfaces: FlatFileFooterCallback as well as
org.springframework.batch.item.file.ResourceAwareItemWriterItemStream . You implement
FlatFileFooterCallback so you can use the data compiled while you write the items to write the footer.
The ResourceAwareItemWriterItemStream interface is implemented so MultiResourceItemWriter can
inject the resource into the ItemWriter as they're created. If you didn't use this interface, you would be
required to inject a single resource at startup, which is not what you require. For all practical purposes,
the methods required by the ResourceAwareItemWriterItemStream interface are just passthroughs to the
FlatFileItemWriter it wraps. Listing 9-72 shows the implementation of the CustomerWriterWithFooter