Java Spring批处理@BeforeContext无法执行
在SpringBatch中,我有一种情况,即我有多个项目处理器,组成一个复合项目处理器。我需要在同一步骤中在两个处理器之间共享一些上下文数据。我找到了一个访问上下文的有效解决方案,如下所示。也就是说,有一种替代解决方案看起来更干净一些,但它使用了@BeforeStepAnnotation,它从未被调用过。如果可能的话,我想使用第二种解决方案。任何关于如何做到这一点的建议都将不胜感激 这项工作:Java Spring批处理@BeforeContext无法执行,java,spring,spring-batch,batch-processing,itemprocessor,Java,Spring,Spring Batch,Batch Processing,Itemprocessor,在SpringBatch中,我有一种情况,即我有多个项目处理器,组成一个复合项目处理器。我需要在同一步骤中在两个处理器之间共享一些上下文数据。我找到了一个访问上下文的有效解决方案,如下所示。也就是说,有一种替代解决方案看起来更干净一些,但它使用了@BeforeStepAnnotation,它从未被调用过。如果可能的话,我想使用第二种解决方案。任何关于如何做到这一点的建议都将不胜感激 这项工作: @Component @StepScope public class MyItemProcessor
@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);
}
}
@组件
@步进镜
公共类MyItemProcessor实现ItemProcessor{
@值(#{stepExecution});
私人分步执行分步执行;
公共字符串进程(字符串s){
//做事
上下文=新上下文();
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();
}
}
@组件
@步进镜
公共类MyItemProcessor实现ItemProcessor{
私有ExecutionContext ExecutionContext;
公共字符串进程(字符串s){
//做事
上下文=新上下文();
set(“Hello context”);
executionContext.put(“Context”,Context);
}
@先于
公共getCurrentContext(步骤执行步骤执行){
executionContext=stepExecution.getExecutionContext();
}
}
由于您的项目处理器是组合的一部分,因此不会对@BeforeStep
注释进行内省,因此不会将其注册为侦听器。SpringBatch将只内省注册为处理器的对象(在您的案例中是复合对象),而不是整个对象图
您需要将任何编写处理器注册为侦听器,才能使其正常工作。以下链接可能会有所帮助: