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

问题描述 投票:0回答:2

我使用以下工厂来设置我的 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
2个回答
16
投票

您可以使用

JobBuilderFactory
StepBuilderFactory
类来代替使用
JobBuilder
StepBuilder

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

最大的区别在于,您需要将

JobRepository
传递给这些构建器,并将
PlatformTransactionManager
传递给
chunk()
方法。 您可以将这些作为字段添加到您的配置类中。 例如:

private JobRepository jobRepository;
private PlatformTransactionManager transactionManager;

另请注意,

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


0
投票

引起:org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.batch.core.repository.JobRepository]:工厂方法“jobRepository”抛出异常并显示消息:无法配置默认作业存储库

我做了同样的事情,但得到了像下面这样的异常 导致:org.springframework.batch.core.configuration.BatchConfigurationException:无法配置默认作业存储库

原因:org.springframework.batch.core.configuration.BatchConfigurationException:无法在应用程序上下文中找到数据源bean。要使用默认配置,应在应用程序上下文中定义名为“dataSource”的数据源 bean,但未找到任何数据源 bean。重写 getDataSource() 以提供用于批处理元数据的数据源。

© www.soinside.com 2019 - 2024. All rights reserved.