3

I am using Spring Batch 2 version. I am reading data from database using JdbcCursorItemReader.I have successfully fetched the data and also written it to a file.

Below is itemReader bean defined in Job.xml File::

<bean id="itemReader"
        class="org.springframework.batch.item.database.JdbcCursorItemReader"
        scope="step">
        <property name="dataSource" ref="dataSource" />
        <property name="sql"
        value="select u.ID, u.USER_LOGIN, u.PASSWORD, u.AGE from USERS u" />
        </property>
        <property name="rowMapper">
           <bean class="com.example.UserRowMapper" />
        </property>
    </bean>

But the issue is,my query is quite big so I just want to keep that query out of xml file and get that query from other file or property file(.property,yaml or xml).

So that I can write xml code as below::

<bean id="itemReader"               class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step">
                <property name="dataSource" ref="dataSource" />
                <property name="sql" value="$sql_query" />
                </property><property name="rowMapper">
                <bean class="com.example.UserRowMapper" />
                </property>
</bean>

What is best way to achieve this?

Mugdha
  • 31
  • 2

2 Answers2

2
<bean id="myProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">     
        <property name="locations">
            <list>
                <value>path1.properties</value>
                <value>path2.properties</value>
                .....
            </list>
        </property>     
        <property name="ignoreUnresolvablePlaceholders" value="false"/>
</bean> 

...

<bean id="itemReader"  class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step">
                <property name="dataSource" ref="dataSource" />
                <property name="sql" value="${sql_query}" />
                </property><property name="rowMapper">
                <bean class="com.example.UserRowMapper" />
                </property>
</bean> 


path1.properties:
sql_query=value

PropertySourcesPlaceholderConfigurer is preffered in 3.1 and higher, instead of PropertyPlaceholderConfigurer

Dreamer
  • 76
  • 4
0

You can add sql in jobexecutioncontext by using job listener before step.

Raje
  • 3,285
  • 15
  • 50
  • 70