首页 > 解决方案 > 如何使用 CompositeItemProcessor 在处理器中获取 stepExecutionContext?

问题描述

在 XML 文件中,我使用CompositeItemProcessor配置了两个处理器

    <processor>
          <beans:bean id="CompositeItemProcessor" class="org.springframework.batch.item.support.CompositeItemProcessor" scope="step">
            <beans:property name="delegates">
              <beans:list>
                <beans:ref bean="oldProcessor"/>
                <beans:ref bean="newProcessor"/>
              </beans:list>
            </beans:property>
          </beans:bean>
        </processor>

在“ oldProcessor ”bean java 文件中,我添加了下面的代码来获取StepExecution对象。

      @BeforeStep
      @Order(1)
      public void setStepExecution(StepExecution stepExecution) {
        this.stepExecution = stepExecution;
      }

但上面的代码没有执行。只有从“ oldProcessor ”bean调用的process()方法。

当我尝试不配置 CompositeItemProcessor时,这个“ setStepExecution() ”方法和process()方法正在执行 oldProcessor bean。例如。

<processor>
    <beans:ref bean="oldProcessor"/>
</processor>

请告诉我,如何使用 CompositeItemProcessor 在处理器中获取 stepExecutionContext

标签: springspring-batchspring-batch-tasklet

解决方案


在您的处理器中尝试以下操作。

@Value("#{stepExecution}")

private StepExecution stepExecution;

如果范围是步骤,这应该有效。


推荐阅读