如何将数据传递给 Spring Batch 的 ItemProcessor?

huangapple go评论61阅读模式
英文:

How to pass data into spring batch ItemProcessor?

问题

我有一个包含数据记录(案例)的CSV文件,我创建了一个caseDTO对象来存储这些数据。然而,caseDTO的一些属性必须从文件名中包含的数据中提取(每个文件的名称都必须具有严格的结构,包含一些数据)。

我想要实现的目标是将文件名中包含的数据传递给项目处理器(item processor),在将其传递给项目写入器(item writer)之前,将此数据追加到每个caseDTO中。

是否有人知道如何在从控制器启动作业时将数据传递给步骤中的itemProcessor

提前感谢。

英文:

I have a CSV file that contains records of data (cases), for which I created a caseDTO object , however some of the properties of the caseDTO must be field from data included in the file name (every file must has a name with strict structure that contains some data).

What i want to achieve is to pass the data included in the file name to the item processor where i will append this data into every caseDTO before passing it to the item writer.

Does any one knows how to pass data to a step itemProcessor when I launch the job from the controller?

Thanks in advance.

答案1

得分: 0

最简单的解决方案是将动态参数设置在作业参数中,这样步骤的写入器、处理器和读取器就可以访问它们。通过以下方式将它们的值注入:

@Value("#{jobParameters['fileName']}")
public void setFileName(final String name) {
  //...
}
英文:

the simplest solution is to set the dynamic parameters in the job parameters so the step's writer , processor , reader can have access to it . by injecting their values as following

@Value("#{jobParameters['fileName']}")
public void setFileName(final String name) {
//...
}

huangapple
  • 本文由 发表于 2020年9月11日 00:36:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63834052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定