Search code examples
springspring-batchspring-java-configlate-binding

Spring Batch Late Binding Within SQL Using Java Config


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?


Solution

  • 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