@Autowired无法正常工作/ bean无法正确注入春季批处理tsklet

时间:2020-01-11 15:19:26

标签: java spring-boot spring-batch

我正在尝试使用tasklet运行多步spring批处理作业。在tasklet的内部创建实现尝试通过@Autowired使用其他服务。但无法正常工作,并且面临空指针异常:

java.lang.NullPointerException:为null com.rml.api.poc.MyTaskTwo.getStackStatus(MyTaskTwo.java:52)位于 com.rml.api.poc.MyTaskTwo.execute(MyTaskTwo.java:41)在 org.springframework.batch.core.step.tasklet.TaskletStep $ ChunkTransactionCallback.doInTransaction(TaskletStep.java:406) 在 org.springframework.batch.core.step.tasklet.TaskletStep $ ChunkTransactionCallback.doInTransaction(TaskletStep.java:330) 在 org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140)

下面给出了示例代码:

  @Configuration
    @EnableBatchProcessing
    public class BatchConfig {
        @Autowired
        private JobBuilderFactory jobs;

        @Autowired
        private StepBuilderFactory steps;



        @Bean
        public Step stepOne() {
            return steps.get("stepOne")
                    .tasklet(new MyTaskOne())
                    .build();
        }

        @Bean
        public Step stepTwo() {
            return steps.get("stepTwo")
                    .tasklet(new MyTaskTwo())
                    .build();
        }

        @Bean
        public Step stepThree() {
            return steps.get("stepThree")
                    .tasklet(new MyTaskThree())
                    .build();
        }

        @Bean
        public Job helloJob() {

            return jobs.get("createProject")
                    .start(stepOne())
                    .next(stepTwo())
                    .next(stepThree())
                    .build();
        }

    }

Tasklet类:

public class MyTaskOne implements Tasklet {

            @Autowired
            HelloService helloService;

            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

                //custom code 
                return RepeatStatus.FINISHED;
            }
        }


public class MyTaskTwo implements Tasklet {

        @Autowired
        HelloService helloService;

        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

            //custom code 
            return RepeatStatus.FINISHED;
        }
    }


public class MyTaskThree implements Tasklet {

        @Autowired
        HelloService helloService;

        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

            //custom code 
            return RepeatStatus.FINISHED;
        }
    }

我在这里缺少什么?

0 个答案:

没有答案