Java Reference
In-Depth Information
class="org.springframework.batch.item.file.mapping.
BeanWrapperFieldSetMapper">
<beans:property name="prototypeBeanName" value="customer"/>
</beans:bean>
</beans:property>
</beans:bean>
</beans:property>
</beans:bean>
<beans:bean id="customer" class="com.apress.springbatch.chapter7.Customer"
scope="prototype"/>
Listing 7-2 begins with the customerFile, which is a reference to the file that will be read in by the
customerReader. Note that the actual name of the customer file will be passed in as a job parameter at
runtime.
From there you have your customerReader. The reader, as noted previously, consists of two pieces:
the file to be read in and a LineMapper instance. When you look at the LineMapper interface, as shown
in Listing 7-3, you can see that it's nearly identical to Spring's RowMapper.
Listing 7-3. The LineMapper Interface
package org.springframework.batch.item.file;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.LineTokenizer;
public interface LineMapper<T> {
T mapLine(String line, int lineNumber) throws Exception;
}
For each line in your file, Spring Batch will call the mapLine method of the LineMapper
implementation configured. In your case, that method will do two things; first, it will use the
org.springframework.batch.item.file.transform.FixedLengthTokenizer to divide the string up into a
FieldSet based upon the columns you configured. Then it will pass the FieldSet to the
org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper , which will use the names
of each field to map the fields to the bean you requested (Customer in this case).
When working with fixed-width files, you use the FixedLengthTokenizer to parse your records into
FieldSets. This implementation of the LineTokenizer interface takes three parameters:
columns (required) : The column number ranges that define each field.
names (optional) : A name to associate with each of the ranges specified in the list
of columns.
strict (optional) : A Boolean telling the reader if an exception should be thrown if a
line of an invalid length is passed in (fixed-width files are expected to have all
records be the same length).
With the LineTokenizer configured, you have a way to parse your line into a FieldSet. Now you need
to map the FieldSet into the fields of your domain object. In this case, you are going to use the
BeanWrapperFieldSetMapper. This implementation of the FieldSetMapper interface uses the bean spec
 
Search WWH ::




Custom Search