【问题标题】:Spring Batch SQL Command with JobParameters带有 JobParameters 的 Spring Batch SQL 命令
【发布时间】:2016-04-26 06:09:36
【问题描述】:

我是 spring-batch 的新手,在这里我使用以下阅读器语句从 DB 获取一些数据。这里我需要动态传递值(通过参数)。

<bean id="ItemReader"
            class="org.springframework.batch.item.database.JdbcCursorItemReader">
            <property name="dataSource" ref="dataSource" />
            <property name="sql">
                <value>
                <![CDATA[
    select * from table where section = #{jobParameters['section']}
    ]]>
                </value>
            </property>
            <property name="rowMapper">
                <bean class="xyzRowMapper" />
            </property>
        </bean>

JUnit 代码:

JobParameters jobParameters = = new JobParametersBuilder()
                    .addString("section", section);

任何机构都可以对此提供帮助吗?

【问题讨论】:

标签: spring-batch


【解决方案1】:

正如 Spring Batch 官方文档的§5.4 Late Binding of Job and Steps Attributes 中所述,您需要将scope="step" 添加到您的步骤中:

为了使用后期绑定,需要使用 Step 的范围,因为 在 Step 开始之前,无法实际实例化 bean,这 允许找到属性。因为它不属于 Spring容器默认情况下,必须显式添加范围, 通过使用批处理命名空间或通过包含 bean 定义 明确用于 StepScope(但不是两者)

给这个:

<bean id="ItemReader" scope="step" class="org.springframework.batch.item.database.JdbcCursorItemReader">
    <property name="dataSource" ref="dataSource" />
    <property name="sql">
        <value>
            <![CDATA[
                select * from table where section = #{jobParameters['section']}
            ]]>
        </value>
    </property>
    <property name="rowMapper">
        <bean class="xyzRowMapper" />
    </property>
</bean>

【讨论】:

  • @Thrax 有什么方法可以传递属性文件值吗?
猜你喜欢
  • 1970-01-01
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
  • 1970-01-01
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
相关资源
最近更新 更多