我在春季批次中有一个情况,我有多个项目处理器组成一个复合项目处理器。我需要在同一步骤中在两个处理器之间共享一些上下文数据。我找到了一个访问上下文的有效解决方案,如下所示。也就是说,有一个替代解决方案似乎更干净,但它使用@BeforeStepAnnotation,它永远不会被调用。如果可能,我想使用第二种解决方案。任何关于如何做到这一点的建议都非常感谢。
这有效:
@Component
@StepScope
public class MyItemProcessor implements ItemProcessor<String,String> {
@Value(#{stepExecution});
private StepExecution stepExecution;
public String process(String s){
//Do things
Context context = new Context();
context.set("Hello Context");
ExecutionContext executionContext = stepExecution.getExecutionContext();
executionContext.put("Context", context);
}
}
这失败了:
@Component
@StepScope
public class MyItemProcessor implements ItemProcessor<String,String> {
private ExecutionContext executionContext;
public String process(String s){
//Do things
Context context = new Context();
context.set("Hello Context");
executionContext.put("Context", context);
}
@BeforeStep
public getCurrentContext(StepExecution stepExecution){
executionContext = stepExecution.getExecutionContext();
}
}