且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用spring Batch注释将Job参数传递到项目处理器

更新时间:2021-10-05 00:11:22

1)在数据处理器上放置范围注释,即

1) Put a scope annotation on your data processor i.e.

@Scope(value = "step") 

2)在数据处理器中创建一个类实例,并使用值注释注入作业参数值:

2) Make a class instance in your data processor and inject the job parameter value by using value annotation :

@Value("#{jobParameters['fileName']}")
private String fileName;

您的最终数据处理器类如下:

Your final Data processor class will look like:

@Scope(value = "step")
public class DataItemProcessor implements ItemProcessor<InputData, OutPutData> {

@Value("#{jobParameters['fileName']}")
private String fileName;

  public OutPutData process(final InputData inputData) throws Exception {

        // i want to get job Parameters here ????
      System.out.println("Job parameter:"+fileName);

  }

  public void setFileName(String fileName) {
        this.fileName = fileName;
    }


}

如果你的话数据处理器未初始化为bean,在其上放置@Component注释:

In case your data processor is not initialized as a bean, put a @Component annotation on it:

@Component("dataItemProcessor")
@Scope(value = "step")
public class DataItemProcessor implements ItemProcessor<InputData, OutPutData> {