Spring Batch – JobRepository 配置不是必需的?

huangapple go评论56阅读模式
英文:

Spring Batch - JobRepository Configuration not required?

问题

I'm following the official Spring Batch starter guide. I have a basic question. I get an error, that the Bean JobRepository is not defined. The guide really never creates that bean. It is used in the importUserJob and step1. How is that possible? Shouldn't they create that Bean to configure the db etc.? This is the Code from the Guide.

这是我在遵循官方的 Spring Batch 入门指南。我有一个基本的问题。我收到一个错误,即未定义 Bean JobRepository。指南确实从未创建过这个 Bean。它在 importUserJob 和 step1 中使用。这是怎么可能的?他们应该创建该 Bean 来配置数据库等等。以下是指南中的代码。

英文:

I'm following the official Spring Batch starter guide.
I have a basic question. I get an error, that the Bean JobRepository is not defined.
The guide really never creates that bean. It is used in the importUserJob and step1. How is that possible?
Shouldn't they create that Bean to configure the db etc.? This is the Code from the Guide.
The full code can be found here.

@Configuration
public class BatchConfiguration {

	// tag::readerwriterprocessor[]
	@Bean
	public FlatFileItemReader<Person> reader() {
		return new FlatFileItemReaderBuilder<Person>()
			.name("personItemReader")
			.resource(new ClassPathResource("sample-data.csv"))
			.delimited()
			.names(new String[]{"firstName", "lastName"})
			.fieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
				setTargetType(Person.class);
			}})
			.build();
	}

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

	@Bean
	public JdbcBatchItemWriter<Person> writer(DataSource dataSource) {
		return new JdbcBatchItemWriterBuilder<Person>()
			.itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
			.sql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)")
			.dataSource(dataSource)
			.build();
	}

	@Bean
	public Job importUserJob(JobRepository jobRepository,
			JobCompletionNotificationListener listener, Step step1) {
		return new JobBuilder("importUserJob", jobRepository)
			.incrementer(new RunIdIncrementer())
			.listener(listener)
			.flow(step1)
			.end()
			.build();
	}

	@Bean
	public Step step1(JobRepository jobRepository,
			PlatformTransactionManager transactionManager, JdbcBatchItemWriter<Person> writer) {
		return new StepBuilder("step1", jobRepository)
			.<Person, Person> chunk(10, transactionManager)
			.reader(reader())
			.processor(processor())
			.writer(writer)
			.build();
	}
}

`

Thanks.

答案1

得分: 1

Spring Boot创建了该Bean并将其添加到应用程序上下文中。因此,在Bean定义方法中可以自动装配它,并将其用于配置作业和步骤。

有关更多详细信息,请查看Spring Boot文档中有关Spring Batch的部分此处

英文:

It is Spring Boot that creates that bean and adds it to the application context. Therefore, it is possible to autowire it in bean definition methods and use it to configure jobs and steps.

For more details, please check the section about Spring Batch in Spring Boot's documentation here.

答案2

得分: 1

JobRepository 将在您添加依赖项后自动配置:

org.springframework.boot:spring-boot-starter-batch

这还将在为您的应用程序配置的数据库中创建一些表,例如 BATCH_JOB_EXECUTIONBATCH_STEP_EXECUTION

在适当的自动配置后,JobRepository bean 可以在任何组件或配置类中自动装配,也可以作为构造函数参数,就像您的 BatchConfiguration 代码片段中一样。

英文:

JobRepository will be auto-configured as soon as you add the dependency:

org.springframework.boot:spring-boot-starter-batch

This will also create a few tables in the database configured for your application. Like BATCH_JOB_EXECUTION and BATCH_STEP_EXECUTION.

After proper auto-configuration JobRepositoty bean can be autowired in any component or configuration class. Also as a constructor parameter like in your code snippet of BatchConfiguration.

huangapple
  • 本文由 发表于 2023年6月15日 01:05:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475987.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定