Spring Batch: How to access the current step's id / name from within an ItemReader or ItemWriter

我的未来我决定 提交于 2019-12-12 23:26:34

问题


I am quite new to Spring-Batch and I wonder whether there is a way to access the step-id from within an ItemReader or ItemWriter?

In my case that would allow switching enum types based on different step-definitions in a single ItemReader implementation.

Does anyone know a way to do that?


回答1:


Assuming the ItemReader or ItemWriter are step scoped, you can do this:

<bean id="flatFileItemReader" scope="step"
      class="org.springframework.batch.item.file.FlatFileItemReader">
    <property name="resource" value="#{stepExecution.stepName}" />
</bean>



回答2:


It is as simple as implementing a with @BeforeStep annotated method:

@BeforeStep
public void beforeStep(StepExecution stepExecution){
    String name = stepExecution.getStepName();
    System.out.println("name: " + name);
}

Placed it in my ItemReader and that's it ...



来源:https://stackoverflow.com/questions/20838522/spring-batch-how-to-access-the-current-steps-id-name-from-within-an-itemread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!