Spring Batch: how to pass jobParameters to a custom bean?

匿名 (未验证) 提交于 2019-12-03 02:28:01

问题:

Im still studying spring batch and came across a scenario where i need to pass a jobParameter to a custom bean. The job parameter contains a path of a file.

Here is how my context looks like:

<bean id="myBean" class=".....MyBean">      <property name="path" value="file:#{jobParameters['PATH'}/fileName"/> </bean> 

This is already included in a step scope from a reader that is not included here.

The question is. When the class is instantiated, the value passed to the bean is "file:#{jobParameters['PATH'}/fileName" instead the value of jobParameter passed when the job is invoked. It puzzles me since i've tried it on multiResourceReader in setting up the resource value and it is working fine. I'ts just i can't make it work on a custom bean. Any help woiuld be greatly appreciated.

回答1:

You need scope="step" in the bean definition.

<bean id="myBean" class=".....MyBean" scope="step">     <property name="path" value="file:#{jobParameters['PATH']}/fileName"/> </bean> 

Also your #{jobParameters['PATH'} was missing the closing bracket ]



回答2:

Further to Serkan Bey's response, make sure you either declare

<bean class="org.springframework.batch.core.scope.StepScope" />

or use the <batch:*/> namespace to make sure the step scope is picked up.



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