【发布时间】:2018-11-08 12:12:51
【问题描述】:
我最初在 [Getting "Scope 'step' is not active for the current thread" while creating spring batch beans 提出了一个问题,根据Spring batch scope issue while using spring boot 提供的建议,我尝试替换 @StepScope 注释,而是在配置中定义 StepScope bean,如下所示
@Bean
@Qualifier("stepScope")
public org.springframework.batch.core.scope.StepScope stepScope() {
final org.springframework.batch.core.scope.StepScope stepScope = new org.springframework.batch.core.scope.StepScope();
stepScope.setAutoProxy(true);
return stepScope;
}
通过此更改,我无法在创建 bean 时传递作业参数,因为它正在抛出
在“org.springframework.beans.factory.config.BeanExpressionContext”类型的对象上找不到“jobParameters”
我的配置是这样的
@Configuration
@EnableBatchProcessing
public class MyConfig{
@Bean
@Qualifier("partitionJob")
public Job partitionJob() throws Exception {
return jobBuilderFactory
.get("partitionJob")
.incrementer(new RunIdIncrementer())
.start(partitionStep(null))
.build();
}
@Bean
@StepScope
public Step partitionStep(
@Value("#{jobParameters[gridSize]}") String gridSize)
throws Exception {
return stepBuilderFactory
.get("partitionStep")
.partitioner("slaveStep", partitioner())
.gridSize(gridZize)
.step(slaveStep(chunkSize))
.taskExecutor(threadPoolTaskExecutor()).build();
}
@Bean
@StepScope
public Step slaveStep(int chunkSize) throws Exception {
return stepBuilderFactory
.get("slaveStep")
......
........
}
我读到,如果需要像我的示例一样访问作业参数,则应使用 @StepScope 注释 bean。但如上所述,我遇到了例外情况。
【问题讨论】:
-
我尝试了一种解决方法,将 gridsize 作为 java 系统属性 -DgridSize=10 传递并使用 @Value 注释注入。仅当在读取器/写入器/处理器上使用时,可能在 stepScope 注释和 JobParameter 注入一起工作。不在台阶上。
-
您是否尝试在 xml 配置中模仿这一点以进行测试?我认为你不应该在 xml 中得到这个错误。我知道您可能需要在 Spring Boot 上完成这项工作,但以防万一这值得一试。
-
还没有,可能会尝试xml配置,而不是java配置,看看它是如何工作的。不过感谢您的建议。
-
您是否尝试过改用
@JobScope?
标签: spring spring-batch