Java Reference
In-Depth Information
</beans:bean>
<beans:bean id="xmlOutputWriter"
class="org.springframework.batch.item.xml.StaxEventItemWriter">
<beans:property name="resource" ref="outputFile" />
<beans:property name="marshaller" ref="customerMarshaller" />
<beans:property name="rootTagName" value="customers" />
</beans:bean>
<beans:bean id="customerMarshaller"
class="org.springframework.oxm.xstream.XStreamMarshaller">
<beans:property name="aliases">
<util:map>
<beans:entry key="customer"
value="com.apress.springbatch.chapter9.Customer" />
</util:map>
</beans:property>
</beans:bean>
<step id="formatStep">
<tasklet>
<chunk reader="customerFileReader" writer="xmlOutputWriter"
commit-interval="10"/>
</tasklet>
</step>
<job id="formatJob">
<step id="step1" parent="formatStep"/>
</job>
</beans:beans>
Of the 69 lines of XML that it took to configure the original
formatJob
as shown in Listing 9-7, the
formatJob
in Listing 9-19 has changed only 14 lines (shortening the file overall by one line). The changes
begin with the definition of a new ItemWriter,
xmlOutputWriter
. This bean is a reference to the
StaxEventItemWriter
the section has been talking about and defines three dependencies: the resource to
write to, the
Marshaller
implementation, and the root tag name for each XML fragment the
Marshaller
will generate.
Just below
xmlOutputWriter
is
customerMarshaller
. This bean is used to generate an XML fragment
for each item the job processes. Using Spring's
org.springframework.oxm.xtream.XStreamMarshaller
class, the only further configuration you're required to provide is a
Map
of aliases to use for each type the
Marshaller comes across. By default, the Marshaller uses the attribute's name as the tag name, but you
provide an alias for the
Customer
class because the
XStreamMarshaller
uses the fully qualified name for
the class by default as the root tag of each fragment (
com.apress.springbatch.chatper8.Customer
instead
of just customer).
In order for the job to be able to compile and run, you need to make one more update. The POM file
needs a new dependency to handle the XML processing, a reference to Spring's Object/XML Mapping
(OXM) library. Listing 9-20 shows the update to the POM that is required.