This is part of my job.xml
:
If you want to define your ItemReader instance and your Step instance in a single JavaConfig class. You can use the @StepScope and the @Value annotations such as:
@Configuration
public class ContributionCardBatchConfiguration {
private static final String WILL_BE_INJECTED = null;
@Bean
@StepScope
public FlatFileItemReader<ContributionCard> contributionCardReader(@Value("#{jobParameters['fileName']}")String contributionCardCsvFileName){
....
}
@Bean
Step ingestContributionCardStep(ItemReader<ContributionCard> reader){
return stepBuilderFactory.get("ingestContributionCardStep")
.<ContributionCard, ContributionCard>chunk(1)
.reader(contributionCardReader(WILL_BE_INJECTED))
.writer(contributionCardWriter())
.build();
}
}
The trick is to pass a null value to the itemReader since it will be injected through the @Value("#{jobParameters['fileName']}")
annotation.
Thanks to Tobias Flohre for his article : Spring Batch 2.2 – JavaConfig Part 2: JobParameters, ExecutionContext and StepScope
Pretty late, but you can also do this by annotating a @BeforeStep method:
@BeforeStep
public void beforeStep(final StepExecution stepExecution) {
JobParameters parameters = stepExecution.getJobExecution().getJobParameters();
//use your parameters
}
As was stated, your reader needs to be 'step' scoped. You can accomplish this via the @Scope("step")
annotation. It should work for you if you add that annotation to your reader, like the following:
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("foo-reader")
@Scope("step")
public final class MyReader implements ItemReader<MyData> {
@Override
public MyData read() throws Exception {
//...
}
@Value("#{jobParameters['fileName']}")
public void setFileName(final String name) {
//...
}
}
This scope is not available by default, but will be if you are using the batch
XML namespace. If you are not, adding the following to your Spring configuration will make the scope available, per the Spring Batch documentation:
<bean class="org.springframework.batch.core.scope.StepScope" />
Did you declare the jobparameters as map properly as bean?
Or did you perhaps accidently instantiate a JobParameters object, which has no getter for the filename?
For more on expression language you can find information in Spring documentation here.
Complement with an additional example, you can access all job parameters in JavaConfig class:
@Bean
@StepScope
public ItemStreamReader<GenericMessage> reader(@Value("#{jobParameters}") Map<String,Object> jobParameters){
....
}
To be able to use the jobParameters I think you need to define your reader as scope 'step', but I am not sure if you can do it using annotations.
Using xml-config it would go like this:
<bean id="foo-readers" scope="step"
class="...MyReader">
<property name="fileName" value="#{jobExecutionContext['fileName']}" />
</bean>
See further at the Spring Batch documentation.
Perhaps it works by using @Scope
and defining the step scope in your xml-config:
<bean class="org.springframework.batch.core.scope.StepScope" />