英文:
Spring Batch @BeforeContext Fails to Execute
问题
以下是您要翻译的部分:
这个可以工作的解决方案如下:
@Component
@StepScope
public class MyItemProcessor implements ItemProcessor<String, String> {
@Value(#{stepExecution});
private StepExecution stepExecution;
public String process(String s){
//做一些操作
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){
//做一些操作
Context context = new Context();
context.set("Hello Context");
executionContext.put("Context", context);
}
@BeforeStep
public getCurrentContext(StepExecution stepExecution){
executionContext = stepExecution.getExecutionContext();
}
}
英文:
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.
This works:
@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);
}
}
This fails:
@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();
}
}
答案1
得分: 1
由于您的项目处理器是复合结构的一部分,它不会被检测到@BeforeStep
注释,因此不会被注册为监听器。Spring Batch只会检查已注册为处理器的对象(在您的情况下是复合对象),而不会检查整个对象图。
要使此功能正常工作,您需要将任何组成的处理器注册为监听器。以下链接可能会有所帮助:
- https://stackoverflow.com/questions/21241683/spring-batch-beforestep-does-not-work-with-stepscope
- https://github.com/spring-projects/spring-batch/issues/1428#issuecomment-566277832
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论