skpraveen
skpraveen

Reputation: 303

How to pass JobParams to other beans in xml?

There are two job params filePath & fileName for my spring batch job. Requirement is to pass this filePath+fileName to other custom bean as a property value. However, I cannot "step" scope that bean to access these params. Hence I need a way to access these job params from (non-step) normal bean. Pls refere below code ::

<bean id="cardDownloadFileTemplate"
    class="org.springframework.integration.file.remote.RemoteFileTemplate">
    <constructor-arg ref="sftpSessionFactory" />
    <property name="fileNameExpression">
        <bean class="org.springframework.expression.common.LiteralExpression">
            <constructor-arg
                value="#{jobParameters['filePath']}#{jobParameters['fileName']}}" />
        </bean>
    </property>
    <property name="charset" value="${fserver.charset}" />
</bean>

Upvotes: 1

Views: 226

Answers (2)

Saifuddin Merchant
Saifuddin Merchant

Reputation: 1171

You can setup the normal beans as Step Listeners, and then implement the @BeforeStep method which can extract the JobParameters from the step execution.

In this case the value for the parameter you are extracting will not be available at bean initialization but will be able just before the step starts to run.

Initialize the state of the listener with the StepExecution from the current scope beforeStep(StepExecution stepExecution) stepExecution --> getJobParameters()

Upvotes: 0

Gary Russell
Gary Russell

Reputation: 174504

It can't be done; for normally-scoped beans (singleton, prototype) the bean is initialized with the context, before the job is launched; there is no jobParameters variable available.

Upvotes: 1

Related Questions