如何在 Spring Boot 2 中为 spring 批处理配置数据源以用于测试目的

How to configure a datasource for a spring batch in Spring Boot 2 for testing purpose

我正在玩简单的批处理,尽管有 H2 依赖性,但我的 DataSource 配置有问题。

控制台输出:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

我的 类 对字符串进行操作并使用:

org.springframework.batch.item.ItemProcessor;
org.springframework.batch.item.ItemReader;
org.springframework.batch.item.ItemWriter;

主要

@SpringBootApplication   
public class Boo2BatchApplication {

    public static void main(String[] args) {
        SpringApplication.run(Boo2BatchApplication.class, args);
    }
}

配置:

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBatchProcessing
public class BatchConfig {

    @Bean
    public Step recordsStep(StepBuilderFactory stepBuilderFactory, RecordReader recordReader,
            RecordProcessor<String> recordProcessor, RecordWriter recordWriter) {

        return stepBuilderFactory.get("recordsSetp").<String, String>chunk(4).reader(recordReader)
                .processor(recordProcessor).writer(recordWriter).build();
    }

    @Bean
    Job recordsJob(JobBuilderFactory jobBuilderFactory, Step recordsStep) {

        return jobBuilderFactory.get("recordsJob").start(recordsStep).build();
    }

}

当像 H2 这样的数据库在路径上时,默认配置数据源(如@Alireza Khajavi 所说)。无需额外配置。我的类路径搞砸了,启动时 pom 文件的依赖项不可用。

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>