Spring Batch 5.0以降の書き方を知りたい

前提

Spring Batch 5.0以降で書き方が変更になったのですが、
現在使用している参考書「悲惨なミスをなくすSpringBatch入門書: Spring解体新書(バッチ編): 基礎から学べるSpring Batch」は、5.0以降に対応した書き方になっていないため、5.0以降に対応した書き方を教えて頂きたいです。
また、Spring Batch 5.0以降に対応した書き方をしているサイト・参考書が知りたい

ここに実現したいことを箇条書きで書いてください。

発生している問題・エラーメッセージ

以下のサイトを参考にしたところエラーメッセージが発生してしまいました。
https://github.com/spring-projects/spring-batch/wiki/Spring-Batch-5.0-Migration-Guide

org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'helloTasklet' defined in class path resource [com/example/demo/config/BatchConfig.class]: @Bean definition illegally overridden by existing bean definition: Root bean: class [org.springframework.aop.scope.ScopedProxyFactoryBean]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null; defined in BeanDefinition defined in file [C:\pleiades\2022-06\workspace\BatchHelloWorldTasklet\target\classes\com\example\demo\tasklet\HelloTasklet.class]

ファイル構成

イメージ説明

該当のソースコード

BatchConfig.java(Spring Batch 5.0以前の書き方)

java

1package com.example.demo.config;2 3import org.springframework.batch.core.Job;4import org.springframework.batch.core.Step;5import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;6import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;7import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;8import org.springframework.batch.core.launch.support.RunIdIncrementer;9import org.springframework.batch.core.step.tasklet.Tasklet;10import org.springframework.beans.factory.annotation.Autowired;11import org.springframework.context.annotation.Bean;12import org.springframework.context.annotation.Configuration;13 14@Configuration15@EnableBatchProcessing16public class BatchConfig {17 18 19 @Autowired20 private JobBuilderFactory jobBuilderFactory;21 22 23 @Autowired24 private StepBuilderFactory stepBuilderFactory;25 26 27 @Autowired28 private Tasklet helloTasklet;29 30 31 @Bean32 public Step taskletStep1() {33 return stepBuilderFactory.get("HelloTaskletStep1") // Builderの取得34 .tasklet(helloTasklet) // Taskletのセット35 .build(); // Stepの生成36 }37 38 39 @Bean40 public Job taskletJob() throws Exception {41 return jobBuilderFactory.get("HelloWorldTaskletJob") // Builderの取得42 .incrementer(new RunIdIncrementer()) // IDのインクリメント43 .start(taskletStep1()) // 最初のStep44 .build(); // Jobの生成45 }46}

BatchConfig.java(Spring Batch 5.0以降の書き方)

java

1package com.example.demo.config;2 3import org.springframework.batch.core.Job;4import org.springframework.batch.core.Step;5import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;6import org.springframework.batch.core.job.builder.JobBuilder;7import org.springframework.batch.core.launch.support.RunIdIncrementer;8import org.springframework.batch.core.repository.JobRepository;9import org.springframework.batch.core.step.builder.StepBuilder;10import org.springframework.batch.core.step.tasklet.Tasklet;11import org.springframework.context.annotation.Bean;12import org.springframework.context.annotation.Configuration;13import org.springframework.transaction.PlatformTransactionManager;14 15import com.example.demo.tasklet.HelloTasklet;16 17@Configuration18@EnableBatchProcessing19public class BatchConfig {20 21 @Bean22 public Tasklet helloTasklet() {23 return new HelloTasklet();24 }25 26 @Bean27 public Step taskletStep1(JobRepository jobRepository, Tasklet helloTasklet, PlatformTransactionManager transactionManager) {28 return new StepBuilder("HelloTaskletStep1", jobRepository) 29 .tasklet(helloTasklet, transactionManager) 30 .build();31 }32 33 @Bean34 public Job taskletJob(JobRepository jobRepository, Step step) {35 return new JobBuilder("HelloTaskletStep", jobRepository)36 .incrementer(new RunIdIncrementer()) 37 .start(step) 38 .build(); 39 }40}

HelloTasklet.java(Spring Batch 5.0以前の書き方) ※ このクラスは書き方を変えておらず共通

java

1package com.example.demo.tasklet;2 3import org.springframework.batch.core.StepContribution;4import org.springframework.batch.core.configuration.annotation.StepScope;5import org.springframework.batch.core.scope.context.ChunkContext;6import org.springframework.batch.core.step.tasklet.Tasklet;7import org.springframework.batch.repeat.RepeatStatus;8import org.springframework.stereotype.Component;9import lombok.extern.slf4j.Slf4j;10 11@Component12@StepScope13@Slf4j14public class HelloTasklet implements Tasklet {15 16 @Override17 public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)18 throws Exception {19 log.info("Hello World");20 return RepeatStatus.FINISHED;21 }22}

補足情報(FW/ツールのバージョンなど)

Spring Boot 3.0.2
Java 19

参考にしたサイト/資料

コメントを投稿

0 コメント