Spring-batch中的通用项目阅读器和项目编写器

时间:2013-04-17 07:29:29

标签: spring-batch

如何编写通用项目阅读器和项目编写器,以便可以为作业中的所有步骤重复使用相同的读者和编写者。请帮忙吗?

提前感谢。

1 个答案:

答案 0 :(得分:0)

Spring批量为ItemReaderItemProcessorItemWriter适配器“开箱即用”,可以帮助实现许多通用功能。基本上ItemReaderAdapter允许您调用现有的对象和方法,而无需编写自己的阅读器。

这是一个配置示例

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:batch="http://www.springframework.org/schema/batch"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">


    <batch:job id="sampleJob">
        <batch:step id="sampleJob.step1">
            <batch:tasklet>
                <batch:chunk reader="readerAdapter" writer="..." commit-interval="10"/>
            </batch:tasklet>
        </batch:step>
    </batch:job>

    <bean id="readerAdapter" class="org.springframework.batch.item.adapter.ItemReaderAdapter" scope="step">
        <property name="targetObject" ref="myService"/>
        <property name="targetMethod" value="get"/>
        <property name="arguments" value="#{jobParameters['parameterName']}"/>
    </bean>

    <bean id="myService"/>

</beans>

对于块中的每个“阶段”都有适配器。看看每个适配器上的javadoc,如果你的服务返回正确类型的对象参与块,它们将帮助获得更好的图片。

相关问题