java 如何使用 spring Batch 注释将作业参数获取到项目处理器中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31737209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Job parameteres in to item processor using spring Batch annotation
提问by Mare
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:
我正在使用 spring MVC。从我的控制器中,我正在调用jobLauncher
并jobLauncher
传递如下作业参数,并且我正在使用注释来启用如下配置:
@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 ????
}
}
回答by Amit Bhati
1) Put a scope annotation on your data processor i.e.
1)在您的数据处理器上放置一个范围注释,即
@Scope(value = "step")
2) Make a class instance in your data processor and inject the job parameter value by using value annotation :
2) 在您的数据处理器中创建一个类实例并使用值注释注入作业参数值:
@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;
}
}
In case your data processor is not initialized as a bean, put a @Component annotation on it:
如果您的数据处理器未初始化为 bean,请在其上添加 @Component 注释:
@Component("dataItemProcessor")
@Scope(value = "step")
public class DataItemProcessor implements ItemProcessor<InputData, OutPutData> {
回答by heez
A better solution (in my opinion) that avoids using Spring's hacky expression language (SpEL) is to autowire the StepExecution
context into your processor using @BeforeStep
.
避免使用 Spring 的 hacky 表达式语言 (SpEL) 的更好解决方案(在我看来)是StepExecution
使用@BeforeStep
.
In your processor, add something like:
在您的处理器中,添加如下内容:
@BeforeStep
public void beforeStep(final StepExecution stepExecution) {
JobParameters jobParameters = stepExecution.getJobParameters();
// Do stuff with job parameters, e.g. set class-scoped variables, etc.
}
The @BeforeStep
annotation
该@BeforeStep
注解
Marks a method to be called before a
Step
is executed, which comes after aStepExecution
is created and persisted, but before the first item is read.
标记要在 a
Step
执行之前调用的方法,该方法在 aStepExecution
创建并持久化之后,但在读取第一项之前。