user1611183
user1611183

Reputation: 361

spring-batch : load and use a property file

I am new on Spring batch so am here to ask some basic advice.

What is the best approach to load a config file in memory (or bean) and use its content while the spring Job/step are running ?

I am not sure but based on some google search I found the below scenario even if I dont quite understand why I should define a writer even if i dont need it :

  1. step1 : load config file (the content is two field delimited by =)
  2. step2 : perform some java code and use the previous config file

so for the step 1 :

<bean id="inputFile" class="org.springframework.core.io.FileSystemResource" scope="step">
    <constructor-arg value="path_config_file"/>
</bean>

<bean id="readerConfigFile" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
    <property name="resource" ref="inputFile"/>
    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <property name="lineTokenizer">
                <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <property name="names" value="field,value"/>
                    <property name="delimiter" value="="/>
                </bean>
            </property>
            <property name="fieldSetMapper">
                <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                    <property name="prototypeBeanName" value="configProperties"/>
                </bean>
            </property>
        </bean>
    </property>
</bean>

<bean id="outputConfig" class="outputConfig"></bean>

<bean id="configProperties" class="configProperties" scope="prototype"/>

so my question are :

  1. How can I use the information gathered in the file ? Should I put them in the Java bean ?
  2. How can I pas this info between different step or make them persistent in the whole application life-cycle ?
  3. Would you recommend to use a itemProcessor to achieve the above ?

Any advice are most than welcome

Upvotes: 0

Views: 8992

Answers (1)

Luca Basso Ricci
Luca Basso Ricci

Reputation: 18413

I'm a bit confused about your questions because I think you only need to load a properties file in spring context using a PropertiesFactoryBean:

    <bean id="config" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
      <property name="location">
        <value>file:path_config_file</value>
      </property>
    </bean>

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="propertiesArray">
        <list>
          <ref bean="config"/>
        </list>
      </property>
    </bean>

and you can refer to property values using ${} placeholder; but this solution is unrelated to spring-batch; I don't need it!

About your questions:

  1. Using a POJO is a good way because spring-batch offers in-box mapping strategies (BeanWrapperFieldSetMapper in your case)
  2. Objects used in a job are accessible only in job context, not in application context (this is why I think you need a PropertiesFactoryBean).
    To pass object between steps read How can we share data between the different steps of a Job in Spring Batch?
  3. ItemProcessor is requested if you need to convert an object T read from a ItemReader<T> to an object of type S written by an ItemWriter<S>. So no, you don't need an ItemProcessor.

I hope I was clear, English is not my native language

Upvotes: 2

Related Questions