首页 > 解决方案 > org.springframework.boot.autoconfigure.batch.BatchConfigurerConfiguration$JdbcBatchConfiguration 中方法batchConfigurer的参数1

问题描述

我正在使用 Spring Batch 并使用 @Scheduler 注释以某种频率安排作业。

从错误消息看来,Spring Boot 至少需要与 Spring DataSource 相关的条目,但我不需要它,因为我现在还没有处理数据库。

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of method batchConfigurer in org.springframework.boot.autoconfigure.batch.BatchConfigurerConfiguration$JdbcBatchConfiguration required a bean of type 'javax.sql.DataSource' that could not be found.
    - Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
    - Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'javax.transaction.TransactionManager'


Action:

Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.

应用程序属性

cron.job.expression=*/1 * * * *

其他类:

@Configuration
@EnableBatchProcessing
@Primary
public class ScheduledDomainJob {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job scheduledJob() {
        return jobBuilderFactory.get("scheduledJob").flow(step1()).end().build();
    }

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1").<Domain, Domain>chunk(10)
                .reader(reader()).writer(writer()).build();
    }

    @Bean
    public FlatFileItemReader<Domain> reader() {
        FlatFileItemReader<Domain> reader = new FlatFileItemReader<>();
        reader.setResource(new ClassPathResource("csv/domain-1-03-2017.csv"));
        reader.setLineMapper(new DefaultLineMapper<Domain>() {{
            setLineTokenizer(new DelimitedLineTokenizer() {{
                setNames(new String[]{"id", "domain"});
            }});
            setFieldSetMapper(new BeanWrapperFieldSetMapper<Domain>() {{
                setTargetType(Domain.class);
            }});
        }});
        return reader;
    }

    @Bean
    public CustomWriter writer() {
        CustomWriter writer = new CustomWriter();
        return writer;
    }

    @Bean
    public RunScheduler scheduler() {
        RunScheduler scheduler = new RunScheduler();
        return scheduler;
    }
}

CustomerWriter.java

@Slf4j
public class CustomWriter implements ItemWriter<Domain> {

    @Override
    public void write(List<? extends Domain> items) throws Exception {
        log.info("writer ....... " + items.size());
        for (Domain domain : items) {
            log.info(domain + "\n");
        }
    }
}

标签: springspring-bootspring-batch

解决方案


我希望您使用的是 Spring Boot 版本 2。您可以对代码进行一些更改,如下所示。

@EnableBatchProcessing
@Primary
public class ScheduledDomainJob extends DefaultBatchConfigurer

在您的 spring-boot 应用程序类上定义以下注释。

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

您可以使用 Spring boot 找到有关Spring Batch Hello world 示例的更多详细信息


推荐阅读