Spring batch 2.2 JavaConfig

試著忘記壹切 提交于 2019-12-11 02:36:15

问题


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, e.g.:

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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/17090739/spring-batch-2-2-javaconfig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!