Java 如何使用spring批处理注释将作业参数输入到项目处理器
我正在使用SpringMVC。在我的控制器中,我正在调用Java 如何使用spring批处理注释将作业参数输入到项目处理器,java,spring,spring-mvc,spring-batch,spring-batch-admin,Java,Spring,Spring Mvc,Spring Batch,Spring Batch Admin,我正在使用SpringMVC。在我的控制器中,我正在调用jobLauncher,在jobLauncher中,我正在传递作业参数,如下所示,并使用注释来启用配置: @Configuration @EnableBatchProcessing public class BatchConfiguration { // read, write ,process and invoke job } JobParameters jobParameters = new JobParameter
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 ????
}
}
@配置
@启用批处理
公共类批处理配置{
//读、写、处理和调用作业
}
JobParameters JobParameters=new JobParameters Builder().addString(“fileName”,“xxxx.txt”).toJobParameters();
stasrtjob=jobLauncher.run(作业,作业参数);
这是我的项目处理器
公共类DataItemProcessor实现ItemProcessor{
公共OutPutData进程(最终InputData InputData)引发异常{
//我想在这里获取作业参数????
}
}
1)在数据处理器上放置范围注释,即
@Scope(value = "step")
2) 在数据处理器中创建类实例,并使用值注释注入作业参数值:
@Value("#{jobParameters['fileName']}")
private String fileName;
您的最终数据处理器类将如下所示:
@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;
}
}
@Scope(value=“step”)
公共类DataItemProcessor实现ItemProcessor{
@值(“#{jobParameters['fileName']}”)
私有字符串文件名;
公共OutPutData进程(最终InputData InputData)引发异常{
//我想在这里获取作业参数????
System.out.println(“作业参数:“+fileName”);
}
public void setFileName(字符串文件名){
this.fileName=文件名;
}
}
如果您的数据处理器未初始化为bean,请在其上放置@Component注释:
@Component("dataItemProcessor")
@Scope(value = "step")
public class DataItemProcessor implements ItemProcessor<InputData, OutPutData> {
@组件(“dataItemProcessor”)
@范围(value=“步骤”)
公共类DataItemProcessor实现ItemProcessor{
在我看来,避免使用Spring的hacky expression language(SpEL)的一个更好的解决方案是使用@BeforeStep
将步骤执行
上下文自动连接到处理器中
在处理器中,添加如下内容:
@BeforeStep
public void beforeStep(final StepExecution stepExecution) {
JobParameters jobParameters = stepExecution.getJobParameters();
// Do stuff with job parameters, e.g. set class-scoped variables, etc.
}
@BeforeStep
注释
在执行步骤之前标记要调用的方法
在创建并持久化步骤执行之后,但在第一步执行之前
项目已读
如果您的处理器是在XML中配置的,您应该在那里添加作用域,例如:有没有办法将自定义类型列表设置为作业参数并在item processor中获取该列表?它不应该是@StepScope
而不是@scope(value=“step”)
?在batch 1.5.10中这是一种非常干净的方法