2013-04-25 128 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在您的自定義步驟或定製作業類。 您也可以對字符串屬性執行相同操作。