Main Tutorials

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>

Reference

  1. Spring Batch – Step Scope

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
8 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Patel
2 years ago

Thanks a lot, explained with 100% clarity

HYUNSANG HAN
3 years ago

Thanks!

mayur
4 years ago

thanks for sharing.. its helpful..

Kush
5 years ago

Thanks for sharing this!

Gabriel Matossian
7 years ago

Thanks for sharing! I was starting to get a headache…

Robert
9 years ago

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.

Brady
10 years ago

Thank you MkYong, this was really very much helpful. Before this I was struggling to passs the jobParameters.

Thank you once again.

Kush
5 years ago
Reply to  Brady

Wrong window