我一直在学习Spring Batch框架,试图通过在线文档以及Appress的Pro Spring Batch书籍将其付诸实践。我有一个快速的问题。
场景
我想做一个简单的测试,从数据库中读取数据,进行一些处理,然后写入另一个数据库。
问题
据我所知,有一个名为launch context.xml的配置文件,其中包含作业存储库数据库模式,用于维护作业的状态以及每个作业的每个步骤。
假设我有一个从中读取的源数据库(a)和一个向中写入的目标数据库(B)。
也许我忽略了它,但是。。。
-
我将A和B的数据源信息放在哪里?
-
我想这取决于#1的答案,但如果将其放在src/main/resources下,例如source-datasource.xml和target-datasource.xmlSpring将如何将其收集起来并进行适当的连接?在Spring web应用程序开发中,我通常将这些类型的文件放在上下文参数标记下。
您可以在您选择的任何spring文件中定义这些数据源,所以是的:
src/main/resources/db/source-datasource.xml
src/main/resources/db/target-datasource.xml
会的。
假设您将数据源bean命名为sourceDataSource
和targetDataSource
。告诉SpringBatch(或者在本例中只是Spring)使用它们的方法是通过"导入"one_answers"依赖项注入"。
正在导入
您可以按照最适合的方式组织您的spring配置,但由于您已经有了launch-context.xml
,为了使上述数据源可见,您需要将它们导入launch-context.xml
中作为:
<import resource="classpath:db/source-datasource.xml"/>
<import resource="classpath:db/target-datasource.xml"/>
注入/使用
<bean id="sourceReader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
<property name="dataSource" ref="sourceDataSource" />
<!-- other properties here -->
</beans:bean>
<bean id="targetWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="targetDataSource" />
<!-- other properties here -->
</beans:bean>
其中sourceReader
和targetWriter
是要注入到步骤中的bean。