Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve deprecation warning of JobBuilderFactory and StepBuilderFactory

I'm using the following factories to set up my Spring Batch application:

private JobBuilderFactory jobBuilderFactory;
private StepBuilderFactory stepBuilderFactory;

However, I'm getting the following deprecation warning:

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

These are the bean declarations I'm using:

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

I'm also getting the following error:

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. 

like image 765
Kumar Sourav Avatar asked Dec 14 '25 00:12

Kumar Sourav


1 Answers

In stead of using JobBuilderFactory and StepBuilderFactory, you can use the JobBuilder and StepBuilder class:

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

The big difference is that you need to pass a JobRepository to these builders and a PlatformTransactionManager to the chunk() method. You can add these as fields to your configuration class. For example:

private JobRepository jobRepository;
private PlatformTransactionManager transactionManager;

Also be aware that the ItemWriter interface has changed from supporting a collection of items to Chunk<? extends T>. You may have to refactor some of your writers. Check also the Spring Batch 5.0 migration guide.

like image 69
g00glen00b Avatar answered Dec 16 '25 22:12

g00glen00b



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!