如何使用 spring 批注解将作业参数导入项目处理器
How to get Job parameteres in to item processor using spring Batch annotation
我正在使用 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 ????
}
}
1) 在您的数据处理器上添加范围注释,即
@Scope(value = "step")
2) 在您的数据处理器中创建一个 class 实例并使用值注释注入作业参数值:
@Value("#{jobParameters['fileName']}")
private String fileName;
您的最终数据处理器 class 将如下所示:
@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 注释:
@Component("dataItemProcessor")
@Scope(value = "step")
public class DataItemProcessor implements ItemProcessor<InputData, OutPutData> {
避免使用 Spring 的 hacky 表达式语言 (SpEL) 的更好解决方案(在我看来)是使用 @BeforeStep
将 StepExecution
上下文自动连接到您的处理器中。
在您的处理器中,添加如下内容:
@BeforeStep
public void beforeStep(final StepExecution stepExecution) {
JobParameters jobParameters = stepExecution.getJobParameters();
// Do stuff with job parameters, e.g. set class-scoped variables, etc.
}
@BeforeStep
注解
Marks a method to be called before a Step
is executed, which comes
after a StepExecution
is created and persisted, but before the first
item is read.
我正在使用 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 ????
}
}
1) 在您的数据处理器上添加范围注释,即
@Scope(value = "step")
2) 在您的数据处理器中创建一个 class 实例并使用值注释注入作业参数值:
@Value("#{jobParameters['fileName']}")
private String fileName;
您的最终数据处理器 class 将如下所示:
@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 注释:
@Component("dataItemProcessor")
@Scope(value = "step")
public class DataItemProcessor implements ItemProcessor<InputData, OutPutData> {
避免使用 Spring 的 hacky 表达式语言 (SpEL) 的更好解决方案(在我看来)是使用 @BeforeStep
将 StepExecution
上下文自动连接到您的处理器中。
在您的处理器中,添加如下内容:
@BeforeStep
public void beforeStep(final StepExecution stepExecution) {
JobParameters jobParameters = stepExecution.getJobParameters();
// Do stuff with job parameters, e.g. set class-scoped variables, etc.
}
@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.