Spring Batch @BeforeContext无法执行 [英] Spring Batch @BeforeContext Fails to Execute
问题描述
在春季批处理中,我有多个组成复合物料处理器的物料处理器.我需要在同一步骤中在两个处理器之间共享一些上下文数据.我找到了一个访问上下文的可行解决方案,如下所示.那就是说,有一个替代解决方案似乎更干净一些,但是它使用了@BeforeStepAnnotation,它从未被调用.如果可能的话,我想使用第二种解决方案.非常感谢您提供有关如何执行此操作的建议.
I have a situation in spring batch where I have multiple item processors that make up a composite item processor. I need to share some context data between two processors in the same step. I have found a working solution to access the context, shown below. That said there is an alternate solution that appears to be a bit cleaner but it uses the @BeforeStepAnnotation, which never gets called. I would like to use the second solution if possible. Any advice on how to do this is much appreciated.
这有效:
@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();
}
}
推荐答案
由于项目处理器是复合处理器的一部分,因此对于@BeforeStep
注释,它不会自检,因此不会注册为侦听器. Spring Batch将仅检查已注册为处理器的对象(在您的情况下为复合对象),而不是整个对象图.
Since your item processor is part of a composite, it is not introspected for @BeforeStep
annotation and hence it is not registered as a listener. Spring Batch will only introspect the object that is registered as a processor (the composite in your case) and not the entire object graph.
您需要将所有组成处理器注册为侦听器,才能正常工作.以下链接可能会有所帮助:
You need to register any composing processor as a listener for this to work. The following links might help:
- Spring-batch @BeforeStep不适用于@StepScope
- https://github.com/spring-projects/spring-batch/issues/1428#issuecomment-566277832
- Spring-batch @BeforeStep does not work with @StepScope
- https://github.com/spring-projects/spring-batch/issues/1428#issuecomment-566277832
这篇关于Spring Batch @BeforeContext无法执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!