gpt4 book ai didi

java - 如何使用 Spring Boot Batch 在不同的类中提供不同的 Bean

转载 作者:行者123 更新时间:2023-12-01 18:42:08 26 4
gpt4 key购买 nike

我正在尝试从 SQL Server 加载数据,应用一些转换并使用 Spring Batch Scheduler 将其放入 CSV 中。当所有内容都在同一个类中时,一切正常。

这是我的代码:

package com.abc.tools.bootbatch;

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

@Autowired
public DataSource dataSource;

private static final String qry = "select top 20 colA, colB, colC from ABC";
private Resource outputResource = new FileSystemResource("output/outputData.csv");

@Bean
public DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driver_class);
dataSource.setUrl("db_url");
dataSource.setUsername(usr);
dataSource.setPassword(pwd);

return dataSource;
}

@Bean
ItemReader<Trade> reader() {
JdbcCursorItemReader<Trade> databaseReader = new JdbcCursorItemReader<>();

databaseReader.setDataSource(dataSource);
databaseReader.setSql(qry);
databaseReader.setRowMapper(new BeanPropertyRowMapper<>(Trade.class));

return databaseReader;
}

@Bean
public TradeProcessor processor() {
return new TradeProcessor();
}

@Bean
public FlatFileItemWriter<Trade> writer()
{
//Create writer instance
FlatFileItemWriter<Trade> writer = new FlatFileItemWriter<>();

//Set output file location
writer.setResource(outputResource);

//All job repetitions should "append" to same output file
writer.setAppendAllowed(true);

//Name field values sequence based on object properties
writer.setLineAggregator(new DelimitedLineAggregator<Trade>() {
{
setDelimiter(",");
setFieldExtractor(new BeanWrapperFieldExtractor<Trade>() {
{
setNames(new String[] { "colA", "colB", "colC" });
}
});
}
});
return writer;
}

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

@Bean
public Job exportUserJob() {
return jobBuilderFactory.get("exportUserJob")
.incrementer(new RunIdIncrementer())
.flow(step1())
.end()
.build();
}

}

当我将处理、加载和数据读取分开在不同的类中时,使用 Autowiring 可以正常工作,除非我使用批处理作业。在使用批处理作业时,它在实例化数据库时出错。所以我删除了 Autowiring 并尝试做这样的事情:

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

@Autowired
public DBConfig dbConfig;

public DataConnection dataconnection=new DataConnection();
DataReader reader=new DataReader();
TradeProcessor processor=new TradeProcessor();
FlatFileWriter flatFileWriter=new FlatFileWriter();

DataSource ds=dataconnection.getDataSource(dbConfig);


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

@Bean
public Job exportUserJob() {
return jobBuilderFactory.get("exportUserJob")
.incrementer(new RunIdIncrementer())
.flow(step1())
.end()
.build();
}

}

这导致无法初始化 BatchConfiguration

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchConfiguration'

我想我缺少一些东西来汇总这一切。我是 Spring 新手,感谢任何帮助

最佳答案

在第一个示例中,您 Autowiring 数据源并在同一类中声明数据源 bean,这是不正确的。在第二个示例中,您可以使用 @Import(DBConfig.class) 导入它,并根据需要在作业配置中 Autowiring 数据源,而不是 Autowiring DBConfig。这是一个典型的配置:

@Configuration
public class DBConfig {

@Bean
public DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driver_class);
dataSource.setUrl("db_url");
dataSource.setUsername(usr);
dataSource.setPassword(pwd);
return dataSource;
}

}

@Configuration
@EnableBatchProcessing
@Import(DBConfig.class)
public class BatchConfiguration {

@Bean
ItemReader<Trade> reader(DataSource datasource) {
// use datasource to configure the reader
}

}

由于您使用 Spring Boot,您可以删除 DBConfig 类,根据需要在 application.properties 文件中配置数据源,数据源将自动注入(inject)到您的批量配置

关于java - 如何使用 Spring Boot Batch 在不同的类中提供不同的 Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59908106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com