Spring Batch Step Scope 是如何工作的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38780796/
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 Does Spring Batch Step Scope Work
提问by lakshmi kanth
I have a requirement where I need to process files based on the rest call in which I get the name of the file, I am adding it to the job parameter and using it while creating the beans.
我有一个要求,我需要根据在其中获取文件名的其余调用来处理文件,我将它添加到作业参数并在创建 bean 时使用它。
I am creating step scope Beans for (reader,writer) and using the job parameter.I am starting the job in a new thread as I am using asynchronus task exceutor to launch the job and my question is how will the beans be created by spring when we define @StepScope
我正在为 (reader,writer) 创建 step scope Beans 并使用作业参数。我正在一个新线程中开始作业,因为我正在使用异步任务执行器来启动作业,我的问题是 spring 如何创建 bean当我们定义 @StepScope
jobParametersBuilder.addString("fileName", request.getFileName());
jobExecution = jobLauncher.run(job, jobParametersBuilder.toJobParameters());
@Bean
public JobLauncher jobLauncher() {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository());
jobLauncher.setTaskExecutor(asyncTaskExecutor());
return jobLauncher;
}
@Bean
@StepScope
public ItemWriter<Object> writer(@Value ("#{jobParameters['fileName']}"String fileName) {
JdbcBatchItemWriter<Object> writer = new JdbcBatchItemWriter<>();
writer.setItemSqlParameterSourceProvider(
new BeanPropertyItemSqlParameterSourceProvider<Object>());
writer.setSql(queryCollection.getquery());
writer.setDataSource(dataSource(fileName));
return writer;
}
回答by Naros
A spring batch StepScope
object is one which is unique to a specific step and not a singleton. As you probably know, the default bean scope in Spring is a singleton. But by specifying a spring batch component being StepScope
means that Spring Batch will use the spring container to instantiate a new instance of that component for each step execution.
spring 批处理StepScope
对象是特定步骤所独有的对象,而不是单例对象。您可能知道,Spring 中的默认 bean 作用域是单例。但是通过指定StepScope
Spring Batch组件意味着 Spring Batch 将使用 spring 容器为每个步骤执行实例化该组件的新实例。
This is often useful for doing parameter late binding where a parameter may be specified either at the StepContext
or the JobExecutionContext
level and needs to be substituted for a placeholder, much like your example with the filename requirement.
这对于执行参数后期绑定通常很有用,其中参数可以StepContext
在JobExecutionContext
级别或级别指定并且需要替换为占位符,就像您的具有文件名要求的示例一样。
Another useful reason to use StepScope
is when you decide to reuse the same component in parallel steps. If the component manages any internal state, its important that it be StepScope
based so that one thread does not impair the state managed by another thread (e.g, each thread of a given step has its own instance of the StepScope
component).
另一个有用的使用原因StepScope
是当您决定在并行步骤中重用相同的组件时。如果组件管理任何内部状态,重要的是它是StepScope
基于这样一个线程不会损害另一个线程管理的状态(例如,给定步骤的每个线程都有自己的StepScope
组件实例)。