Java Reference
In-Depth Information
Listing 9-6. Output Configuration for Format Job
<beans:bean id="outputFile"
class="" scope="step">
<beans:constructor-arg value="#{jobParameters[outputFile]}"/>
<beans:bean id="flatFileOutputWriter"
<beans:property name="resource" ref="outputFile"/>
<beans:property name="lineAggregator" ref="formattedLineAggregator"/>
<beans:bean id="formattedLineAggregator"
<beans:property name="fieldExtractor">
<beans:bean class="org.springframework.batch.item.file.transform.
<beans:property name="names"
<beans:property name="format" value="%s %s lives at %s %s in %s, %s."/>
As Listing 9-6 shows, the configuration for the output side of this step is actually smaller than the
input. You begin by configuring of the output file; again, the name of the file is passed in as a job
parameter. Next you have the configuration of the FlatFileItemWriter . flatFileOutputWriter takes two
dependencies: a resource (the file to write to) and the LineAggregator implementation. The last piece of
the output puzzle is the LineAggregator implementation: FormatterLineAggregator in this case. It takes
two dependencies: a FieldExtractor implementation and a format.
The org.springframework.batch.item.file.transform.FieldExtractor interface is intended to
abstract the process of taking the fields of an object tree and convert them into an Object array. With the
objects to be written into an array, the FormatterLineAggregator uses Java's String.format() method in
conjunction with the string provided in the format dependency to generate the formatted String to be
written to the file. In this case, BeanWrapperFieldExtractor uses the getters for each of the properties
defined and returns the results, in order, in an Object array to be formatted according to the format
string. In the case of Listing 9-6, you're extracting the firstName , lastName , address , city , state , and zip
from each item. It's important to note that there is no key/value pairing during the formatting process. If
you want a bean property to appear twice in the formatted String , you need to include it twice, in order,
in the names list.
With all of the input and output configured, all you need to do to complete the job is configure the
step and job. Listing 9-7 shows the complete configuration of formatJob including the previous input
and output.
Search WWH ::

Custom Search