0

We are converting xml-based spring batch configuration to java config.
In xml form of JdbcCursorItemReader we were using late binding:

SELECT * FROM MY_TABLE_#{jobParameters[param1]}

How can this be implemented using Java config syntax?

leon.shalit
  • 3
  • 1
  • 2

1 Answers1

0

You can achieve this as follows:

@Bean
@StepScope
public JdbcCursorItemReader jdbcCursorItemReader(@Value("#{jobParameters['param1']}") String param1) {
    return new JdbcCursorItemReaderBuilder<>()
            .sql("SELECT * FROM MY_TABLE_" + param1)
            // set other properties
            .build();
}

The reference documentation contains a toggle on each page that allows you to see examples in either Java and Xml configuration. This can be helpful in your migration. See example here: https://docs.spring.io/spring-batch/4.0.x/reference/html/readersAndWriters.html#readersAndWriters

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • Thank you! This will work, but the concatenation can become messy really quick in real-life size queries. Is there a way to use late binding like in 'old' style? It looks cleaner and more readable to me. – leon.shalit Jul 17 '18 at 16:45
  • Well, the same happens in XML config with real-life sized queries, the SpEL expression will become messy too. The example answers the question of how to use late binding to get a job parameter, now it's up to you to use a `StringBuilder` or load the query from a properties files or use any other method you think is cleaner. – Mahmoud Ben Hassine Jul 17 '18 at 16:54