4

I have a spring batch program which reads from one database and writes to a file.

It has item reader as below:

<beans:bean id="myItemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
    <beans:property name="dataSource" ref="jobRepository-dataSource" />
    <beans:property name="sql" value="${dbTofileDataReadSQL}"/>
    <beans:property name="rowMapper">
        <beans:bean class="com.mypackage.MyRowMapper" />
    </beans:property>
</beans:bean>

The sql is something like:

select one, two, three, four from myTable where business_date='12/12/11'

This line will go into my properties file:

dbTofileDataReadSQL = select one, two, three, four from myTable where business_date='12/12/11'

How to pass this business date at run time in the item reader so that it gets added in item reader.

Thanks for reading!!

2 Answers 2

3

you can use late-binding with step scope or a simple PropertyPlaceholderConfigurer

<bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="path to your properties file, either classpath: ... or c:/... " />
</bean>

and if you need multi-line entries in your properties file, go like this

# sql, multiline property with \
sql=\
SELECT \
ID, \
NAME \
FROM TEST \
ORDER BY ID

Or starting from Spring 3, you can use SpEL:

<util:properties id="myProperties" location="..." />
...
<beans:bean ...
   <beans:property name="sql" value="#{myProperties.sql}"/>
</beans:bean>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Michael.. I reached at this solution.. However, I had the updated date available in some other table. So used inner query. Can you please check my other question about skipping Item Writer in a step.
0

Have a placeholder in your property


dbTofileDataReadSQL = select one, two, three, four from myTable where business_date={0}

And check this answer that shows how to resolve it at runtime using MessageFormat class

Update:

With Spring, if you have that property file added to your resource bundle, you could retrieve the resolved value using ApplicationContext#getMessage


String getMessage(String code,
                  Object[] args,
                  Locale locale)
                  throws NoSuchMessageException

Comments

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.