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