【问题标题】:Where does business processing datasource config go in Spring Batch?Spring Batch 中的业务处理数据源配置在哪里?
【发布时间】:2012-01-05 21:12:42
【问题描述】:

我一直在学习 Spring Batch 框架,试图通过在线文档以及 Appress 的 Pro Spring Batch 书将其付诸实践。我有一个简单的问题。

场景

我想做一个简单的测试,从数据库中读取数据,进行一些处理,然后写入另一个数据库。

问题

我了解有一个名为 launch-context.xml 的配置文件,其中包含作业存储库数据库架构,用于维护作业的状态以及每个作业的每个步骤。

假设我有一个源数据库 (A) 用于读取数据,而目标数据库 (B) 用于写入数据。

也许我忽略了它,但是...

  1. A 和 B 的数据源信息放在哪里?

  2. 我想这取决于 #1 的答案,但如果将其放在 src/main/resources 下,例如 source-datasource.xml 和 target-datasource.xml Spring 将如何拾取并正确连接它?在 Spring Web 应用程序开发中,我通常将这些类型的文件放在 context-param 标记下。

【问题讨论】:

    标签: spring spring-batch spring-3


    【解决方案1】:

    您可以在您选择的任何 spring 文件中定义这些数据源,所以是的:

    • src/main/resources/db/source-datasource.xml
    • src/main/resources/db/target-datasource.xml

    会的。

    假设您将数据源 bean 命名为 sourceDataSourcetargetDataSource。您告诉 Spring Batch(或在本例中为 Spring )使用它们的方式是通过“导入”和“依赖注入”。

    导入

    您可以以最适合的方式组织您的 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>
    

    sourceReadertargetWriter 是您将注入到步骤中的 bean。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-27
      • 2022-12-04
      • 1970-01-01
      • 2017-10-14
      • 2018-12-16
      • 2016-06-16
      • 2013-08-17
      • 2012-05-27
      相关资源
      最近更新 更多