2013-04-25 127 views
1

如何添加属性:Spring Batch的:作业属性

<property name="myProperty" value="value"/> 

到批处理作业定义:

<batch:job id="MyJob"> 
    <batch:description>description</batch:description> 
    <batch:step id="step0"> 
     <batch:tasklet ref="MyJobCls"/> 
     <batch:listeners> 
      <batch:listener ref="MyJobkListener"/> 
     </batch:listeners> 
    </batch:step> 
</batch:job> 

然后我可以在运行时使用此属性。

回答

2

您可以在外部文件中具有的属性或XML注入它PropertyPlaceholderConfigurer:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"><value>my_config.properties</value></property> 
    <property name="properties"> 
    <props> 
     <prop key="myProperty">value</prop> 
    </props> 
    </property> 
</bean> 

更多细节在这里:Using Properties in spring config

此外,如果您运行过程中使用CommandLineJobRunner你可以将它设置命令行使用-D(How do I read JVM arguments in the Spring applicationContext.xml) a-DmyProperty =值

对于myJobCls/MyJobkListener中的myProperty值,您可以使用以下选项:

注释How can I inject a property value into a Spring Bean which was configured using annotations?

或XML配置注: http://www.mkyong.com/spring/spring-propertyplaceholderconfigurer-example/

+0

谢谢您的回答,即时寻找添加属性为Spring Batch的作业(而不是普通的bean)<批次:作业id =“MyJob”> – Yosefarr 2013-04-25 11:47:08

+0

,所以你想拥有在ChunkContext那些PARAMS? (ChunkContext.getStepContext()。getStepExecution()。getJobExecution()。getExecutionContext()) – user1121883 2013-04-25 14:35:37

+0

你能描述你打算如何使用属性? – 2013-04-26 00:27:11

0

我有点迷惑,你问什么样的财产。 字符串或某种或Spring管理豆的参考。

如果你想那么一些字符串键值对春季批次作业参数来定义它。它将通过所有步骤/提供给整个工作。

如果你想有一些物业以供参考/类,然后添加属性/类的成员变量与setter和getter在您的自定义步骤或定制作业类。 您也可以对字符串属性执行相同操作。