应用程序启动失败
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;
}
}
1条答案
按热度按时间lstz6jyr1#
如果你使用spring batch 5,你可以这样修改你的代码: