在我的Spring批处理配置中,我正在尝试设置一个分区步骤,它从JobParameters访问值,如下所示:

@Bean
@Qualifier("partitionJob")
public Job partitionJob() throws Exception {

    return jobBuilderFactory
            .get("partitionJob")
            .incrementer(new RunIdIncrementer())
            .start(partitionStep(null))
            .build();
}

@Bean
@StepScope //I'm getting exception here - > Error creating bean 
public Step partitionStep(
        @Value("#{jobParameters[gridSize]}") String gridSize)
        throws Exception {

    return stepBuilderFactory
            .get("partitionStep")
            .partitioner("slaveStep", partitioner())
            .gridSize(
                    StringUtils.isEmpty(gridSize) ? 10 : Integer
                            .parseInt(gridSize))
            .step(slaveStep(50000))
            .taskExecutor(threadPoolTaskExecutor()).build();
}

@Bean
@StepScope
public Step slaveStep(int chunkSize) throws Exception {

    return stepBuilderFactory
            .get("slaveStep")
            .<Person,Person> chunk(chunkSize)
            .reader(jdbcPagingItemReader()),
            .writer(csvFileWriterParts())
            .listener(stepExecutionListener()).build();
}

我已将@EnableBatchProcessing注释添加到我的SpringBoot应用程序中 .

由于我想在构建步骤时访问JobParameters,因此我使用了@StepScope . 我有一个很好的例子,没有@StepScope注释,但在这种情况下,我没有访问任何JobParameters或任何来自上下文 .

但是如果我在partitionStep上使用StepScope注释,我就会得到

创建名为'scopedTarget.partitionStep'的bean时出错:Scope'step'对当前线程无效;考虑为这个bean定义一个范围代理,如果你想从一个单例引用它;嵌套异常是java.lang.IllegalStateException:没有可用于步骤范围的上下文持有者

但如果我将其更改为JobScope,那么它在slaveStep()失败时会显示相同的错误消息 .

在这种情况下使用的正确范围是什么以及如何解决这个问题我得到了什么?

在配置spring bean时访问JobParameters的更好方法是什么?

异常堆栈如下

2018-05-25 21:07:32,075错误[main] org.springframework.batch.core.job.AbstractJob:执行作业时遇到致命错误org.springframework.beans.factory.BeanCreationException:创建名为'scopedTarget的bean时出错 . partitionStep':Scope'step'对当前线程无效;考虑为这个bean定义一个范围代理,如果你想从一个单例引用它;嵌套异常是java.lang.IllegalStateException:在org.springframework.beans.factory.support.AbstractBeanFactory的org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:361)中没有可用于步骤范围的上下文持有者 . getBean(AbstractBeanFactory.java:197)位于com.sun的org.springframework.aop.frame.J. .proxy . $ Proxy55.getName(未知来源)org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:115)org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java) :392)在org.springframework的org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306)的org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:135) . org.springframe上的batch.core.launch.support.SimpleJobLauncher $ 1.run(SimpleJobLauncher.java:135) work.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)atg.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) )在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)atg的java.lang.reflect.Method.invoke(Method.java:498) .springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation) .java:157)org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration $ PassthruAdvice.invoke(SimpleBatchConfiguration.java:127)org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodI) nvocation.java:179)位于com.sample.main的comgun.proxy . $ Proxy54.run(未知来源)的org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)(ExtractApplication.java) :58)引起:java.lang.IllegalStateException:在org.springframework.batch.core.scope的org.springframework.batch.core.scope.StepScope.getContext(StepScope.java:167)中没有可用于步骤范围的上下文持有者.stepScope.get(StepScope.java:99)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:346)...省略了23个常用帧

如果我修改为JobScope,我会在slaveStep上获得异常,这与上面的异常类似 .