如何使用spring Batch注释将Job参数传递到项目处理器 [英] How to get Job parameteres in to item processor using spring Batch annotation

查看:3349
本文介绍了如何使用spring Batch注释将Job参数传递到项目处理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring MVC。从我的控制器,我调用 jobLauncher 并在 jobLauncher 我传递的作业参数如下,我正在使用注释启用如下配置:

I am using spring MVC. From my controller, I am calling jobLauncher and in jobLauncher I am passing job parameters like below and I'm using annotations to enable configuration as below:

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
        // read, write ,process and invoke job
} 

JobParameters jobParameters = new JobParametersBuilder().addString("fileName", "xxxx.txt").toJobParameters();
stasrtjob = jobLauncher.run(job, jobParameters);                              

and here is my itemprocessor                                                         
public class DataItemProcessor implements ItemProcessor<InputData, OutPutData> {

  public OutPutData process(final InputData inputData) throws Exception {

        // i want to get job Parameters here ????

  }

}


推荐答案

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> {

这篇关于如何使用spring Batch注释将Job参数传递到项目处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆