Spring Batch: Job Properties

半世苍凉 提交于 2019-12-11 05:36:20

问题


How can i add property:

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

To the batch job definition:

<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>

then i can use this property in my run time.


回答1:


You can have the properties in an external file or inject it in PropertyPlaceholderConfigurer from xml:

<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>

More details here: Using Properties in spring config

Also if you run you process using CommandLineJobRunner you can set it from command line using -D (How do I read JVM arguments in the Spring applicationContext.xml) a-DmyProperty=value

For myProperty value in MyJobCls/MyJobkListener you have at leas to options:

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

or inject from xml config: http://www.mkyong.com/spring/spring-propertyplaceholderconfigurer-example/




回答2:


I am little confuse, What kind of property you are asking. String or some kind or reference of spring managed beans.

If you want some string key-value pair then define it in the spring-batch-job parameter. it will be available to whole job with all the steps/.

If you want to have some property for reference/class, then add that property/class as member variable with setter and getter in your custom step or custom job class. You can do the same for string property also.



来源:https://stackoverflow.com/questions/16209838/spring-batch-job-properties

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