![](/img/trans.png)
[英]How to access jobparameter from ItemReader class of Spring batch job in spring boot web application
[英]How to get access to job parameters from ItemReader, in Spring Batch?
这是我的job.xml
的一部分:
<job id="foo" job-repository="job-repository">
<step id="bar">
<tasklet transaction-manager="transaction-manager">
<chunk commit-interval="1"
reader="foo-reader" writer="foo-writer"
/>
</tasklet>
</step>
</job>
这是项目阅读器:
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("foo-reader")
public final class MyReader implements ItemReader<MyData> {
@Override
public MyData read() throws Exception {
//...
}
@Value("#{jobParameters['fileName']}")
public void setFileName(final String name) {
//...
}
}
这就是 Spring Batch 在运行时所说的:
Field or property 'jobParameters' cannot be found on object of
type 'org.springframework.beans.factory.config.BeanExpressionContext'
这里有什么问题? 在哪里可以阅读更多关于 Spring 3.0 中这些机制的信息?
如前所述,您的读者需要在“步骤”范围内。 您可以通过@Scope("step")
注释来完成此操作。 如果您将该注释添加到您的阅读器,它应该对您有用,如下所示:
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("foo-reader")
@Scope("step")
public final class MyReader implements ItemReader<MyData> {
@Override
public MyData read() throws Exception {
//...
}
@Value("#{jobParameters['fileName']}")
public void setFileName(final String name) {
//...
}
}
默认情况下,此范围不可用,但如果您使用batch
XML 命名空间,则可用。 如果不是,根据Spring Batch 文档,将以下内容添加到 Spring 配置中将使范围可用:
<bean class="org.springframework.batch.core.scope.StepScope" />
如果要在单个 JavaConfig 类中定义ItemReader
实例和Step
实例。 您可以使用@StepScope
和@Value
注释,例如:
@Configuration
public class ContributionCardBatchConfiguration {
private static final String WILL_BE_INJECTED = null;
@Bean
@StepScope
public FlatFileItemReader<ContributionCard> contributionCardReader(@Value("#{jobParameters['fileName']}")String contributionCardCsvFileName){
....
}
@Bean
Step ingestContributionCardStep(ItemReader<ContributionCard> reader){
return stepBuilderFactory.get("ingestContributionCardStep")
.<ContributionCard, ContributionCard>chunk(1)
.reader(contributionCardReader(WILL_BE_INJECTED))
.writer(contributionCardWriter())
.build();
}
}
诀窍是将空值传递给 itemReader,因为它将通过@Value("#{jobParameters['fileName']}")
注释注入。
感谢 Tobias Flohre 的文章: Spring Batch 2.2 – JavaConfig 第 2 部分:JobParameters、ExecutionContext 和 StepScope
很晚了,但您也可以通过注释 @BeforeStep 方法来做到这一点:
@BeforeStep
public void beforeStep(final StepExecution stepExecution) {
JobParameters parameters = stepExecution.getJobExecution().getJobParameters();
//use your parameters
}
为了能够使用 jobParameters,我认为您需要将您的阅读器定义为范围“步骤”,但我不确定您是否可以使用注释来做到这一点。
使用 xml-config 它会像这样:
<bean id="foo-readers" scope="step"
class="...MyReader">
<property name="fileName" value="#{jobExecutionContext['fileName']}" />
</bean>
在Spring Batch 文档中进一步了解。
也许它可以通过使用@Scope
并在您的 xml-config 中定义步骤范围来工作:
<bean class="org.springframework.batch.core.scope.StepScope" />
补充一个附加示例,您可以访问 JavaConfig 类中的所有作业参数:
@Bean
@StepScope
public ItemStreamReader<GenericMessage> reader(@Value("#{jobParameters}") Map<String,Object> jobParameters){
....
}
在执行作业时,我们需要传递 Job 参数,如下所示:
JobParameters jobParameters= new JobParametersBuilder().addString("file.name", "filename.txt").toJobParameters();
JobExecution execution = jobLauncher.run(job, jobParameters);
通过使用表达式语言,我们可以按如下方式导入值:
#{jobParameters['file.name']}
这可能是一种更简单的方法:
@Configuration
@Setter
@StepScope
public class Reader extends FlatFileItemReader<Object> {
public Reader(@Value("#{jobParameters['filePath']}") String resource){
setResource(new FileSystemResource(resource));
}
}
您是否将作业参数正确地声明为 bean?
还是您可能不小心实例化了一个没有文件名 getter 的JobParameters对象?
有关表达式语言的更多信息,您可以在此处的 Spring 文档中找到信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.