如何在 spring-batch 中将参数从作业传递到 FlowStep?

How to pass parameters from a job to a FlowStep in spring-batch?

在特定的 spring 批处理作业中,我使用 Flow(因为它是可重复使用的步骤序列)作为 Step.

我必须将一组参数传递给 Flow

我该怎么做?

我的工作定义如下:

@Component
public class MyJob {
   @Bean
   public Job myJob(@Qualifier("myFlowStep") Flow myFlow) {
      return jobBuilderFactory.get("myJob").incrementer(new RunIdIncrementer())
        .start(someFirstStep())
        .next(myFlowStep(myFlow)).
        .build();
   }

   ...
   @Bean
   public Step myFlowStep(Flow myFlow) {
      // Need to pass parameters to the flow
      FlowStepBuilder flowStepBuilder = stepBuilderFactory.get("myFlowStep").flow(myFlow);
      return flowStepBuilder.build();
   }
   ...
}

您可以将流 bean 定义步骤限定范围并使用 late-binding 作业参数:

@Bean
@StepScope
public Flow myFlow(@Value("#{jobParameters['name']}") String name) {
    // use job parameter name here
    return null;
}

@Bean
public Step myFlowStep(Flow myFlow) {
  // Need to pass parameters to the flow
  FlowStepBuilder flowStepBuilder = stepBuilderFactory.get("myFlowStep").flow(myFlow);
  return flowStepBuilder.build();
}