我正在尝试使用stepExecution在Spring批处理中保留数据。但是,当我尝试在代码执行中使用它时遇到了一些问题:

ExecutionContext stepContext = this.stepExecution.getExecutionContext();


当我对此感到累时,stepExecution无法识别。我导入org.springframework.batch.core.StepExecution并尝试:

ExecutionContext context = StepExecution.getJobExecution().getExecutionContext();


在这里,可以识别StepExecution,但是出现“无法静态引用非静态方法”错误。我的项目设置不正确吗?我在这里做错了什么?

import TextFileReader;
import OracleService;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.batch.core.StepExecution;


public class myTask implements Tasklet {

private List<String> sourceQueries;
private List<String> targetQueries;

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

    ExecutionContext context = StepExecution.getJobExecution().getExecutionContext();

    ExecutionContext stepContext = this.stepExecution.getExecutionContext();
    stepContext.put("theListKey", sourceQueries);

    return RepeatStatus.FINISHED;
}

public List<String> getSourceQueries() {
    return sourceQueries;
}

public void setSourceQueries(List<String> sourceQueries) {
    this.sourceQueries = sourceQueries;
}

public List<String> getTargetQueries() {
    return targetQueries;
}

public void setTargetQueries(List<String> targetQueries) {
    this.targetQueries = targetQueries;
}


}

最佳答案

您可以使用BeforeStep批注从逐步作用域的bean中获取stepExecution:

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

10-05 17:51