简体   繁体   中英

Spring batch 2.2 JavaConfig

I'm trying to get Spring Batch 2.2 working with JavaConfig.

Nowadays they have a @EnableBatchProcessing annotation that sets up a lot of things. Default that annotation uses a datasource for its job data, but we don't want to save this data and don't want to create the table for it. The documentation says something about customizing but I have not been able to get it working:

  • The user has to provide a DataSource as a bean in the context, or else implement BatchConfigurer in the configuration class itself, eg:

public class AppConfig extends DefaultBatchConfigurer {

In our older version we've been able to use MapJobRepositoryFactoryBean class so it keeps all its data in memory. Is there anyway to use the full JavaConfig way and not define a DataSource ? I've not been able to get it working.

Even if I define two data sources (one HSQL in-memory that never gets used) and our real Oracle datasource it does not work because it finds two data sources instead of one.

Anyone have an idea how to get this working? Or is the only solution going back to configuring this in the XML way?

Assuming that no other artifacts require a DataSource, you can use java config to create a context without a DataSource. To do that, your configuration will need to extend DefaultBatchConfigurer as you point out. In there, you'll override two methods, createJobRepository() and setDataSource(). Below is an example context (it doesn't define a job or steps, but it bootstraps all the related beans correctly).

@Configuration
@EnableBatchProcessing
public static class BatchConfiguration extends DefaultBatchConfigurer {

    @Override
    protected JobRepository createJobRepository() throws Exception {
        MapJobRepositoryFactoryBean factory = 
            new MapJobRepositoryFactoryBean();
        factory.afterPropertiesSet();
        return  (JobRepository) factory.getObject();
    }

    @Override
    @Autowired
    public void setDataSource(DataSource dataSource) {
        if(dataSource != null) {
            super.setDataSource(dataSource);
        }
    }

    @Bean
    public DataSource dataSource() {
        return null;
    }
}

I do think that simplifying this would be a useful feature and have added it to Jira. You can track it's progress here: https://jira.springsource.org/browse/BATCH-2048

Just define a dataSource() method in your BatchConfig Class Here is how

@Bean
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(driverClassName);
    dataSource.setUrl(driverUrl);
    dataSource.setUsername(driverUsername);
    dataSource.setPassword(driverPassword);
    return dataSource;
}

This will automatically be invoked while setting up the TransactionManager

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM