Java Reference
In-Depth Information
For the LineMapper implementation, you are going to use Spring Batch's
org.springframework.batch.item.file.DefaultLineMapper
. This LineMapper implementation is
intended for the two-step process of mapping lines to domain objects you talked about previously:
parsing the line into a FieldSet and then mapping the fields of the FieldSet to a domain object, the
Customer
object in your case.
To support the two step mapping process, the DefaultLineMapper takes two dependencies: a
LineTokenizer implementation which will parse the String that is read in from your file into a FieldSet
and a FieldSetMapper implementation to map the fields in your FieldSet to the fields in your domain
object. Listing 7-2 shows the customerFile and customerReader bean definitions.
Listing 7-2.
customerFile and customerReader in
copyJob.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="
http://www.springframework.org/schema/batch"
xmlns:beans="
http://www.springframework.org/schema/beans"
xmlns:util="
http://www.springframework.org/schema/beans"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
<beans:import resyource="../launch-context.xml"/>
<beans:bean id="customerFile"
class="org.springframework.core.io.FileSystemResyource" scope="step">
<beans:constructor-arg value="#{jobParameters[customerFile]}"/>
</beans:bean>
<beans:bean id="customerReader"
class="org.springframework.batch.item.file.FlatFileItemReader">
<beans:property name="resyource" ref="customerFile" />
<beans:property name="lineMapper">
<beans:bean
class="org.springframework.batch.item.file.mapping.
DefaultLineMapper">
<beans:property name="lineTokenizer">
<beans:bean
class="org.springframework.batch.item.file.transform.
FixedLengthTokenizer">
<beans:property name="names"
value="firstName,middleInitial,lastName,addressNumber,street,
city,state,zip"/>
<beans:property name="columns"
value="1-10,11,12-21,22-27,28-47,48-56,57-58,59-63"/>
</beans:bean>
</beans:property>
<beans:property name="fieldSetMapper">
<beans:bean