jobParameters cannot be found on object of type BeanExpressionContext
Create a simple Spring batch job to write data to a csv file. The csv file name depends on the pass in job’s parameters, interprets by Spring EL .
job-sample.xml
<bean id="csvFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
<!-- write to this csv file -->
<property name="resource"
value="file:outputs/csv/domain.done.#{jobParameters['pid']}.csv" />
<property name="appendAllowed" value="false" />
<property name="lineAggregator">
<bean
class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="delimiter" value="," />
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="id, domainName" />
</bean>
</property>
</bean>
</property>
</bean>
Unit test above job :
public class TestSampleJob extends AbstractTestNGSpringContextTests {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
public void launchJob() throws Exception {
JobParameters jobParameters =
new JobParametersBuilder().addString("pid", "10").toJobParameters();
JobExecution jobExecution = jobLauncherTestUtils.launchJob(jobParameters);
Assert.assertEquals(jobExecution.getStatus(), BatchStatus.COMPLETED);
}
}
Problem
It prompts “jobParameters cannot be found” error message :
Caused by: org.springframework.expression.spel.SpelEvaluationException:
EL1008E:(pos 0): Field or property 'jobParameters' cannot be found on object
of type 'org.springframework.beans.factory.config.BeanExpressionContext'
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:208)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:72)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:52)
Solution
The jobParameters
bean can not actually be instantiated until the “Step” starts. To fix it, late binding with a scope of “Step” is required.
job-sample.xml
<bean id="csvFileItemWriter"
class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
<!-- ...... -->
</bean>
Thanks a lot, explained with 100% clarity
Thanks!
thanks for sharing.. its helpful..
Thanks for sharing this!
Thanks for sharing! I was starting to get a headache…
This solution is certainly helpful. I did get an error even after I have set scope attribute, but then I noticed I had another bean which used jobParameters and didn’t have scope defined – fixed it and it works perfectly. Thank you.
Thank you MkYong, this was really very much helpful. Before this I was struggling to passs the jobParameters.
Thank you once again.
Wrong window