Spring Batch @BeforeContext执行失败

huangapple go评论87阅读模式
英文:

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&lt;String,String&gt; {

   @Value(#{stepExecution});
   private StepExecution stepExecution;


   public String process(String s){
     //Do things

     Context context = new Context();
     context.set(&quot;Hello Context&quot;);

     ExecutionContext executionContext = stepExecution.getExecutionContext();
     executionContext.put(&quot;Context&quot;, context);

   }

}

This fails:

@Component
@StepScope
public class MyItemProcessor implements ItemProcessor&lt;String,String&gt; {

   private ExecutionContext executionContext;

   public String process(String s){
      //Do things
      Context context = new Context();
      context.set(&quot;Hello Context&quot;);

      executionContext.put(&quot;Context&quot;, context);
   } 


   @BeforeStep
   public getCurrentContext(StepExecution stepExecution){
         executionContext = stepExecution.getExecutionContext();
   } 

}

答案1

得分: 1

由于您的项目处理器是复合结构的一部分,它不会被检测到@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:

huangapple
  • 本文由 发表于 2020年8月13日 23:10:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63398047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定