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

How to solve deprecation warning of JobBuilderFactory and StepBuilderFactory

提问人:Kumar Sourav 提问时间:2/20/2023 最后编辑:g00glen00bKumar Sourav 更新时间:2/20/2023 访问量:13821

问:

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

private JobBuilderFactory jobBuilderFactory;
private StepBuilderFactory stepBuilderFactory;

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

The type JobBuilderFactory has been deprecated since version 5.0.0 and marked for removal

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

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

@Bean
public Job runJob() {
    return jobBuilderFactory
        .get("MSTabcNEUser")
        .flow(step1())
        .end()
        .build();
}

我还收到以下错误:

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. 

spring-boot spring-batch

评论


答:

13赞 g00glen00b 2/20/2023 #1

代替使用 和 ,您可以使用 和 类:JobBuilderFactoryStepBuilderFactoryJobBuilderStepBuilder

@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();
}

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

private JobRepository jobRepository;
private PlatformTransactionManager transactionManager;

另请注意,接口已从支持项目集合更改为 。 您可能需要重构一些编写器。 另请查看 Spring Batch 5.0 迁移指南ItemWriterChunk<? extends T>

评论

0赞 Kumar Sourav 2/20/2023
在类路径资源 [com/config/SpringBatchConfig.class] 中定义名称为“writer”的 Bean 时出错:需要 CrudRepository 实现
0赞 Kumar Sourav 2/20/2023
@Bean public RepositoryItemWriter<MSTabcUser> writer() { RepositoryItemWriter<MSTabcUser> writer = new RepositoryItemWriter<>(); writer.setRepository(mSTabcUserRepository); writer.setMethodName(“save”); return writer; }