如何解决JobBuilderFactory和StepBuilderFactory的弃用警告

Kum*_*rav 9 spring-batch spring-boot

我使用以下工厂来设置我的 Spring Batch 应用程序:

private JobBuilderFactory jobBuilderFactory;
private StepBuilderFactory stepBuilderFactory;
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下弃用警告:

The type JobBuilderFactory has been deprecated since version 5.0.0 and marked for removal
Run Code Online (Sandbox Code Playgroud)

这些是我正在使用的 bean 声明:

The type JobBuilderFactory has been deprecated since version 5.0.0 and marked for removal
Run Code Online (Sandbox Code Playgroud)

我还收到以下错误:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jobController': Unsatisfied dependency expressed through field 'job': Error creating bean with name 'runJob' defined in class path resource [com/nissan/auraQuantics/config/SpringBatchConfig.class]: Failed to instantiate [org.springframework.batch.core.Job]: Factory method 'runJob' threw exception with message: Error creating bean with name 'step1' defined in class path resource [com/nissan/auraQuantics/config/SpringBatchConfig.class]: Unsatisfied dependency expressed through method 'step1' parameter 2: No qualifying bean of type 'org.springframework.batch.item.database.JdbcBatchItemWriter<com.nissan.auraQuantics.entity.MSTAuraQuanticNEUser>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2023-02-20T16:10:49.187+05:30  INFO 22644 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2023-02-20T16:10:49.199+05:30  INFO 22644 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2023-02-20T16:10:49.221+05:30  INFO 22644 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed. 

Run Code Online (Sandbox Code Playgroud)

g00*_*00b 14

您可以使用JobBuilderFactoryand类来代替使用and :StepBuilderFactoryJobBuilderStepBuilder

@Bean
public Step step1() {
    return new StepBuilder("csv-step", jobRepository)
        .<MSTabcNEUser, MSTabcNEUser>chunk(10, transactionManager)
        .reader(reader())
        .processor(processor())
        .writer(writer())
        .taskExecutor(taskExecutor())
        .build();
    }

@Bean
public Job runJob() {
    return new JobBuilder("MSTabcNEUser", jobRepository)
        .start(step1())
        .build();
}
Run Code Online (Sandbox Code Playgroud)

最大的区别是您需要将 a 传递JobRepository给这些构建器并将 aPlatformTransactionManager传递给chunk()方法。您可以将这些作为字段添加到您的配置类中。例如:

private JobRepository jobRepository;
private PlatformTransactionManager transactionManager;
Run Code Online (Sandbox Code Playgroud)

另请注意,ItemWriter界面已从支持项目集合更改为Chunk<? extends T>. 您可能必须重构一些编写器。另请查看Spring Batch 5.0 迁移指南