首页 > 解决方案 > 如何在spring-batch中将参数从作业传递到FlowStep?

问题描述

在一个特定的 spring-batch 作业中,我使用 a 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();
   }
   ...
}

标签: spring-batch

解决方案


您可以将流 bean 定义步骤设置为范围并使用作业参数的后期绑定

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

推荐阅读