csv 考虑在配置中定义一个类型为'org.springframework.batch.core.configuration.annotation.JobBuilderFactory'的bean

niknxzdl  于 2023-05-20  发布在  Spring
关注(0)|答案(1)|浏览(142)

应用程序启动失败
com.example.persistence.batch.BatchConfiguration中构造函数的参数0需要类型为“org.springframework.batch.core.configuration.annotation.JobBuilderFactory”的Bean,但找不到该Bean。
我试图提取csv数据并使用spring批处理填充mysql数据库。我正面临着这个错误

package com.example.persistence.batch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import com.example.persistence.entities.CsvModel;

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(BatchConfiguration.class);

    private final JobBuilderFactory jobBuilderFactory;
    private final StepBuilderFactory stepBuilderFactory;

    @Autowired
    public BatchConfiguration(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
        this.jobBuilderFactory = jobBuilderFactory;
        this.stepBuilderFactory = stepBuilderFactory;
    }

    @Bean
    public ItemReader<CsvModel> reader() {
        return new FlatFileItemReaderBuilder<CsvModel>()
                .name("csvModelItemReader")
                .resource(new ClassPathResource("SwiftMessage.csv"))
                .delimited()
                .names("block", "tag", "value")
                .linesToSkip(1)
                .fieldSetMapper(new BeanWrapperFieldSetMapper<>() {{
                    setTargetType(CsvModel.class);
                }})
                .build();
    }

    @Bean
    public ItemProcessor<CsvModel, CsvModel> processor() {
        return item -> item; // Perform any necessary processing here
    }

    @Bean
    public ItemWriter<CsvModel> writer() {
        return items -> {
            for (CsvModel item : items) {
                // Write item to the destination
                logger.info("Writing item: {}", item);
            }
        };
    }

    @Bean
    public Step step(ItemReader<CsvModel> reader, ItemProcessor<CsvModel, CsvModel> processor,
                     ItemWriter<CsvModel> writer) {
        return stepBuilderFactory.get("myStep")
                .<CsvModel, CsvModel>chunk(10)
                .reader(reader)
                .processor(processor)
                .writer(writer)
                .build();
    }

    @Bean
    public Job job(Step step) {
        return jobBuilderFactory.get("myJob")
                .incrementer(new RunIdIncrementer())
                .flow(step)
                .end()
                .build();
    }
}

CsvModel

package com.example.persistence.entities;

public class CsvModel {
    private String block;
    private String tag;
    private String value;

    // Constructors, getters, and setters

    public CsvModel() {
    }

    public CsvModel(String block, String tag, String value) {
        this.block = block;
        this.tag = tag;
        this.value = value;
    }

    public String getBlock() {
        return block;
    }

    public void setBlock(String block) {
        this.block = block;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
lstz6jyr

lstz6jyr1#

如果你使用spring batch 5,你可以这样修改你的代码:

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.job.builder.JobBuilder;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

private static final Logger logger = LoggerFactory.getLogger(BatchConfiguration.class);

@Bean
public ItemReader<CsvModel> reader() {
    return new FlatFileItemReaderBuilder<CsvModel>()
            .name("csvModelItemReader")
            .resource(new ClassPathResource("SwiftMessage.csv"))
            .delimited()
            .names("block", "tag", "value")
            .linesToSkip(1)
            .fieldSetMapper(new BeanWrapperFieldSetMapper<>() {{
                setTargetType(CsvModel.class);
            }})
            .build();
}

@Bean
public ItemProcessor<CsvModel, CsvModel> processor() {
    return item -> item; // Perform any necessary processing here
}

@Bean
public ItemWriter<CsvModel> writer() {
    return items -> {
        for (CsvModel item : items) {
            // Write item to the destination
            logger.info("Writing item: {}", item);
        }
    };
}

@Bean
public Step step(ItemReader<CsvModel> reader, ItemProcessor<CsvModel, CsvModel> processor,
                 ItemWriter<CsvModel> writer,JobRepository jobRepository,PlatformTransactionManager transactionManager) {
    return new StepBuilder("myStep",jobRepository)
            .<CsvModel, CsvModel>chunk(10,transactionManager)
            .reader(reader)
            .processor(processor)
            .writer(writer)
            .build();
}

@Bean
public Job job(Step step,JobRepository jobRepository) {
    return new JobBuilder("myJob",jobRepository)
            .incrementer(new RunIdIncrementer())
            .flow(step)
            .end()
            .build();
}
}

相关问题