随着项目的规模的不断扩大,Spring Batch的配置文件,也是一个头疼的问题,本人试了很多次,建议的配置方式如下:
基础配置放在一个文件中applicationContext-batch.xml:
<beans:beans xmlns="http://www.springframework.org/schema/batch"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:bean="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-2.1.xsd">
<bean:bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<bean:property name="transactionManager" ref="transactionManager" />
</bean:bean>
<bean:bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">
</bean:bean>
<bean:bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<bean:property name="jobRepository" ref="jobRepository" />
<bean:property name="taskExecutor">
<bean:bean class="org.springframework.core.task.SimpleAsyncTaskExecutor"></bean:bean>
</bean:property>
</bean:bean>
</beans:beans>
一个业务场景对应一个配置文件applicationContext-batch-mysteps.xml:
<beans:beans xmlns="http://www.springframework.org/schema/batch"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:bean="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-2.1.xsd">
<flow id="mysteps_flow">
<step id="mysteps_flow_first" parent="mysteps_first" next="mysteps_flow_second"></step>
<step id="mysteps_flow_second" parent="mysteps_second" next="mysteps_flow_third"></step>
<step id="mysteps_flow_third" parent="mysteps_third"></step>
</flow>
<step id="mysteps_first">
<tasklet ref="first"></tasklet>
</step>
<step id="mysteps_second">
<tasklet ref="second"></tasklet>
</step>
<step id="mysteps_third">
<tasklet ref="third"></tasklet>
</step>
<bean:bean id="first" class="com.test.tasklet.MyFirstTasklet" scope="step">
</bean:bean>
<bean:bean id="second" class="com.test.tasklet.MySecondTasklet" scope="step">
</bean:bean>
<bean:bean id="third" class="com.test.tasklet.MyThirdTasklet" scope="step">
</bean:bean>
</beans:beans>
业务场景的配置&#x