Java Reference
In-Depth Information
to map fields in the FieldSet to fields in the domain object by name (the Customer object will have a
getFirstName() and a setFirstName(String name) , etc). The only thing you need to supply for the
BeanWrapperFieldSetMapper is a reference to the bean it will be using, in your case it is the reference to
the customer bean.
Note The FixedLengthTokenizer doesn't trim any leading or trailing characters (spaces, zeros, etc) within each
field. To do this, you'll have to implement your own LineTokenizer or you can trim in your own FieldSetMapper.
To put your reader to use, you need to configure your step and job. You will also need to configure a
writer so that you can see that everything works. You will be covering writers in depth in the next chapter
so you can keep the writer for this example simple. Listing 7-4 shows how to configure a simple writer to
output the domain objects to a file.
Listing 7-4. A Simple Writer
<beans:bean id="outputFile"
class="org.springframework.core.io.FileSystemResyource" scope="step">
<beans:constructor-arg value="#{jobParameters[outputFile]}"/>
</beans:bean>
<beans:bean id="outputWriter"
class="org.springframework.batch.item.file.FlatFileItemWriter">
<beans:property name="resource" ref="outputFile" />
<beans:property name="lineAggregator">
<beans:bean class="org.springframework.batch.item.file.transform.
FormatterLineAggregator">
<beans:property name="fieldExtractor">
<beans:bean class="org.springframework.batch.item.file.transform.
BeanWrapperFieldExtractor">
<beans:property name="names" value="firstName,middleInitial,
lastName,addressNumber,street,city,state,zip" />
</beans:bean>
</beans:property>
<beans:property name="format" value=" %s %s. %s, %s %s, %s %s %s" />
</beans:bean>
</beans:property>
</beans:bean>
Looking at the output file resource and the writer in Listing 7-4, you can see a pattern between the
readers and writers. The writer has two dependencies: the file resource to write to and a lineAggregator.
The lineAggregator is used to take an object and convert it to the string that will be written to the file.
Your job configuration is very simple. As shown in Listing 7-5, a simple step that consists of the
reader and writer with a commit count of 10 records is all you need. Your job uses that single step.
 
Search WWH ::




Custom Search