1

I am new to spring-batch, here i am getting some data from DB using following reader statements. Here i need to pass value dynamically(thru arguments).

<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 Code:

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

Can any body help on this?

1

1 Answer 1

4

As explained in §5.4 Late Binding of Job and Steps Attributes of official Spring Batch documentation, you need to add scope="step" to your step :

Using a scope of Step is required in order to use late binding since the bean cannot actually be instantiated until the Step starts, which allows the attributes to be found. Because it is not part of the Spring container by default, the scope must be added explicitly, either by using the batch namespace or by including a bean definition explicitly for the StepScope (but not both)

Giving this :

<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>
Sign up to request clarification or add additional context in comments.

1 Comment

@Thrax Is there any way that I can pass property file value ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.