0

I have the following spring batch job configuration. There is a single reader which then passes details to a composite writer which has two specific writers. Both writers share a common parent and need to use the same JobId for the INSERT operations they execute.

<bean id="job" parent="simpleJob">
    <property name="steps">
        <list>
            <bean parent="simpleStep">
                <property name="itemReader" ref="policyReader"/>    
                <property name="itemWriter" ref="stagingCompositeWriter"/>
            </bean>
        </list>
    </property>
</bean>

<bean id="stagingCompositeWriter" class="org.springframework.batch.item.support.CompositeItemWriter">
    <property name="delegates">
        <list>
            <ref bean="stagingLoadWriter"/>
            <ref bean="stagingPolicyWriter"/>
        </list>
    </property>
</bean>

<bean id="abstractStagingWriter" class="a.b.c.AbstractStagingWriter" abstract="true">
    <property name="stepExecution" value="#{stepExecutionContext}"/>
    <property name="hedgingStagingDataSource" ref="hedgingStagingDataSource"/>
</bean>

<bean id="stagingLoadWriter" class="a.b.c.StagingLoadWriter" parent="abstractStagingWriter"/>

<bean id="stagingPolicyWriter" class="a.b.c.StagingPolicyWriter" parent="abstractStagingWriter"/>

When i run my code i get the following error

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'stepExecutionContext' 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.SpelNodeImpl.getValue(SpelNodeImpl.java:93)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:88)
at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:139)

I have tried setting the scope="step" in various place but to no avail. Any suggestions?

emeraldjava
  • 10,894
  • 26
  • 97
  • 170
  • can you try replacing #{stepExecutionContext} with ${stepExecutionContext}" – Vinay Lodha Jan 14 '14 at 13:04
  • 2
    Don't think so - the dollar notation would tell the context to look for a property field with name 'stepExecutionContext' which i know does not exist. – emeraldjava Jan 14 '14 at 13:22
  • 2
    my assumption: you can't wire stepExecutionContext directly, so use a StepListener. Else try place scope="step" in all *Writer beans – Luca Basso Ricci Jan 14 '14 at 14:05
  • See 5.4. Late Binding of Job and Step Attributes : http://docs.spring.io/spring-batch/2.2.x/reference/html/configureStep.html#late-binding – emeraldjava Jan 14 '14 at 14:09
  • To access jobId you can also try use the same strategy specified in http://stackoverflow.com/questions/20838522/spring-batch-how-to-access-the-current-steps-id-name-from-within-an-itemread but with `#{stepExecution.jobExecutionId}` – Luca Basso Ricci Jan 14 '14 at 15:16

1 Answers1

9

You can access the stepExecutionContext only within a bean defined in the scope="step". Change your bean definition to

<bean id="stagingLoadWriter" scope="step" class="a.b.c.StagingLoadWriter" parent="abstractStagingWriter" />

<bean id="stagingPolicyWriter" scope="step" class="a.b.c.StagingPolicyWriter" parent="abstractStagingWriter"/>
Haim Raman
  • 11,508
  • 6
  • 44
  • 70
  • I am stilly getting the same error. My code is like : @Bean(name = "myMongoItemReader") @StepScope public ItemReader searchMongoItemReader() – Shashank Aug 31 '17 at 11:34